aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/objective-c/GRPCClient/private/GRPCChannel.m
diff options
context:
space:
mode:
authorGravatar Muxi Yan <mxyan@google.com>2018-11-01 11:43:09 -0700
committerGravatar Muxi Yan <mxyan@google.com>2018-11-01 11:43:09 -0700
commit601475b57105f0aff3e96043e6399b621cfc5b84 (patch)
tree60471f663bd78ba78bcbeaec5df3a9d26d8ce44d /src/objective-c/GRPCClient/private/GRPCChannel.m
parentdc4fc1ce38f9060c10ae639173d106c5a7d2bece (diff)
Easier check if timer is canceled
Diffstat (limited to 'src/objective-c/GRPCClient/private/GRPCChannel.m')
-rw-r--r--src/objective-c/GRPCClient/private/GRPCChannel.m16
1 files changed, 11 insertions, 5 deletions
diff --git a/src/objective-c/GRPCClient/private/GRPCChannel.m b/src/objective-c/GRPCClient/private/GRPCChannel.m
index 377900fd9e..1d7fb421e5 100644
--- a/src/objective-c/GRPCClient/private/GRPCChannel.m
+++ b/src/objective-c/GRPCClient/private/GRPCChannel.m
@@ -66,6 +66,12 @@ static GRPCChannelPool *gChannelPool;
BOOL _disconnected;
dispatch_queue_t _dispatchQueue;
dispatch_queue_t _timerQueue;
+
+ /**
+ * Date and time when last timer is scheduled. When a timer is fired, if
+ * _lastDispatch + _destroyDelay < now, it can be determined that another timer is scheduled after
+ * schedule of the current timer, hence the current one should be discarded.
+ */
NSDate *_lastDispatch;
}
@@ -107,11 +113,12 @@ static GRPCChannelPool *gChannelPool;
if (!self->_disconnected) {
self->_refCount--;
if (self->_refCount == 0) {
- self->_lastDispatch = [NSDate date];
+ NSDate *now = [NSDate date];
+ self->_lastDispatch = now;
dispatch_time_t delay =
dispatch_time(DISPATCH_TIME_NOW, (int64_t)self->_destroyDelay * NSEC_PER_SEC);
dispatch_after(delay, self->_timerQueue, ^{
- [self timerFire];
+ [self timerFireWithScheduleDate:now];
});
}
}
@@ -129,10 +136,9 @@ static GRPCChannelPool *gChannelPool;
});
}
-- (void)timerFire {
+- (void)timerFireWithScheduleDate:(NSDate *)scheduleDate {
dispatch_async(_dispatchQueue, ^{
- if (self->_disconnected || self->_lastDispatch == nil ||
- -[self->_lastDispatch timeIntervalSinceNow] < -self->_destroyDelay) {
+ if (self->_disconnected || self->_lastDispatch != scheduleDate) {
return;
}
self->_lastDispatch = nil;