Merge branch 'charlesmchen/skipRedundantSyncMessages'

pull/1/head
Matthew Chen 7 years ago
commit b271eab4c7

@ -1,5 +1,5 @@
// //
// Copyright (c) 2017 Open Whisper Systems. All rights reserved. // Copyright (c) 2018 Open Whisper Systems. All rights reserved.
// //
#import "OWSLinkDeviceViewController.h" #import "OWSLinkDeviceViewController.h"
@ -147,7 +147,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)provisionWithParser:(OWSDeviceProvisioningURLParser *)parser - (void)provisionWithParser:(OWSDeviceProvisioningURLParser *)parser
{ {
// Optimistically set this flag. // Optimistically set this flag.
[OWSDeviceManager.sharedManager setMayHaveLinkedDevices:YES dbConnection:self.dbConnection]; [OWSDeviceManager.sharedManager setMayHaveLinkedDevices];
ECKeyPair *_Nullable identityKeyPair = [[OWSIdentityManager sharedManager] identityKeyPair]; ECKeyPair *_Nullable identityKeyPair = [[OWSIdentityManager sharedManager] identityKeyPair];
OWSAssert(identityKeyPair); OWSAssert(identityKeyPair);

@ -146,9 +146,7 @@ int const OWSLinkedDevicesTableViewControllerSectionAddDevice = 1;
if (devices.count > 1) { if (devices.count > 1) {
// Setting this flag here shouldn't be necessary, but we do so // Setting this flag here shouldn't be necessary, but we do so
// because the "cost" is low and it will improve robustness. // because the "cost" is low and it will improve robustness.
[OWSDeviceManager.sharedManager [OWSDeviceManager.sharedManager setMayHaveLinkedDevices];
setMayHaveLinkedDevices:YES
dbConnection:[[TSStorageManager sharedManager] newDatabaseConnection]];
} }
if (devices.count > [OWSDevice numberOfKeysInCollection]) { if (devices.count > [OWSDevice numberOfKeysInCollection]) {

@ -1,5 +1,5 @@
// //
// Copyright (c) 2017 Open Whisper Systems. All rights reserved. // Copyright (c) 2018 Open Whisper Systems. All rights reserved.
// //
#import "TSYapDatabaseObject.h" #import "TSYapDatabaseObject.h"
@ -16,7 +16,7 @@ extern uint32_t const OWSDevicePrimaryDeviceId;
+ (instancetype)sharedManager; + (instancetype)sharedManager;
- (BOOL)mayHaveLinkedDevices:(YapDatabaseConnection *)dbConnection; - (BOOL)mayHaveLinkedDevices:(YapDatabaseConnection *)dbConnection;
- (void)setMayHaveLinkedDevices:(BOOL)value dbConnection:(YapDatabaseConnection *)dbConnection; - (void)setMayHaveLinkedDevices;
- (BOOL)hasReceivedSyncMessageInLastSeconds:(NSTimeInterval)intervalSeconds; - (BOOL)hasReceivedSyncMessageInLastSeconds:(NSTimeInterval)intervalSeconds;
- (void)setHasReceivedSyncMessage; - (void)setHasReceivedSyncMessage;

@ -1,5 +1,5 @@
// //
// Copyright (c) 2017 Open Whisper Systems. All rights reserved. // Copyright (c) 2018 Open Whisper Systems. All rights reserved.
// //
#import "OWSDevice.h" #import "OWSDevice.h"
@ -19,7 +19,9 @@ NSString *const kTSStorageManager_MayHaveLinkedDevices = @"kTSStorageManager_May
@interface OWSDeviceManager () @interface OWSDeviceManager ()
// This property should only be accessed while synchronized on self.
@property (atomic, nullable) NSNumber *mayHaveLinkedDevicesCached; @property (atomic, nullable) NSNumber *mayHaveLinkedDevicesCached;
@property (atomic) NSDate *lastReceivedSyncMessage; @property (atomic) NSDate *lastReceivedSyncMessage;
@end @end
@ -59,20 +61,25 @@ NSString *const kTSStorageManager_MayHaveLinkedDevices = @"kTSStorageManager_May
} }
} }
- (void)setMayHaveLinkedDevices:(BOOL)value dbConnection:(YapDatabaseConnection *)dbConnection - (void)setMayHaveLinkedDevices
{ {
OWSAssert(dbConnection);
@synchronized(self) @synchronized(self)
{ {
self.mayHaveLinkedDevicesCached = @(value); if (self.mayHaveLinkedDevicesCached != nil && self.mayHaveLinkedDevicesCached.boolValue) {
// Skip redundant writes.
return;
}
[dbConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) { self.mayHaveLinkedDevicesCached = @(YES);
[transaction setObject:@(value) }
// Note that we write async to avoid opening transactions within transactions.
[TSStorageManager.sharedManager.newDatabaseConnection
asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
[transaction setObject:@(YES)
forKey:kTSStorageManager_MayHaveLinkedDevices forKey:kTSStorageManager_MayHaveLinkedDevices
inCollection:kTSStorageManager_OWSDeviceCollection]; inCollection:kTSStorageManager_OWSDeviceCollection];
}]; }];
}
} }
- (BOOL)hasReceivedSyncMessageInLastSeconds:(NSTimeInterval)intervalSeconds - (BOOL)hasReceivedSyncMessageInLastSeconds:(NSTimeInterval)intervalSeconds
@ -83,6 +90,8 @@ NSString *const kTSStorageManager_MayHaveLinkedDevices = @"kTSStorageManager_May
- (void)setHasReceivedSyncMessage - (void)setHasReceivedSyncMessage
{ {
self.lastReceivedSyncMessage = [NSDate new]; self.lastReceivedSyncMessage = [NSDate new];
[self setMayHaveLinkedDevices];
} }
@end @end

@ -1,5 +1,5 @@
// //
// Copyright (c) 2017 Open Whisper Systems. All rights reserved. // Copyright (c) 2018 Open Whisper Systems. All rights reserved.
// //
#import "OWSMessageSender.h" #import "OWSMessageSender.h"
@ -1090,7 +1090,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
if (missingDevices.count > 0) { if (missingDevices.count > 0) {
NSString *localNumber = [TSAccountManager localNumber]; NSString *localNumber = [TSAccountManager localNumber];
if ([localNumber isEqualToString:recipient.uniqueId]) { if ([localNumber isEqualToString:recipient.uniqueId]) {
[OWSDeviceManager.sharedManager setMayHaveLinkedDevices:YES dbConnection:self.dbConnection]; [OWSDeviceManager.sharedManager setMayHaveLinkedDevices];
} }
} }

Loading…
Cancel
Save