From ec4e5cf212219432b7512a39186dd73bcebe7e51 Mon Sep 17 00:00:00 2001 From: murgatroid99 <mlumish@google.com> Date: Thu, 14 May 2015 11:34:40 -0700 Subject: [PATCH] Added channel caching by host string --- src/objective-c/GRPCClient/private/GRPCChannel.m | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/objective-c/GRPCClient/private/GRPCChannel.m b/src/objective-c/GRPCClient/private/GRPCChannel.m index 8b7055815d..e1aebdfe57 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 { -- GitLab