Skip to content

Commit

Permalink
Don't pile up already fired ping request delaying timers
Browse files Browse the repository at this point in the history
  • Loading branch information
tmolitor-stud-tu committed Dec 24, 2024
1 parent 795e89c commit 873f77d
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions Monal/Classes/xmpp.m
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,20 @@ -(void) reinitLoginTimer
}));
}

-(void) addTimerToCancelOnDisconnect:(monal_void_block_t) timer
{
@synchronized(self->_timersToCancelOnDisconnect) {
[self->_timersToCancelOnDisconnect addObject:time];
}
}

-(void) removeTimerToCancelOnDisconnect:(monal_void_block_t) timer
{
@synchronized(self->_timersToCancelOnDisconnect) {
[self->_timersToCancelOnDisconnect removeObject:time];
}
}

-(void) connect
{
if([self parseQueueFrozen])
Expand Down Expand Up @@ -1477,6 +1491,8 @@ -(void) startXMPPStreamWithXMLOpening:(BOOL) withXMLOpening withStartTLS:(BOOL)

-(void) sendPing:(double) timeout
{
static monal_void_block_t delayTimer = nil; //it doesn't matter if this has a race condition

DDLogVerbose(@"sendPing called");
[self dispatchAsyncOnReceiveQueue: ^{
DDLogVerbose(@"sendPing called - now inside receiveQueue");
Expand Down Expand Up @@ -1507,12 +1523,18 @@ -(void) sendPing:(double) timeout
}
else if([self->_parseQueue operationCount] > 4)
{
DDLogWarn(@"parseQueue overflow, delaying ping by 4 seconds.");
@synchronized(self->_timersToCancelOnDisconnect) {
[self->_timersToCancelOnDisconnect addObject:createTimer(4.0, (^{
if(delayTimer !== nil)
DDLogWarn(@"Ping already delayed, ignoring additional ping...");
else
{
DDLogWarn(@"parseQueue overflow, delaying ping by 4 seconds.");
delayTimer = createTimer(4.0, (^{
[self removeTimerToCancelOnDisconnect:delayTimer];
delayTimer = nil;
DDLogDebug(@"ping delay expired, retrying ping.");
[self sendPing:timeout];
}))];
}));
[self addTimerToCancelOnDisconnect:delayTimer];
}
}
else
Expand Down Expand Up @@ -4877,15 +4899,14 @@ -(void)stream:(NSStream*) stream handleEvent:(NSStreamEvent) eventCode
DDLogInfo(@"%@ Stream %@ encountered eof, trying to reconnect via parse queue in 1 second", [stream class], stream);
//use a timer to make sure the incoming data was pushed *through* the MLPipe and reached the parseQueue
//already when pushing our reconnect block onto the parseQueue
@synchronized(self->_timersToCancelOnDisconnect) {
[self->_timersToCancelOnDisconnect addObject:createTimer(1.0, (^{
//add this to parseQueue to make sure we completely handle everything that came in before the connection was closed, before handling the close event itself
[self->_parseQueue addOperations:@[[NSBlockOperation blockOperationWithBlock:^{
DDLogInfo(@"Inside parseQueue: %@ Stream %@ encountered eof, trying to reconnect", [stream class], stream);
[self reconnect];
}]] waitUntilFinished:NO];
}))];
}
//this timer will only be created once on every connect cycle (and removed from our timers list on reconnect/disconnect)
[self addTimerToCancelOnDisconnect:createTimer(1.0, (^{
//add this to parseQueue to make sure we completely handle everything that came in before the connection was closed, before handling the close event itself
[self->_parseQueue addOperations:@[[NSBlockOperation blockOperationWithBlock:^{
DDLogInfo(@"Inside parseQueue: %@ Stream %@ encountered eof, trying to reconnect", [stream class], stream);
[self reconnect];
}]] waitUntilFinished:NO];
}))];
break;
}
}
Expand Down

0 comments on commit 873f77d

Please sign in to comment.