Add a maybe-temporary way for apps to clear the channel cache

pull/6378/head
Jorge Canizales 9 years ago
parent 514699c63b
commit 454432542a
  1. 5
      src/objective-c/GRPCClient/GRPCCall+ChannelArg.h
  2. 4
      src/objective-c/GRPCClient/GRPCCall+ChannelArg.m
  3. 2
      src/objective-c/GRPCClient/private/GRPCHost.h
  4. 21
      src/objective-c/GRPCClient/private/GRPCHost.m

@ -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

@ -46,4 +46,8 @@
hostConfig.userAgentPrefix = userAgentPrefix;
}
+ (void)closeOpenConnections {
[GRPCHost flushChannelCache];
}
@end

@ -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;

@ -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;

Loading…
Cancel
Save