diff options
author | 2018-03-19 20:37:28 -0700 | |
---|---|---|
committer | 2018-03-26 11:50:10 -0700 | |
commit | 6d855c5f3c7518e2b26a97e464ebb9366da05993 (patch) | |
tree | 8e4957ce849ea4c7c711150510f7fbc7dc7e26b9 | |
parent | bc7e096dd38cd8fca1504986abd2025019004563 (diff) |
Allow gRPC ObjC user to set keepalive options
-rw-r--r-- | src/objective-c/GRPCClient/GRPCCall+ChannelArg.h | 7 | ||||
-rw-r--r-- | src/objective-c/GRPCClient/GRPCCall+ChannelArg.m | 8 | ||||
-rw-r--r-- | src/objective-c/GRPCClient/private/GRPCHost.h | 2 | ||||
-rw-r--r-- | src/objective-c/GRPCClient/private/GRPCHost.m | 5 | ||||
-rw-r--r-- | src/objective-c/tests/InteropTests.m | 37 |
5 files changed, 59 insertions, 0 deletions
diff --git a/src/objective-c/GRPCClient/GRPCCall+ChannelArg.h b/src/objective-c/GRPCClient/GRPCCall+ChannelArg.h index 18d4597098..0c3f37de81 100644 --- a/src/objective-c/GRPCClient/GRPCCall+ChannelArg.h +++ b/src/objective-c/GRPCClient/GRPCCall+ChannelArg.h @@ -46,4 +46,11 @@ typedef NS_ENUM(NSInteger, GRPCCompressAlgorithm) { + (void)setDefaultCompressMethod:(GRPCCompressAlgorithm)algorithm forhost:(nonnull NSString *)host; +/** Configure keepalive timeout parameters. A client ping after \a interval ms to check if the + * transport is still alive. After waiting for \a timeout ms, if the client does not receive the + * ping ack, it closes the transport. */ ++ (void)setKeepaliveWithInterval:(int)interval + timeout:(int)timeout + forHost:(nonnull NSString *)host; + @end diff --git a/src/objective-c/GRPCClient/GRPCCall+ChannelArg.m b/src/objective-c/GRPCClient/GRPCCall+ChannelArg.m index 805e54b890..217d0e9220 100644 --- a/src/objective-c/GRPCClient/GRPCCall+ChannelArg.m +++ b/src/objective-c/GRPCClient/GRPCCall+ChannelArg.m @@ -57,4 +57,12 @@ } } ++ (void)setKeepaliveWithInterval:(int)interval + timeout:(int)timeout + forHost:(nonnull NSString *)host { + GRPCHost *hostConfig = [GRPCHost hostWithAddress:host]; + hostConfig.keepaliveInterval = interval; + hostConfig.keepaliveTimeout = timeout; +} + @end diff --git a/src/objective-c/GRPCClient/private/GRPCHost.h b/src/objective-c/GRPCClient/private/GRPCHost.h index 0215db8531..a1d2e5fcdd 100644 --- a/src/objective-c/GRPCClient/private/GRPCHost.h +++ b/src/objective-c/GRPCClient/private/GRPCHost.h @@ -35,6 +35,8 @@ struct grpc_channel_credentials; @property(nonatomic, copy, nullable) NSString *userAgentPrefix; @property(nonatomic, nullable) struct grpc_channel_credentials *channelCreds; @property(nonatomic) grpc_compression_algorithm compressAlgorithm; +@property(nonatomic) int keepaliveInterval; +@property(nonatomic) int keepaliveTimeout; /** The following properties should only be modified for testing: */ diff --git a/src/objective-c/GRPCClient/private/GRPCHost.m b/src/objective-c/GRPCClient/private/GRPCHost.m index 8568e334dd..c282fbd194 100644 --- a/src/objective-c/GRPCClient/private/GRPCHost.m +++ b/src/objective-c/GRPCClient/private/GRPCHost.m @@ -216,6 +216,11 @@ static NSMutableDictionary *kHostCache; [NSNumber numberWithInt:_compressAlgorithm]; } + if (_keepaliveInterval != 0) { + args[@GRPC_ARG_KEEPALIVE_TIME_MS] = [NSNumber numberWithInt:_keepaliveInterval]; + args[@GRPC_ARG_KEEPALIVE_TIMEOUT_MS] = [NSNumber numberWithInt:_keepaliveTimeout]; + } + id logConfig = [GRPCCall logConfig]; if (logConfig != nil) { args[@GRPC_ARG_MOBILE_LOG_CONFIG] = logConfig; diff --git a/src/objective-c/tests/InteropTests.m b/src/objective-c/tests/InteropTests.m index dfa874adab..66fea22c83 100644 --- a/src/objective-c/tests/InteropTests.m +++ b/src/objective-c/tests/InteropTests.m @@ -486,4 +486,41 @@ BOOL isRemoteInteropTest(NSString *host) { [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil]; } +- (void)testKeepalive { + XCTAssertNotNil(self.class.host); + __weak XCTestExpectation *expectation = [self expectationWithDescription:@"PingPong"]; + + [GRPCCall setKeepaliveWithInterval:1500 timeout:1 forHost:self.class.host]; + + NSArray *requests = @[@27182, @8]; + NSArray *responses = @[@31415, @9]; + + GRXBufferedPipe *requestsBuffer = [[GRXBufferedPipe alloc] init]; + + __block int index = 0; + + id request = [RMTStreamingOutputCallRequest messageWithPayloadSize:requests[index] + requestedResponseSize:responses[index]]; + [requestsBuffer writeValue:request]; + + [_service fullDuplexCallWithRequestsWriter:requestsBuffer + eventHandler:^(BOOL done, + RMTStreamingOutputCallResponse *response, + NSError *error) { + if (index == 0) { + XCTAssertNil(error, @"Finished with unexpected error: %@", error); + XCTAssertTrue(response, @"Event handler called without an event."); + XCTAssertFalse(done); + index++; + } else { + // Keepalive should kick after 1s elapsed and fails the call. + XCTAssertNotNil(error); + XCTAssertTrue(done); + [expectation fulfill]; + } + }]; + + [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil]; +} + @end |