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

Commit c642946

Browse files
force immediate shutdown of the network connection (#153)
Configuration option on Gateway builder to enable or disable force shutdown of the underlying Channels when closing the Gateway. Signed-off-by: yoshiteru imamura <[email protected]>
1 parent ec11bf5 commit c642946

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

src/main/java/org/hyperledger/fabric/gateway/Gateway.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,14 @@ interface Builder {
183183
*/
184184
Builder discovery(boolean enabled);
185185

186+
/**
187+
* <em>Optional</em> - Enable or disable force immediate shutdown of the network connection.
188+
* forceClose is enabled by default.
189+
* @param enabled - true to enable force immediate shutdown.
190+
* @return The builder instance, allowing multiple configuration options to be chained.
191+
*/
192+
Builder forceClose(boolean enabled);
193+
186194
/**
187195
* Connects to the gateway using the specified options.
188196
* @return The connected {@link Gateway} object.

src/main/java/org/hyperledger/fabric/gateway/impl/GatewayImpl.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public final class GatewayImpl implements Gateway {
6262
private final TimePeriod commitTimeout;
6363
private final QueryHandlerFactory queryHandlerFactory;
6464
private final boolean discovery;
65+
private final boolean forceClose;
6566

6667
public static final class Builder implements Gateway.Builder {
6768
private CommitHandlerFactory commitHandlerFactory = DefaultCommitHandlers.PREFER_MSPID_SCOPE_ALLFORTX;
@@ -71,6 +72,7 @@ public static final class Builder implements Gateway.Builder {
7172
private Identity identity = null;
7273
private HFClient client;
7374
private boolean discovery = false;
75+
private boolean forceClose = true;
7476

7577
private static final class ExposedByteArrayOutputStream extends ByteArrayOutputStream {
7678
public byte[] getInternalBuffer() {
@@ -152,6 +154,12 @@ public Builder discovery(final boolean enabled) {
152154
return this;
153155
}
154156

157+
@Override
158+
public Builder forceClose(final boolean enabled) {
159+
this.forceClose = enabled;
160+
return this;
161+
}
162+
155163
public Builder client(final HFClient client) {
156164
this.client = client;
157165
return this;
@@ -168,6 +176,7 @@ private GatewayImpl(final Builder builder) {
168176
this.commitTimeout = builder.commitTimeout;
169177
this.queryHandlerFactory = builder.queryHandlerFactory;
170178
this.discovery = builder.discovery;
179+
this.forceClose = builder.forceClose;
171180

172181
if (builder.client != null) {
173182
// Only for testing!
@@ -201,6 +210,7 @@ private GatewayImpl(final GatewayImpl that) {
201210
this.commitTimeout = that.commitTimeout;
202211
this.queryHandlerFactory = that.queryHandlerFactory;
203212
this.discovery = that.discovery;
213+
this.forceClose = that.forceClose;
204214
this.networkConfig = that.networkConfig;
205215
this.identity = that.identity;
206216

@@ -281,6 +291,10 @@ public boolean isDiscoveryEnabled() {
281291
return discovery;
282292
}
283293

294+
public boolean isForceClose() {
295+
return forceClose;
296+
}
297+
284298
public GatewayImpl newInstance() {
285299
return new GatewayImpl(this);
286300
}

src/main/java/org/hyperledger/fabric/gateway/impl/NetworkImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public void close() {
179179
orderedBlockSource.close();
180180
channelBlockSource.close();
181181

182-
channel.shutdown(false);
182+
channel.shutdown(gateway.isForceClose());
183183
}
184184

185185
@Override

src/test/java/org/hyperledger/fabric/gateway/impl/GatewayTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,37 @@ public void testCloseGatewayClosesNetworks() {
8282
assertThat(channel.isShutdown()).isTrue();
8383
}
8484

85+
@Test
86+
public void testCloseGatewayForceClosesNetworks() {
87+
HFClient mockClient = testUtils.newMockClient();
88+
Channel mockChannel = testUtils.newMockChannel("CHANNEL");
89+
Mockito.when(mockClient.getChannel("CHANNEL")).thenReturn(mockChannel);
90+
91+
builder.client(mockClient);
92+
93+
try (Gateway gateway = builder.connect()) {
94+
gateway.getNetwork("CHANNEL");
95+
}
96+
97+
Mockito.verify(mockChannel).shutdown(true);
98+
}
99+
100+
@Test
101+
public void testCloseGatewayWithForceCloseDisabledClosesNetworks() {
102+
HFClient mockClient = testUtils.newMockClient();
103+
Channel mockChannel = testUtils.newMockChannel("CHANNEL");
104+
Mockito.when(mockClient.getChannel("CHANNEL")).thenReturn(mockChannel);
105+
106+
builder.client(mockClient);
107+
builder.forceClose(false);
108+
109+
try (Gateway gateway = builder.connect()) {
110+
gateway.getNetwork("CHANNEL");
111+
}
112+
113+
Mockito.verify(mockChannel).shutdown(false);
114+
}
115+
85116
@Test
86117
public void testNewInstanceHasSameCryptoSuite() {
87118
final HFClient clientSpy;

0 commit comments

Comments
 (0)