Skip to content
This repository was archived by the owner on Apr 22, 2025. It is now read-only.

Commit eef82e2

Browse files
FGJ-36 set sensible defaults for grpc
Unless otherwise specified, the grpc time/timeout properties will default to more realistic values for the orderers and peers Change-Id: I686ab93da926daa28109beeed97f6697e7660b2d Signed-off-by: andrew-coleman <andrew_coleman@uk.ibm.com>
1 parent 77546b5 commit eef82e2

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/main/java/org/hyperledger/fabric/sdk/HFClient.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,26 @@ public static HFClient createNewInstance() {
151151
return new HFClient();
152152
}
153153

154+
/**
155+
* Set sensible default grpc properties for newPeer and newOrderer. Only sets values
156+
* if they don't already exist.
157+
* @param props The properties object to apply defaults to
158+
*/
159+
private void setDefaultProperties(Properties props) {
160+
if (!props.containsKey("grpc.NettyChannelBuilderOption.keepAliveTime")) {
161+
props.put("grpc.NettyChannelBuilderOption.keepAliveTime", new Object[] {2L, TimeUnit.MINUTES});
162+
}
163+
if (!props.containsKey("grpc.NettyChannelBuilderOption.keepAliveTimeout")) {
164+
props.put("grpc.NettyChannelBuilderOption.keepAliveTimeout", new Object[] {20L, TimeUnit.SECONDS});
165+
}
166+
if (!props.containsKey("grpc.NettyChannelBuilderOption.keepAliveTime")) {
167+
props.put("grpc.NettyChannelBuilderOption.keepAliveTime", new Object[] {2L, TimeUnit.MINUTES});
168+
}
169+
if (!props.containsKey("grpc.NettyChannelBuilderOption.keepAliveWithoutCalls")) {
170+
props.put("grpc.NettyChannelBuilderOption.keepAliveWithoutCalls", new Object[] {true});
171+
}
172+
}
173+
154174
/**
155175
* Configures a channel based on information loaded from a Network Config file.
156176
* Note that it is up to the caller to initialize the returned channel.
@@ -394,6 +414,7 @@ public Channel deSerializeChannel(byte[] channelBytes)
394414

395415
public Peer newPeer(String name, String grpcURL, Properties properties) throws InvalidArgumentException {
396416
clientCheck();
417+
setDefaultProperties(properties);
397418
return Peer.createNewInstance(name, grpcURL, properties);
398419
}
399420

@@ -408,7 +429,7 @@ public Peer newPeer(String name, String grpcURL, Properties properties) throws I
408429

409430
public Peer newPeer(String name, String grpcURL) throws InvalidArgumentException {
410431
clientCheck();
411-
return Peer.createNewInstance(name, grpcURL, null);
432+
return Peer.createNewInstance(name, grpcURL, new Properties());
412433
}
413434

414435
/**
@@ -696,7 +717,7 @@ public User setUserContext(User userContext) throws InvalidArgumentException {
696717

697718
public Orderer newOrderer(String name, String grpcURL) throws InvalidArgumentException {
698719
clientCheck();
699-
return newOrderer(name, grpcURL, null);
720+
return newOrderer(name, grpcURL, new Properties());
700721
}
701722

702723
/**
@@ -745,6 +766,7 @@ public Orderer newOrderer(String name, String grpcURL) throws InvalidArgumentExc
745766

746767
public Orderer newOrderer(String name, String grpcURL, Properties properties) throws InvalidArgumentException {
747768
clientCheck();
769+
setDefaultProperties(properties);
748770
return Orderer.createNewInstance(name, grpcURL, properties);
749771
}
750772

src/test/java/org/hyperledger/fabric/sdk/NetworkConfigTest.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,15 @@ public void testLoadFromConfigFileYamlNOOverrides() throws Exception {
299299
Properties properties = peer.getProperties();
300300

301301
assertNotNull(properties);
302-
assertNull(properties.get("grpc.NettyChannelBuilderOption.keepAliveTime"));
303-
assertNull(properties.get("grpc.NettyChannelBuilderOption.keepAliveTimeout"));
304-
assertNull(properties.get("grpc.NettyChannelBuilderOption.keepAliveWithoutCalls"));
302+
// check for default properties
303+
Object[] o = (Object[]) properties.get("grpc.NettyChannelBuilderOption.keepAliveTime");
304+
assertEquals(o[0], 120000L);
305+
306+
o = (Object[]) properties.get("grpc.NettyChannelBuilderOption.keepAliveTimeout");
307+
assertEquals(o[0], 20000L);
305308

309+
o = (Object[]) properties.get("grpc.NettyChannelBuilderOption.keepAliveWithoutCalls");
310+
assertEquals(o[0], true);
306311
}
307312

308313
}

0 commit comments

Comments
 (0)