2

Is there a way to limit the bandwidth in NSURLSession?

I'm making file-sync-client for macOS like Dropbox/GoogleDrive/pCloud and they all have bandwidth limiting options, but I'm not sure how to configure NSURLSession to respect bandwidth limiting.

Vladyslav
  • 2,018
  • 4
  • 18
  • 44
mixtly87
  • 1,675
  • 15
  • 32

2 Answers2

2

Unless Apple has added something very recently, NSURLSession provides no facilities for bandwidth limiting. The only ways I'm aware of to do that are:

  • Use lower-level APIs that allow you to provide your own sockets and then throttle the data rate at the TCP socket level.
  • Provide an in-app web proxy and use it for all outgoing requests. Configure the proxy to limit the bandwidth of all requests that go through it.
dgatwood
  • 10,129
  • 1
  • 28
  • 49
  • Thanks dgatwood, do you know of any in-app web proxy which could help in this case? – mixtly87 Oct 02 '17 at 11:28
  • Hi, following your answer, NSURLSession is using CFNetwork internally, just like the ASIHTTPRequest mentioned in the previous answer. As ASIHTTPRequest supports throttling, maybe there's a way to access the lower level CFNetwork objects from the NSURLSession, and configure them for throttling? do you recon this is possible? – Motti Shneor Jul 06 '20 at 14:03
  • 1
    No. NSURLSession doesn't expose its CF underpinnings at all. And in background sessions (which you would probably want to use for something that does large uploads and downloads), the requests aren't even issued within your app's process, so marshalling the CF structures across the process boundaries would likely not work correctly even if you could find a way to access those structures in foreground requests. This really is a "file an enhancement request" thing. – dgatwood Jul 06 '20 at 18:56
1

Looks like ASIHTTPRequest features Bandwidth throttling support. Although it may be an old library, from what I observe it's still fully functional thus it may be worth exploring.

Of course, using ASIHTTPRequest would require abandoning NSURLSession for this purpose.

mixtly87
  • 1,675
  • 15
  • 32