diff --git a/src/objective-c/GRPCClient/private/GRPCChannel.h b/src/objective-c/GRPCClient/private/GRPCChannel.h index 7a40638dc33..5f5fae04135 100644 --- a/src/objective-c/GRPCClient/private/GRPCChannel.h +++ b/src/objective-c/GRPCClient/private/GRPCChannel.h @@ -51,10 +51,20 @@ struct grpc_channel_credentials; completionQueue:(nonnull GRPCCompletionQueue *)queue callOptions:(nonnull GRPCCallOptions *)callOptions; -- (void)unmanagedCallRef; +/** + * Increase the refcount of the channel. If the channel was timed to be destroyed, cancel the timer. + */ +- (void)ref; -- (void)unmanagedCallUnref; +/** + * Decrease the refcount of the channel. If the refcount of the channel decrease to 0, start a timer + * to destroy the channel + */ +- (void)unref; +/** + * Force the channel to be disconnected and destroyed immediately. + */ - (void)disconnect; // TODO (mxyan): deprecate with GRPCCall:closeOpenConnections diff --git a/src/objective-c/GRPCClient/private/GRPCChannel.m b/src/objective-c/GRPCClient/private/GRPCChannel.m index 63bc267a762..3e4db837658 100644 --- a/src/objective-c/GRPCClient/private/GRPCChannel.m +++ b/src/objective-c/GRPCClient/private/GRPCChannel.m @@ -180,7 +180,7 @@ NSTimeInterval kChannelDestroyDelay = 30; return call; } -- (void)unmanagedCallRef { +- (void)ref { dispatch_async(_dispatchQueue, ^{ if (self->_unmanagedChannel) { [self->_channelRef refChannel]; @@ -188,7 +188,7 @@ NSTimeInterval kChannelDestroyDelay = 30; }); } -- (void)unmanagedCallUnref { +- (void)unref { dispatch_async(_dispatchQueue, ^{ if (self->_unmanagedChannel) { [self->_channelRef unrefChannel]; diff --git a/src/objective-c/GRPCClient/private/GRPCChannelPool.m b/src/objective-c/GRPCClient/private/GRPCChannelPool.m index 4908b82feac..8e0f6976cf9 100644 --- a/src/objective-c/GRPCClient/private/GRPCChannelPool.m +++ b/src/objective-c/GRPCClient/private/GRPCChannelPool.m @@ -191,7 +191,7 @@ extern const char *kCFStreamVarName; @synchronized(self) { if ([_channelPool objectForKey:configuration]) { channel = _channelPool[configuration]; - [channel unmanagedCallRef]; + [channel ref]; } else { channel = [GRPCChannel createChannelWithConfiguration:configuration]; if (channel != nil) { diff --git a/src/objective-c/GRPCClient/private/GRPCWrappedCall.m b/src/objective-c/GRPCClient/private/GRPCWrappedCall.m index a7c50a17519..4d5257aca79 100644 --- a/src/objective-c/GRPCClient/private/GRPCWrappedCall.m +++ b/src/objective-c/GRPCClient/private/GRPCWrappedCall.m @@ -311,7 +311,7 @@ - (void)dealloc { grpc_call_unref(_call); - [_channel unmanagedCallUnref]; + [_channel unref]; _channel = nil; } diff --git a/src/objective-c/tests/ChannelTests/ChannelPoolTest.m b/src/objective-c/tests/ChannelTests/ChannelPoolTest.m index f4a9fb4a2c4..5c3f0edba0b 100644 --- a/src/objective-c/tests/ChannelTests/ChannelPoolTest.m +++ b/src/objective-c/tests/ChannelTests/ChannelPoolTest.m @@ -74,7 +74,7 @@ extern NSTimeInterval kChannelDestroyDelay; [[GRPCChannelConfiguration alloc] initWithHost:kDummyHost callOptions:options1]; GRPCChannelPool *pool = [[GRPCChannelPool alloc] init]; GRPCChannel *channel1 = [pool channelWithConfiguration:config1]; - [channel1 unmanagedCallUnref]; + [channel1 unref]; sleep(1); GRPCChannel *channel2 = [pool channelWithConfiguration:config1]; XCTAssertEqual(channel1, channel2);