@onekeyfe/react-native-network-throttle 3.0.79 → 3.0.80

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,6 +8,7 @@ static NSString *const OneKeyNetworkThrottleHandledKey = @"OneKeyNetworkThrottle
8
8
  static NSString *const OneKeyNetworkThrottleProfileSlow4G = @"slow4g";
9
9
  static const NSTimeInterval OneKeyNetworkThrottleDefaultLatencyMs = 562.5;
10
10
  static const NSInteger OneKeyNetworkThrottleDefaultThroughputBps = 102 * 1024;
11
+ static const NSUInteger OneKeyNetworkThrottleMaxPendingDownloadBytes = 256 * 1024;
11
12
 
12
13
  @interface OneKeyNetworkThrottleState : NSObject
13
14
  + (NSDictionary *)currentConfig;
@@ -111,7 +112,9 @@ static atomic_llong _oneKeyNetworkThrottleUploadBps = ATOMIC_VAR_INIT(102 * 1024
111
112
  @property (atomic, assign) BOOL upstreamCompleted;
112
113
  @property (nonatomic, strong) NSError *upstreamError;
113
114
  @property (nonatomic, strong) NSMutableArray<NSData *> *pendingData;
115
+ @property (nonatomic, assign) NSUInteger pendingDataBytes;
114
116
  @property (nonatomic, copy) void (^pendingResponseCompletionHandler)(NSURLSessionResponseDisposition disposition);
117
+ @property (atomic, assign) BOOL upstreamSuspendedForBackpressure;
115
118
  - (NSTimeInterval)remainingLatencyDelay;
116
119
  - (NSTimeInterval)uploadDelayForRequest:(NSURLRequest *)request;
117
120
  - (NSTimeInterval)downloadDelayForDataLength:(NSUInteger)dataLength;
@@ -119,6 +122,8 @@ static atomic_llong _oneKeyNetworkThrottleUploadBps = ATOMIC_VAR_INIT(102 * 1024
119
122
  - (void)flushPendingData;
120
123
  - (void)finishIfPossible;
121
124
  - (void)cancelPendingResponseCompletionHandler;
125
+ - (void)applyUpstreamBackpressureIfNeeded;
126
+ - (void)clearPendingData;
122
127
  - (void)invalidateSessionAndClearTaskWithCancel:(BOOL)cancel;
123
128
  @end
124
129
 
@@ -155,6 +160,8 @@ static atomic_llong _oneKeyNetworkThrottleUploadBps = ATOMIC_VAR_INIT(102 * 1024
155
160
  self.upstreamCompleted = NO;
156
161
  self.upstreamError = nil;
157
162
  self.pendingData = [NSMutableArray array];
163
+ self.pendingDataBytes = 0;
164
+ self.upstreamSuspendedForBackpressure = NO;
158
165
 
159
166
  NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
160
167
  NSNumber *useWifiOnly = [[NSBundle mainBundle].infoDictionary objectForKey:@"ReactNetworkForceWifiOnly"];
@@ -182,6 +189,7 @@ static atomic_llong _oneKeyNetworkThrottleUploadBps = ATOMIC_VAR_INIT(102 * 1024
182
189
  {
183
190
  self.stopped = YES;
184
191
  [self cancelPendingResponseCompletionHandler];
192
+ [self clearPendingData];
185
193
  [self invalidateSessionAndClearTaskWithCancel:YES];
186
194
  }
187
195
 
@@ -273,8 +281,14 @@ static atomic_llong _oneKeyNetworkThrottleUploadBps = ATOMIC_VAR_INIT(102 * 1024
273
281
  [self.client URLProtocol:self didLoadData:data];
274
282
  @synchronized (self) {
275
283
  self.downloadedBytes += (long long)data.length;
284
+ if (self.pendingDataBytes >= data.length) {
285
+ self.pendingDataBytes -= data.length;
286
+ } else {
287
+ self.pendingDataBytes = 0;
288
+ }
276
289
  self.flushingData = NO;
277
290
  }
291
+ [self applyUpstreamBackpressureIfNeeded];
278
292
  [self flushPendingData];
279
293
  });
280
294
  }
@@ -306,6 +320,7 @@ static atomic_llong _oneKeyNetworkThrottleUploadBps = ATOMIC_VAR_INIT(102 * 1024
306
320
  [self.client URLProtocolDidFinishLoading:self];
307
321
  }
308
322
  self.stopped = YES;
323
+ [self clearPendingData];
309
324
  [self invalidateSessionAndClearTaskWithCancel:NO];
310
325
  }
311
326
 
@@ -321,6 +336,40 @@ static atomic_llong _oneKeyNetworkThrottleUploadBps = ATOMIC_VAR_INIT(102 * 1024
321
336
  }
322
337
  }
323
338
 
339
+ - (void)applyUpstreamBackpressureIfNeeded
340
+ {
341
+ NSURLSessionDataTask *taskToSuspend = nil;
342
+ NSURLSessionDataTask *taskToResume = nil;
343
+ @synchronized (self) {
344
+ if (self.stopped || !self.task || self.downloadBps <= 0) {
345
+ return;
346
+ }
347
+ BOOL shouldSuspend = self.pendingDataBytes >= OneKeyNetworkThrottleMaxPendingDownloadBytes;
348
+ if (shouldSuspend && !self.upstreamSuspendedForBackpressure) {
349
+ self.upstreamSuspendedForBackpressure = YES;
350
+ taskToSuspend = self.task;
351
+ } else if (!shouldSuspend && self.upstreamSuspendedForBackpressure) {
352
+ self.upstreamSuspendedForBackpressure = NO;
353
+ taskToResume = self.task;
354
+ }
355
+ }
356
+ if (taskToSuspend) {
357
+ [taskToSuspend suspend];
358
+ }
359
+ if (taskToResume) {
360
+ [taskToResume resume];
361
+ }
362
+ }
363
+
364
+ - (void)clearPendingData
365
+ {
366
+ @synchronized (self) {
367
+ [self.pendingData removeAllObjects];
368
+ self.pendingDataBytes = 0;
369
+ self.upstreamSuspendedForBackpressure = NO;
370
+ }
371
+ }
372
+
324
373
  - (void)invalidateSessionAndClearTaskWithCancel:(BOOL)cancel
325
374
  {
326
375
  NSURLSessionDataTask *task = nil;
@@ -374,7 +423,9 @@ didReceiveResponse:(NSURLResponse *)response
374
423
  }
375
424
  @synchronized (self) {
376
425
  [self.pendingData addObject:data];
426
+ self.pendingDataBytes += data.length;
377
427
  }
428
+ [self applyUpstreamBackpressureIfNeeded];
378
429
  if (self.responseDelivered) {
379
430
  [self flushPendingData];
380
431
  }
@@ -390,6 +441,7 @@ didReceiveResponse:(NSURLResponse *)response
390
441
  [self.client URLProtocol:self didFailWithError:error];
391
442
  self.stopped = YES;
392
443
  }
444
+ [self clearPendingData];
393
445
  [self invalidateSessionAndClearTaskWithCancel:NO];
394
446
  return;
395
447
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/react-native-network-throttle",
3
- "version": "3.0.79",
3
+ "version": "3.0.80",
4
4
  "description": "react-native-network-throttle",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",