Remove connection timeout

Summary:
This change mainly removes the connection timeout period as it may introduce a race condition in which slow server connections get terminated potentially creating connection loops.

Also, suspend and wait for all operations to complete once a socket is disconnected.

Reviewed By: ivanmisuno

Differential Revision: D43048252

fbshipit-source-id: 64c28a3d3d2fd4e065084d5f55a17444385c07e0
This commit is contained in:
Lorenzo Blasa
2023-02-07 05:55:11 -08:00
committed by Facebook GitHub Bot
parent d97dfae1a0
commit e9c0a459dd
2 changed files with 21 additions and 23 deletions

View File

@@ -166,25 +166,26 @@ static constexpr int connectionKeepaliveSeconds = 10;
} }
- (void)disconnect { - (void)disconnect {
[_dispatchQueue cancelAllOperations];
if ([_keepAlive isValid]) { if ([_keepAlive isValid]) {
[_keepAlive invalidate]; [_keepAlive invalidate];
} }
_keepAlive = nil; _keepAlive = nil;
if (_socket) {
// Clear the socket delegate before close. Ensures that we won't get
// any messages after the disconnect takes place.
[_socket setDelegate:nil];
[_socket close];
_socket = nil;
};
[_dispatchQueue cancelAllOperations];
[_dispatchQueue waitUntilAllOperationsAreFinished];
// Manually trigger a 'close' event as SocketRocket close method will // Manually trigger a 'close' event as SocketRocket close method will
// not notify the delegate. SocketRocket only triggers the close event // not notify the delegate. SocketRocket only triggers the close event
// when the connection is closed from the server. // when the connection is closed from the server.
_eventHandler(facebook::flipper::SocketEvent::CLOSE); _eventHandler(facebook::flipper::SocketEvent::CLOSE);
if (_socket) {
// Clear the socket delegate before close. Ensures that we won't get
// any messages after the disconnect takes place.
_socket.delegate = nil;
[_socket close];
_socket = nil;
};
} }
- (void)send:(NSString*)message - (void)send:(NSString*)message
@@ -192,10 +193,12 @@ static constexpr int connectionKeepaliveSeconds = 10;
__weak auto weakSelf = self; __weak auto weakSelf = self;
[_dispatchQueue addOperationWithBlock:^{ [_dispatchQueue addOperationWithBlock:^{
__strong auto strongSelf = weakSelf; __strong auto strongSelf = weakSelf;
NSError* error = nil; if (strongSelf) {
[strongSelf->_socket sendString:message error:&error]; NSError* error = nil;
if (completionHandler) { [strongSelf->_socket sendString:message error:&error];
completionHandler(error); if (completionHandler) {
completionHandler(error);
}
} }
}]; }];
} }
@@ -210,7 +213,9 @@ static constexpr int connectionKeepaliveSeconds = 10;
__weak auto weakSelf = self; __weak auto weakSelf = self;
[_dispatchQueue addOperationWithBlock:^{ [_dispatchQueue addOperationWithBlock:^{
__strong auto strongSelf = weakSelf; __strong auto strongSelf = weakSelf;
[strongSelf->_socket sendPing:nil error:nil]; if (strongSelf) {
[strongSelf->_socket sendPing:nil error:nil];
}
}]; }];
} }
@@ -251,7 +256,6 @@ static constexpr int connectionKeepaliveSeconds = 10;
_keepAlive = nil; _keepAlive = nil;
_eventHandler(facebook::flipper::SocketEvent::CLOSE); _eventHandler(facebook::flipper::SocketEvent::CLOSE);
_socket = nil;
} }
- (void)_webSocketDidReceiveMessage:(id)message { - (void)_webSocketDidReceiveMessage:(id)message {

View File

@@ -125,13 +125,7 @@ bool FlipperWebSocket::connect(FlipperConnectionManager* manager) {
[socket_ connect]; [socket_ connect];
auto state = connected.wait_for(std::chrono::seconds(10)); return connected.get();
if (state == std::future_status::ready) {
return connected.get();
}
disconnect();
return false;
} }
void FlipperWebSocket::disconnect() { void FlipperWebSocket::disconnect() {