Allow gRPC ObjC user to set keepalive options

pull/14781/head
Muxi Yan 7 years ago
parent bc7e096dd3
commit 6d855c5f3c
  1. 7
      src/objective-c/GRPCClient/GRPCCall+ChannelArg.h
  2. 8
      src/objective-c/GRPCClient/GRPCCall+ChannelArg.m
  3. 2
      src/objective-c/GRPCClient/private/GRPCHost.h
  4. 5
      src/objective-c/GRPCClient/private/GRPCHost.m
  5. 37
      src/objective-c/tests/InteropTests.m

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

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

@ -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: */

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

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

Loading…
Cancel
Save