diff options
author | Jorge Canizales <jcanizales@google.com> | 2015-08-05 17:22:53 -0700 |
---|---|---|
committer | Jorge Canizales <jcanizales@google.com> | 2015-08-05 17:22:53 -0700 |
commit | 82fb883bec8eef4f396723d259ceea06fa2771ea (patch) | |
tree | ca350d79b2ea2490d0d0a70a72ace2421f73e49c /src/objective-c | |
parent | cceeb515927d72e3cfb24ecce4af89a9eed3b42b (diff) |
Make GRPCHost cache thread-safe.
Diffstat (limited to 'src/objective-c')
-rw-r--r-- | src/objective-c/GRPCClient/private/GRPCHost.m | 26 |
1 files changed, 14 insertions, 12 deletions
diff --git a/src/objective-c/GRPCClient/private/GRPCHost.m b/src/objective-c/GRPCClient/private/GRPCHost.m index 7355139fef..452283c76e 100644 --- a/src/objective-c/GRPCClient/private/GRPCHost.m +++ b/src/objective-c/GRPCClient/private/GRPCHost.m @@ -76,25 +76,27 @@ address = [@[hostURL.host, port] componentsJoinedByString:@":"]; // Look up the GRPCHost in the cache. - // TODO(jcanizales): Make this cache thread-safe. static NSMutableDictionary *hostCache; static dispatch_once_t cacheInitialization; dispatch_once(&cacheInitialization, ^{ hostCache = [NSMutableDictionary dictionary]; }); - if (hostCache[address]) { - // We could verify here that the cached host uses the same protocol that we're expecting. But - // creating non-SSL channels by adding "http://" to the address is going away (to make the use - // of insecure channels less subtle), so it's not worth it now. - return hostCache[address]; - } + @synchronized(hostCache) { + GRPCHost *cachedHost = hostCache[address]; + if (cachedHost) { + // We could verify here that the cached host uses the same protocol that we're expecting. But + // creating non-SSL channels by adding "http://" to the address is going away (to make the use + // of insecure channels less subtle), so it's not worth it now. + return cachedHost; + } - if ((self = [super init])) { - _address = address; - _secure = [scheme isEqualToString:@"https"]; - hostCache[address] = self; + if ((self = [super init])) { + _address = address; + _secure = [scheme isEqualToString:@"https"]; + hostCache[address] = self; + } + return self; } - return self; } - (grpc_call *)unmanagedCallWithPath:(NSString *)path completionQueue:(GRPCCompletionQueue *)queue { |