|
|
|
@ -5,6 +5,7 @@
|
|
|
|
|
#import "OWSBatchMessageProcessor.h"
|
|
|
|
|
#import "NSArray+OWS.h"
|
|
|
|
|
#import "OWSMessageManager.h"
|
|
|
|
|
#import "OWSQueues.h"
|
|
|
|
|
#import "OWSSignalServiceProtos.pb.h"
|
|
|
|
|
#import "TSDatabaseView.h"
|
|
|
|
|
#import "TSStorageManager.h"
|
|
|
|
@ -277,6 +278,16 @@ NSString *const OWSMessageContentJobFinderExtensionGroup = @"OWSBatchMessageProc
|
|
|
|
|
|
|
|
|
|
#pragma mark - instance methods
|
|
|
|
|
|
|
|
|
|
- (dispatch_queue_t)serialQueue
|
|
|
|
|
{
|
|
|
|
|
static dispatch_queue_t queue = nil;
|
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
|
queue = dispatch_queue_create("org.whispersystems.message.process", DISPATCH_QUEUE_SERIAL);
|
|
|
|
|
});
|
|
|
|
|
return queue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)enqueueEnvelopeData:(NSData *)envelopeData plaintextData:(NSData *_Nullable)plaintextData
|
|
|
|
|
{
|
|
|
|
|
OWSAssert(envelopeData);
|
|
|
|
@ -287,7 +298,7 @@ NSString *const OWSMessageContentJobFinderExtensionGroup = @"OWSBatchMessageProc
|
|
|
|
|
|
|
|
|
|
- (void)drainQueue
|
|
|
|
|
{
|
|
|
|
|
DispatchMainThreadSafe(^{
|
|
|
|
|
dispatch_async(self.serialQueue, ^{
|
|
|
|
|
if ([TSDatabaseView hasPendingViewRegistrations]) {
|
|
|
|
|
// We don't want to process incoming messages until database
|
|
|
|
|
// view registration is complete.
|
|
|
|
@ -305,7 +316,7 @@ NSString *const OWSMessageContentJobFinderExtensionGroup = @"OWSBatchMessageProc
|
|
|
|
|
|
|
|
|
|
- (void)drainQueueWorkStep
|
|
|
|
|
{
|
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
AssertOnDispatchQueue(self.serialQueue);
|
|
|
|
|
|
|
|
|
|
NSArray<OWSMessageContentJob *> *jobs = [self.finder nextJobsForBatchSize:kIncomingMessageBatchSize];
|
|
|
|
|
OWSAssert(jobs);
|
|
|
|
@ -315,9 +326,8 @@ NSString *const OWSMessageContentJobFinderExtensionGroup = @"OWSBatchMessageProc
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[self processJobs:jobs
|
|
|
|
|
completion:^{
|
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
|
[self processJobs:jobs];
|
|
|
|
|
|
|
|
|
|
[self.finder removeJobsWithIds:jobs.uniqueIds];
|
|
|
|
|
|
|
|
|
|
DDLogVerbose(@"%@ completed %zd jobs. %zd jobs left.",
|
|
|
|
@ -329,17 +339,15 @@ NSString *const OWSMessageContentJobFinderExtensionGroup = @"OWSBatchMessageProc
|
|
|
|
|
// This delay won't affect the first message to arrive when this queue is idle,
|
|
|
|
|
// so by definition we're receiving more than one message and can benefit from
|
|
|
|
|
// batching.
|
|
|
|
|
dispatch_after(
|
|
|
|
|
dispatch_time(DISPATCH_TIME_NOW, (int64_t)0.1f * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
|
|
|
|
|
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), self.serialQueue, ^{
|
|
|
|
|
[self drainQueueWorkStep];
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)processJobs:(NSArray<OWSMessageContentJob *> *)jobs completion:(void (^)())completion
|
|
|
|
|
- (void)processJobs:(NSArray<OWSMessageContentJob *> *)jobs
|
|
|
|
|
{
|
|
|
|
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
|
|
|
|
AssertOnDispatchQueue(self.serialQueue);
|
|
|
|
|
|
|
|
|
|
[self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
|
|
|
|
for (OWSMessageContentJob *job in jobs) {
|
|
|
|
|
[self.messagesManager processEnvelope:job.envelopeProto
|
|
|
|
@ -347,8 +355,6 @@ NSString *const OWSMessageContentJobFinderExtensionGroup = @"OWSBatchMessageProc
|
|
|
|
|
transaction:transaction];
|
|
|
|
|
}
|
|
|
|
|
}];
|
|
|
|
|
completion();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark Logging
|
|
|
|
|