Make throttling a tiny bit more accurate by reducing the sizes of reads when our…
… allowance is nearly used up
Showing
1 changed file
with
18 additions
and
2 deletions
| @@ -1646,6 +1646,22 @@ BOOL shouldThrottleBandwidth = NO; | @@ -1646,6 +1646,22 @@ BOOL shouldThrottleBandwidth = NO; | ||
| 1646 | bufferSize = 16384; | 1646 | bufferSize = 16384; |
| 1647 | } | 1647 | } |
| 1648 | 1648 | ||
| 1649 | + // Reduce the buffer size if we're receiving data too quickly when bandwidth throttling is active | ||
| 1650 | + // This just augments the throttling done in performBandwidthThrottling | ||
| 1651 | + if ([ASIHTTPRequest shouldThrottleBandwidth]) { | ||
| 1652 | + long long maxSize = [ASIHTTPRequest maxBandwidthPerSecond]; | ||
| 1653 | + if (maxSize > 0) { | ||
| 1654 | + maxSize = maxSize-[ASIHTTPRequest bandwidthUsedInLastSecond]; | ||
| 1655 | + if (maxSize < 0) { | ||
| 1656 | + // We aren't supposed to read any more data right now, but we'll read a single byte anyway so the CFNetwork's buffer isn't full | ||
| 1657 | + bufferSize = 1; | ||
| 1658 | + } else if (maxSize < bufferSize) { | ||
| 1659 | + // We were going to fetch more data that we should be allowed, so we'll reduce the size of our read | ||
| 1660 | + bufferSize = maxSize; | ||
| 1661 | + } | ||
| 1662 | + } | ||
| 1663 | + } | ||
| 1664 | + | ||
| 1649 | UInt8 buffer[bufferSize]; | 1665 | UInt8 buffer[bufferSize]; |
| 1650 | CFIndex bytesRead = CFReadStreamRead(readStream, buffer, sizeof(buffer)); | 1666 | CFIndex bytesRead = CFReadStreamRead(readStream, buffer, sizeof(buffer)); |
| 1651 | 1667 | ||
| @@ -2320,12 +2336,12 @@ BOOL shouldThrottleBandwidth = NO; | @@ -2320,12 +2336,12 @@ BOOL shouldThrottleBandwidth = NO; | ||
| 2320 | 2336 | ||
| 2321 | + (void)performBandwidthThrottling | 2337 | + (void)performBandwidthThrottling |
| 2322 | { | 2338 | { |
| 2323 | - // Other requests may have to wait for this lock if we're sleeping, but this is fine, since we already know they shouldn't be sending or receiving data | 2339 | + // Other requests may have to wait for this lock if we're sleeping, but this is fine, since in that case we already know they shouldn't be sending or receiving data |
| 2324 | [bandwidthThrottlingLock lock]; | 2340 | [bandwidthThrottlingLock lock]; |
| 2325 | 2341 | ||
| 2326 | // Are we performing bandwidth throttling? | 2342 | // Are we performing bandwidth throttling? |
| 2327 | if (maxBandwidthPerSecond > 0) { | 2343 | if (maxBandwidthPerSecond > 0) { |
| 2328 | - if (!bandwidthThrottlingMeasurementDate) { | 2344 | + if (!bandwidthThrottlingMeasurementDate || [bandwidthThrottlingMeasurementDate timeIntervalSinceNow] < 0) { |
| 2329 | bandwidthThrottlingMeasurementDate = [NSDate dateWithTimeIntervalSinceNow:1]; | 2345 | bandwidthThrottlingMeasurementDate = [NSDate dateWithTimeIntervalSinceNow:1]; |
| 2330 | } | 2346 | } |
| 2331 | 2347 |
-
Please register or login to post a comment