diff --git a/src/objective-c/GRPCClient/private/GRPCChannel.m b/src/objective-c/GRPCClient/private/GRPCChannel.m index 8b7055815d3e7423ab8f45cf0bf9fd99a3d063f0..36f4c0aa5e08b42c053bc7de89b5e664b9bd4390 100644 --- a/src/objective-c/GRPCClient/private/GRPCChannel.m +++ b/src/objective-c/GRPCClient/private/GRPCChannel.m @@ -41,8 +41,18 @@ @implementation GRPCChannel + (instancetype)channelToHost:(NSString *)host { - // TODO(jcanizales): Reuse channels. - return [[self alloc] initWithHost:host]; + // TODO(mlumish): Investigate whether a cache with strong links is a good idea + static NSMutableDictionary *channelCache; + static dispatch_once_t cacheInitialization; + dispatch_once(&cacheInitialization, ^{ + channelCache = [NSMutableDictionary dictionary]; + }); + GRPCChannel *channel = channelCache[host]; + if (!channel) { + channel = [[self alloc] initWithHost:host]; + channelCache[host] = channel; + } + return channel; } - (instancetype)init {