Fix: No contacts/groups after initial device link

The server caches your device list on the websocket, so sending on the
websocket to a just-linked device will always fail. We could close/open the
websocket, but that might be disruptive in it's own way. Instead we'll closely
mirror the Android approach, where WebSocket sends are attempted only one time,
and failure is handled by falling back to the original REST approach.

So note: we don't do any special handling of failures on the websocket
(409/410). We simply retry it with REST which will handle the 409/410/etc.

Consequently, we don't want to decrement our retry count for websocket sends.
pull/1/head
Michael Kirk 7 years ago
parent c918118509
commit 8576de0618

@ -625,11 +625,12 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
} }
[self sendMessageToService:message [self sendMessageToService:message
recipient:recipient recipient:recipient
thread:thread thread:thread
attempts:OWSMessageSenderRetryAttempts attempts:OWSMessageSenderRetryAttempts
success:successHandler useWebsocketIfAvailable:YES
failure:failureHandler]; success:successHandler
failure:failureHandler];
} else { } else {
// Neither a group nor contact thread? This should never happen. // Neither a group nor contact thread? This should never happen.
OWSFail(@"%@ Unknown message type: %@", self.logTag, NSStringFromClass([message class])); OWSFail(@"%@ Unknown message type: %@", self.logTag, NSStringFromClass([message class]));
@ -652,6 +653,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
recipient:recipient recipient:recipient
thread:thread thread:thread
attempts:OWSMessageSenderRetryAttempts attempts:OWSMessageSenderRetryAttempts
useWebsocketIfAvailable:YES
success:^{ success:^{
[futureSource trySetResult:@1]; [futureSource trySetResult:@1];
} }
@ -772,7 +774,8 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
- (void)sendMessageToService:(TSOutgoingMessage *)message - (void)sendMessageToService:(TSOutgoingMessage *)message
recipient:(SignalRecipient *)recipient recipient:(SignalRecipient *)recipient
thread:(nullable TSThread *)thread thread:(nullable TSThread *)thread
attempts:(int)remainingAttempts attempts:(int)remainingAttemptsParam
useWebsocketIfAvailable:(BOOL)useWebsocketIfAvailable
success:(void (^)(void))successHandler success:(void (^)(void))successHandler
failure:(RetryableFailureHandler)failureHandler failure:(RetryableFailureHandler)failureHandler
{ {
@ -806,7 +809,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
return failureHandler(error); return failureHandler(error);
} }
if (remainingAttempts <= 0) { if (remainingAttemptsParam <= 0) {
// We should always fail with a specific error. // We should always fail with a specific error.
OWSProdFail([OWSAnalyticsEvents messageSenderErrorGenericSendFailure]); OWSProdFail([OWSAnalyticsEvents messageSenderErrorGenericSendFailure]);
@ -814,7 +817,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
[error setIsRetryable:YES]; [error setIsRetryable:YES];
return failureHandler(error); return failureHandler(error);
} }
remainingAttempts -= 1; int remainingAttempts = remainingAttemptsParam - 1;
NSArray<NSDictionary *> *deviceMessages; NSArray<NSDictionary *> *deviceMessages;
@try { @try {
@ -978,7 +981,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
messages:deviceMessages messages:deviceMessages
relay:recipient.relay relay:recipient.relay
timeStamp:message.timestamp]; timeStamp:message.timestamp];
if (TSSocketManager.canMakeRequests) { if (useWebsocketIfAvailable && TSSocketManager.canMakeRequests) {
[TSSocketManager.sharedManager makeRequest:request [TSSocketManager.sharedManager makeRequest:request
success:^(id _Nullable responseObject) { success:^(id _Nullable responseObject) {
[self messageSendDidSucceed:message [self messageSendDidSucceed:message
@ -988,19 +991,21 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
success:successHandler]; success:successHandler];
} }
failure:^(NSInteger statusCode, NSData *_Nullable responseData, NSError *error) { failure:^(NSInteger statusCode, NSData *_Nullable responseData, NSError *error) {
[self messageSendDidFail:message // Websockets can fail in different ways, so we don't decrement remainingAttempts for websocket failure.
recipient:recipient // Instead we fall back to REST, which will decrement retries.
thread:thread // e.g. after linking a new device, sync messages will fail until the websocket re-opens.
isLocalNumber:isLocalNumber [self sendMessageToService:message
deviceMessages:deviceMessages recipient:recipient
remainingAttempts:remainingAttempts thread:thread
statusCode:statusCode attempts:remainingAttemptsParam
error:error useWebsocketIfAvailable:NO
responseData:responseData success:successHandler
success:successHandler failure:failureHandler];
failure:failureHandler];
}]; }];
} else { } else {
if (!useWebsocketIfAvailable && TSSocketManager.canMakeRequests) {
DDLogDebug(@"%@ in %s falling back to REST since first attempt failed.", self.logTag, __PRETTY_FUNCTION__);
}
[self.networkManager makeRequest:request [self.networkManager makeRequest:request
success:^(NSURLSessionDataTask *task, id responseObject) { success:^(NSURLSessionDataTask *task, id responseObject) {
[self messageSendDidSucceed:message [self messageSendDidSucceed:message
@ -1098,11 +1103,12 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
dispatch_async([OWSDispatch sendingQueue], ^{ dispatch_async([OWSDispatch sendingQueue], ^{
DDLogDebug(@"%@ Retrying: %@", self.logTag, message.debugDescription); DDLogDebug(@"%@ Retrying: %@", self.logTag, message.debugDescription);
[self sendMessageToService:message [self sendMessageToService:message
recipient:recipient recipient:recipient
thread:thread thread:thread
attempts:remainingAttempts attempts:remainingAttempts
success:successHandler useWebsocketIfAvailable:NO
failure:failureHandler]; success:successHandler
failure:failureHandler];
}); });
}; };
@ -1250,6 +1256,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
recipient:[SignalRecipient selfRecipient] recipient:[SignalRecipient selfRecipient]
thread:message.thread thread:message.thread
attempts:OWSMessageSenderRetryAttempts attempts:OWSMessageSenderRetryAttempts
useWebsocketIfAvailable:YES
success:^{ success:^{
DDLogInfo(@"Successfully sent sync transcript."); DDLogInfo(@"Successfully sent sync transcript.");
} }

Loading…
Cancel
Save