6

Is it possible to throttle the bandwidth of an upload operation in e.g. Alamofire?

I would like to upload data in the background while the user is using the app and up- and downloading more important stuff.
Therefore, I would like to throttle the bandwidth in the background under specific circumstances.

The only possibility I found so far is using ASIHTTPRequest, which has a maxBandwidthPerSecond property, but that library is far too old, I would like to use something newer.

boraseoksoon
  • 2,164
  • 1
  • 20
  • 25
swalkner
  • 16,679
  • 31
  • 123
  • 210
  • Related? [looking for alternative solution than IPFW for slowing down an internet connection](http://stackoverflow.com/q/33064329/2415822) – JAL Oct 03 '16 at 21:46

2 Answers2

0

The Chilkat API provides a CKOSocket() that provides the possibility to throttle the used bandwidths like this:

//  To use bandwidth throttling, the connection should be made using the socket API.
//  This provides numerous properties to customize the connection, such as
//  BandwidthThrottleDown, BandwidthThrottleUp, ClientIpAddress, ClintPort, Http Proxy,
//  KeepAlive, PreferIpv6, RequireSslCertVerify, SoRcvBuf, SoSndBuf, SoReuseAddr,
//  SOCKS proxy, TcpNoSDelay, TlsPinSet, TlsCipherSuite, SslAllowedCiphers, etc.

let socket = CkoSocket()
var maxWaitMs: Int = 5000
var success: Bool = socket.Connect("content.dropboxapi.com", port: 443, ssl: true, maxWaitMs: maxWaitMs)
if success != true {
    print("\(socket.LastErrorText)")
    print("Connect Fail Reason: \(socket.ConnectFailReason.integerValue)")
    return
}

//  Set the upload bandwidth throttle rate to 50000 bytes per second.
socket.BandwidthThrottleUp = 50000 

Check this for further documentation.

The example in the documentation demonstrates how to use upload bandwidth throttling with the REST API. It will upload a file to Drobox using a file stream, with a limit on the bandwidth that can be used for the transfer.

David Seek
  • 16,783
  • 19
  • 105
  • 136
0

I cannot find anything about Alamofire (swift part) but you could use AFNetwork.

As you can see from this link (AFNetworking/AFNetworking/AFURLRequestSerialization.h) the sources report:

/**
 Throttles request bandwidth by limiting the packet size and adding a delay for each chunk read from the upload stream.
 When uploading over a 3G or EDGE connection, requests may fail with "request body stream exhausted". Setting a maximum packet size and delay according to the recommended values (`kAFUploadStream3GSuggestedPacketSize` and `kAFUploadStream3GSuggestedDelay`) lowers the risk of the input stream exceeding its allocated bandwidth. Unfortunately, there is no definite way to distinguish between a 3G, EDGE, or LTE connection over `NSURLConnection`. As such, it is not recommended that you throttle bandwidth based solely on network reachability. Instead, you should consider checking for the "request body stream exhausted" in a failure block, and then retrying the request with throttled bandwidth.
 @param numberOfBytes Maximum packet size, in number of bytes. The default packet size for an input stream is 16kb.
 @param delay Duration of delay each time a packet is read. By default, no delay is set.
 */
- (void)throttleBandwidthWithPacketSize:(NSUInteger)numberOfBytes
                                  delay:(NSTimeInterval)delay;

@end

You could build a your custom "NetworkManager" class in objective-C and easily import it to your swift project.

Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133