diff options
author | Jorge Canizales <jcanizales@google.com> | 2016-04-29 16:19:51 -0700 |
---|---|---|
committer | Jorge Canizales <jcanizales@google.com> | 2016-04-29 16:19:51 -0700 |
commit | 454432542a6cc6145621003ecd8303c82a4e2b7b (patch) | |
tree | 55adfc451c47c5256dcda8ded0632ec81ed1ebcd | |
parent | 514699c63b5470d90395b4511982d3dc3700cad3 (diff) |
Add a maybe-temporary way for apps to clear the channel cache
-rw-r--r-- | src/objective-c/GRPCClient/GRPCCall+ChannelArg.h | 5 | ||||
-rw-r--r-- | src/objective-c/GRPCClient/GRPCCall+ChannelArg.m | 4 | ||||
-rw-r--r-- | src/objective-c/GRPCClient/private/GRPCHost.h | 2 | ||||
-rw-r--r-- | src/objective-c/GRPCClient/private/GRPCHost.m | 21 |
4 files changed, 27 insertions, 5 deletions
diff --git a/src/objective-c/GRPCClient/GRPCCall+ChannelArg.h b/src/objective-c/GRPCClient/GRPCCall+ChannelArg.h index bd6b064f16..646bf43b54 100644 --- a/src/objective-c/GRPCClient/GRPCCall+ChannelArg.h +++ b/src/objective-c/GRPCClient/GRPCCall+ChannelArg.h @@ -32,6 +32,8 @@ */ #import "GRPCCall.h" +#include <AvailabilityMacros.h> + /** * Methods to configure GRPC channel options. */ @@ -43,4 +45,7 @@ */ + (void)setUserAgentPrefix:(NSString *)userAgentPrefix forHost:(NSString *)host; ++ (void)closeOpenConnections DEPRECATED_MSG_ATTRIBUTE("The API for this feature is experimental, " + "and might be removed or modified at any " + "time."); @end diff --git a/src/objective-c/GRPCClient/GRPCCall+ChannelArg.m b/src/objective-c/GRPCClient/GRPCCall+ChannelArg.m index 5f9932d86d..bcc3b91507 100644 --- a/src/objective-c/GRPCClient/GRPCCall+ChannelArg.m +++ b/src/objective-c/GRPCClient/GRPCCall+ChannelArg.m @@ -46,4 +46,8 @@ hostConfig.userAgentPrefix = userAgentPrefix; } ++ (void)closeOpenConnections { + [GRPCHost flushChannelCache]; +} + @end diff --git a/src/objective-c/GRPCClient/private/GRPCHost.h b/src/objective-c/GRPCClient/private/GRPCHost.h index 9220e2a33d..350c69bf8e 100644 --- a/src/objective-c/GRPCClient/private/GRPCHost.h +++ b/src/objective-c/GRPCClient/private/GRPCHost.h @@ -41,6 +41,8 @@ struct grpc_channel_credentials; @interface GRPCHost : NSObject ++ (void)flushChannelCache; + @property(nonatomic, readonly) NSString *address; @property(nonatomic, copy, nullable) NSString *userAgentPrefix; @property(nonatomic, nullable) struct grpc_channel_credentials *channelCreds; diff --git a/src/objective-c/GRPCClient/private/GRPCHost.m b/src/objective-c/GRPCClient/private/GRPCHost.m index 43166cbb52..0358cc6236 100644 --- a/src/objective-c/GRPCClient/private/GRPCHost.m +++ b/src/objective-c/GRPCClient/private/GRPCHost.m @@ -48,6 +48,8 @@ NS_ASSUME_NONNULL_BEGIN // templates/src/core/surface/version.c.template . #define GRPC_OBJC_VERSION_STRING @"0.13.0" +static NSMutableDictionary *kHostCache; + @implementation GRPCHost { // TODO(mlumish): Investigate whether caching channels with strong links is a good idea. GRPCChannel *_channel; @@ -79,13 +81,12 @@ NS_ASSUME_NONNULL_BEGIN } // Look up the GRPCHost in the cache. - static NSMutableDictionary *hostCache; static dispatch_once_t cacheInitialization; dispatch_once(&cacheInitialization, ^{ - hostCache = [NSMutableDictionary dictionary]; + kHostCache = [NSMutableDictionary dictionary]; }); - @synchronized(hostCache) { - GRPCHost *cachedHost = hostCache[address]; + @synchronized(kHostCache) { + GRPCHost *cachedHost = kHostCache[address]; if (cachedHost) { return cachedHost; } @@ -93,12 +94,22 @@ NS_ASSUME_NONNULL_BEGIN if ((self = [super init])) { _address = address; _secure = YES; - hostCache[address] = self; + kHostCache[address] = self; } } return self; } ++ (void)flushChannelCache { + @synchronized(kHostCache) { + [kHostCache enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, + GRPCHost * _Nonnull host, + BOOL * _Nonnull stop) { + [host disconnect]; + }]; + } +} + - (nullable grpc_call *)unmanagedCallWithPath:(NSString *)path completionQueue:(GRPCCompletionQueue *)queue { GRPCChannel *channel; |