From c635e41c22fb939f781837b9705b8642c13af9ae Mon Sep 17 00:00:00 2001 From: Muxi Yan Date: Tue, 19 Jun 2018 13:44:57 -0700 Subject: Timeout and backoff --- src/objective-c/GRPCClient/GRPCCall+ChannelArg.h | 10 ++++++++++ src/objective-c/GRPCClient/GRPCCall+ChannelArg.m | 10 ++++++++++ src/objective-c/GRPCClient/private/GRPCHost.h | 4 ++++ src/objective-c/GRPCClient/private/GRPCHost.m | 10 ++++++++++ 4 files changed, 34 insertions(+) diff --git a/src/objective-c/GRPCClient/GRPCCall+ChannelArg.h b/src/objective-c/GRPCClient/GRPCCall+ChannelArg.h index 0b6855f6a4..803f19dedf 100644 --- a/src/objective-c/GRPCClient/GRPCCall+ChannelArg.h +++ b/src/objective-c/GRPCClient/GRPCCall+ChannelArg.h @@ -60,4 +60,14 @@ typedef NS_ENUM(NSInteger, GRPCCompressAlgorithm) { * immediately returned to the application layer. */ + (void)enableRetry:(BOOL)enabled forHost:(nonnull NSString *)host; +/** Set channel connection timeout and backoff parameters. All parameters are positive integers in + * milliseconds. Set a parameter to 0 to make gRPC use default value for that parameter. + * + * Refer to gRPC's doc at https://github.com/grpc/grpc/blob/master/doc/connection-backoff.md for the + * details of each parameter. */ ++ (void)setMinConnectTimeout:(unsigned int)timeout + initialBackoff:(unsigned int)initialBackoff + maxBackoff:(unsigned int)maxBackoff + forHost:(nonnull NSString *)host; + @end diff --git a/src/objective-c/GRPCClient/GRPCCall+ChannelArg.m b/src/objective-c/GRPCClient/GRPCCall+ChannelArg.m index 78bf3fd79f..0e631fb3ad 100644 --- a/src/objective-c/GRPCClient/GRPCCall+ChannelArg.m +++ b/src/objective-c/GRPCClient/GRPCCall+ChannelArg.m @@ -69,4 +69,14 @@ hostConfig.retryEnabled = enabled; } ++ (void)setMinConnectTimeout:(unsigned int)timeout + initialBackoff:(unsigned int)initialBackoff + maxBackoff:(unsigned int)maxBackoff + forHost:(nonnull NSString *)host { + GRPCHost *hostConfig = [GRPCHost hostWithAddress:host]; + hostConfig.minConnectTimeout = timeout; + hostConfig.initialConnectBackoff = initialBackoff; + hostConfig.maxConnectBackoff = maxBackoff; +} + @end diff --git a/src/objective-c/GRPCClient/private/GRPCHost.h b/src/objective-c/GRPCClient/private/GRPCHost.h index 9a5293d90b..291b07df37 100644 --- a/src/objective-c/GRPCClient/private/GRPCHost.h +++ b/src/objective-c/GRPCClient/private/GRPCHost.h @@ -40,6 +40,10 @@ struct grpc_channel_credentials; @property(nonatomic) id logContext; @property(nonatomic) BOOL retryEnabled; +@property(nonatomic) unsigned int minConnectTimeout; +@property(nonatomic) unsigned int initialConnectBackoff; +@property(nonatomic) unsigned int maxConnectBackoff; + /** The following properties should only be modified for testing: */ @property(nonatomic, getter=isSecure) BOOL secure; diff --git a/src/objective-c/GRPCClient/private/GRPCHost.m b/src/objective-c/GRPCClient/private/GRPCHost.m index 13c2baada5..2e9f9f243b 100644 --- a/src/objective-c/GRPCClient/private/GRPCHost.m +++ b/src/objective-c/GRPCClient/private/GRPCHost.m @@ -245,6 +245,16 @@ static NSMutableDictionary *kHostCache; args[@GRPC_ARG_ENABLE_RETRIES] = [NSNumber numberWithInt:0]; } + if (_minConnectTimeout > 0) { + args[@GRPC_ARG_MIN_RECONNECT_BACKOFF_MS] = [NSNumber numberWithInt:_minConnectTimeout]; + } + if (_initialConnectBackoff > 0) { + args[@GRPC_ARG_INITIAL_RECONNECT_BACKOFF_MS] = [NSNumber numberWithInt:_initialConnectBackoff]; + } + if (_maxConnectBackoff > 0) { + args[@GRPC_ARG_MAX_RECONNECT_BACKOFF_MS] = [NSNumber numberWithInt:_maxConnectBackoff]; + } + return args; } -- cgit v1.2.3