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