Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to update wwan throttling code so that 3g performs better than 2g, but both still work #171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Classes/ASIHTTPRequest.h
Expand Up @@ -71,7 +71,7 @@ extern NSString* const NetworkRequestErrorDomain;
// You can use this number to throttle upload and download bandwidth in iPhone OS apps send or receive a large amount of data
// This may help apps that might otherwise be rejected for inclusion into the app store for using excessive bandwidth
// This number is not official, as far as I know there is no officially documented bandwidth limit
extern unsigned long const ASIWWANBandwidthThrottleAmount;
extern unsigned long const ASIWWANLowBandwidthThrottleAmount;

#if NS_BLOCKS_AVAILABLE
typedef void (^ASIBasicBlock)(void);
Expand Down
45 changes: 41 additions & 4 deletions Classes/ASIHTTPRequest.m
Expand Up @@ -94,8 +94,28 @@ static void ReadStreamClientCallBack(CFReadStreamRef readStream, CFStreamEventTy
// the maximum number of bytes that can be transmitted in one second
static unsigned long maxBandwidthPerSecond = 0;

// A default figure for throttling bandwidth on mobile devices
unsigned long const ASIWWANBandwidthThrottleAmount = 14800;
// A default figure for throttling bandwidth on slower mobile devices
// Docs suggest 5MB/5Min for entire device, this allows for some overhead
// And some additional device traffic. This is absolutely not perfect, just
// a good approximation.
unsigned long const ASIWWANLowBandwidthThrottleAmount = 14800;

// A default figure for throttling bandwidth on faster mobile connections
unsigned long const ASIWWANHighBandwidthThrottleAmount = 148000;

// If we don't get enough traffic during a bandwidthMeasurement cycle this is incremented
// If we do get good bandwidth this is set back to 0
static unsigned long wwanBandwidthStrikes = 0;

// If bandwidth in a measurement cycle is less than this value we ignore the cycle
unsigned long const ASIWWANMinBandwidthThreashold = 1024;

// If bandwidth in a cycle is less than this value we increment wwanBandwidthStrikes
unsigned long const ASIWWANMaxBandwidthThreashold = 65537;

// When wwanBandwidthStrikes reaches this number, the WWAN throttle is downgraded
unsigned long const ASIWWANBandwidthStrikesForFallback = 2;


#if TARGET_OS_IPHONE
// YES when bandwidth throttling is active
Expand Down Expand Up @@ -4270,8 +4290,23 @@ + (void)recordBandwidthUsage
}
}
#if DEBUG_THROTTLING
NSLog(@"===Used: %u bytes of bandwidth in last measurement period===",bandwidthUsedInLastSecond);
NSLog(@"===Used: %lu bytes of bandwidth in last measurement period===",bandwidthUsedInLastSecond);
#endif

if (maxBandwidthPerSecond > ASIWWANLowBandwidthThrottleAmount && bandwidthUsedInLastSecond > ASIWWANMinBandwidthThreashold && wwanBandwidthStrikes < ASIWWANBandwidthStrikesForFallback) {
if (bandwidthUsedInLastSecond < ASIWWANMaxBandwidthThreashold) {
wwanBandwidthStrikes++;
if (wwanBandwidthStrikes >= ASIWWANBandwidthStrikesForFallback) {
#if DEBUG_THROTTLING
NSLog(@"Reducing WWAN throttle to %ld bytes/second because of low bandwidth in %lu measurement cycles, bandwidth used in last second was %lu", ASIWWANBandwidthThrottleAmount, wwanBandwidthStrikes, bandwidthUsedInLastSecond);
#endif
maxBandwidthPerSecond = ASIWWANLowBandwidthThrottleAmount;
}
} else {
wwanBandwidthStrikes = 0;
}
}

[bandwidthUsageTracker addObject:[NSNumber numberWithUnsignedLong:bandwidthUsedInLastSecond]];
[bandwidthMeasurementDate release];
bandwidthMeasurementDate = [[NSDate dateWithTimeIntervalSinceNow:1] retain];
Expand Down Expand Up @@ -4351,7 +4386,9 @@ + (unsigned long)maxUploadReadLength
+ (void)setShouldThrottleBandwidthForWWAN:(BOOL)throttle
{
if (throttle) {
[ASIHTTPRequest throttleBandwidthForWWANUsingLimit:ASIWWANBandwidthThrottleAmount];
// Reset throttle and fallback strikes counter
[ASIHTTPRequest throttleBandwidthForWWANUsingLimit:ASIWWANHighBandwidthThrottleAmount];
wwanBandwidthStrikes = 0;
} else {
[ASIHTTPRequest unsubscribeFromNetworkReachabilityNotifications];
[ASIHTTPRequest setMaxBandwidthPerSecond:0];
Expand Down