aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/objective-c/GRPCClient/private/GRPCHost.m
diff options
context:
space:
mode:
authorGravatar Jorge Canizales <jcanizales@google.com>2015-08-05 17:22:53 -0700
committerGravatar Jorge Canizales <jcanizales@google.com>2015-08-05 17:22:53 -0700
commit82fb883bec8eef4f396723d259ceea06fa2771ea (patch)
treeca350d79b2ea2490d0d0a70a72ace2421f73e49c /src/objective-c/GRPCClient/private/GRPCHost.m
parentcceeb515927d72e3cfb24ecce4af89a9eed3b42b (diff)
Make GRPCHost cache thread-safe.
Diffstat (limited to 'src/objective-c/GRPCClient/private/GRPCHost.m')
-rw-r--r--src/objective-c/GRPCClient/private/GRPCHost.m26
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 {