mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-09-03 03:10:47 +00:00
133 lines
2.3 KiB
Objective-C
133 lines
2.3 KiB
Objective-C
|
|
|
|
#import "MTHttpWorkerBehaviour.h"
|
|
|
|
#import "MTQueue.h"
|
|
#import "MTTimer.h"
|
|
|
|
@interface MTHttpWorkerBehaviour ()
|
|
{
|
|
MTQueue *_queue;
|
|
MTTimer *_backoffTimer;
|
|
NSInteger _backoffCount;
|
|
|
|
bool _workersNeeded;
|
|
}
|
|
|
|
@end
|
|
|
|
@implementation MTHttpWorkerBehaviour
|
|
|
|
- (instancetype)initWithQueue:(MTQueue *)queue
|
|
{
|
|
self = [super init];
|
|
if (self != nil)
|
|
{
|
|
_queue = queue;
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (void)dealloc
|
|
{
|
|
[self invalidateTimer];
|
|
}
|
|
|
|
- (void)clearBackoff
|
|
{
|
|
[_queue dispatchOnQueue:^
|
|
{
|
|
_backoffCount = 0;
|
|
}];
|
|
}
|
|
|
|
- (void)setWorkersNeeded
|
|
{
|
|
[_queue dispatchOnQueue:^
|
|
{
|
|
_workersNeeded = true;
|
|
|
|
if (_backoffCount <= 1)
|
|
[self timerEvent];
|
|
else
|
|
{
|
|
NSTimeInterval delay = 1.0;
|
|
|
|
if (_backoffCount <= 5)
|
|
delay = 1.0;
|
|
else if (_backoffCount <= 20)
|
|
delay = 4.0;
|
|
else
|
|
delay = 8.0;
|
|
|
|
[self startTimer:delay];
|
|
}
|
|
}];
|
|
}
|
|
|
|
- (void)workerConnected
|
|
{
|
|
[_queue dispatchOnQueue:^
|
|
{
|
|
_backoffCount = 0;
|
|
|
|
if (_workersNeeded)
|
|
[self timerEvent];
|
|
}];
|
|
}
|
|
|
|
- (void)workerReceivedValidData
|
|
{
|
|
}
|
|
|
|
- (void)workerDisconnectedWithError
|
|
{
|
|
[_queue dispatchOnQueue:^
|
|
{
|
|
_backoffCount++;
|
|
}];
|
|
}
|
|
|
|
- (void)invalidateTimer
|
|
{
|
|
MTTimer *backoffTimer = _backoffTimer;
|
|
_backoffTimer = nil;
|
|
|
|
[_queue dispatchOnQueue:^
|
|
{
|
|
[backoffTimer invalidate];
|
|
}];
|
|
}
|
|
|
|
- (void)startTimer:(NSTimeInterval)timeout
|
|
{
|
|
[self invalidateTimer];
|
|
|
|
[_queue dispatchOnQueue:^
|
|
{
|
|
__weak MTHttpWorkerBehaviour *weakSelf = self;
|
|
_backoffTimer = [[MTTimer alloc] initWithTimeout:timeout repeat:false completion:^
|
|
{
|
|
__strong MTHttpWorkerBehaviour *strongSelf = weakSelf;
|
|
[strongSelf timerEvent];
|
|
} queue:[_queue nativeQueue]];
|
|
[_backoffTimer start];
|
|
}];
|
|
}
|
|
|
|
- (void)timerEvent
|
|
{
|
|
[self invalidateTimer];
|
|
|
|
[_queue dispatchOnQueue:^
|
|
{
|
|
_workersNeeded = false;
|
|
|
|
id<MTHttpWorkerBehaviourDelegate> delegate = _delegate;
|
|
if ([delegate respondsToSelector:@selector(httpWorkerBehaviourAllowsNewWorkerCreation:)])
|
|
[delegate httpWorkerBehaviourAllowsNewWorkerCreation:self];
|
|
}];
|
|
}
|
|
|
|
@end
|