Skip to content

Commit ca272ae

Browse files
committed
Support for shutdown grace period in netty clients
1 parent 50e2db6 commit ca272ae

3 files changed

Lines changed: 56 additions & 1 deletion

File tree

spring-grpc-core/src/main/java/org/springframework/grpc/client/DefaultGrpcChannelFactory.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616

1717
package org.springframework.grpc.client;
1818

19+
import java.time.Duration;
1920
import java.util.ArrayList;
2021
import java.util.List;
2122
import java.util.Map;
2223
import java.util.concurrent.ConcurrentHashMap;
24+
import java.util.concurrent.TimeUnit;
2325

2426
import org.springframework.beans.factory.DisposableBean;
2527

@@ -50,6 +52,8 @@ public class DefaultGrpcChannelFactory implements GrpcChannelFactory, Disposable
5052

5153
private VirtualTargets targets = VirtualTargets.DEFAULT;
5254

55+
private Duration shutdownGracePeriod;
56+
5357
public DefaultGrpcChannelFactory() {
5458
this(List.of());
5559
}
@@ -66,6 +70,10 @@ public void setCredentialsProvider(ChannelCredentialsProvider credentials) {
6670
this.credentials = credentials;
6771
}
6872

73+
public void setShutdownGracePeriod(Duration shutdownGracePeriod) {
74+
this.shutdownGracePeriod = shutdownGracePeriod;
75+
}
76+
6977
@Override
7078
public ManagedChannelBuilder<?> createChannel(String authority) {
7179
ManagedChannelBuilder<?> target = this.builders.computeIfAbsent(authority, path -> {
@@ -93,8 +101,20 @@ protected ManagedChannelBuilder<?> newChannel(String path, ChannelCredentials cr
93101

94102
@Override
95103
public void destroy() {
104+
long millis = this.shutdownGracePeriod.toMillis();
105+
96106
for (ManagedChannel channel : this.channels.values()) {
97-
channel.shutdown();
107+
try {
108+
channel.shutdown();
109+
110+
if (!channel.awaitTermination(millis, TimeUnit.MILLISECONDS)) {
111+
channel.shutdownNow();
112+
}
113+
}
114+
catch (InterruptedException e) {
115+
channel.shutdownNow();
116+
Thread.currentThread().interrupt();
117+
}
98118
}
99119
}
100120

spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/client/GrpcClientAutoConfiguration.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public DefaultGrpcChannelFactory defaultGrpcChannelFactory(final List<GrpcChanne
4848
DefaultGrpcChannelFactory factory = new DefaultGrpcChannelFactory(configurers);
4949
factory.setCredentialsProvider(credentials);
5050
factory.setVirtualTargets(new NamedChannelVirtualTargets(channels));
51+
// Take shutdownGracePeriod from the default channel since it is the same for all
52+
// channels
53+
factory.setShutdownGracePeriod(channels.getDefaultChannel().getShutdownGracePeriod());
5154
return factory;
5255
}
5356

spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/client/GrpcClientProperties.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,35 @@ public void setIdleTimeout(final Duration idleTimeout) {
221221

222222
// --------------------------------------------------
223223

224+
/**
225+
* Maximum time to wait for the client channels to gracefully shutdown. The
226+
* default is 30 seconds.
227+
*/
228+
@DurationUnit(ChronoUnit.SECONDS)
229+
private Duration shutdownGracePeriod = Duration.ofSeconds(30);
230+
231+
/**
232+
* Gets the shutdown grace period.
233+
* @return The shutdown grace period.
234+
*
235+
* @see #setShutdownGracePeriod(Duration)
236+
*/
237+
public Duration getShutdownGracePeriod() {
238+
return this.shutdownGracePeriod;
239+
}
240+
241+
/**
242+
* The shutdown grace period.
243+
* @param shutdownGracePeriod shutdown grace period will client force shutdown
244+
* channels immediately
245+
*
246+
*/
247+
public void setShutdownGracePeriod(final Duration shutdownGracePeriod) {
248+
this.shutdownGracePeriod = shutdownGracePeriod;
249+
}
250+
251+
// --------------------------------------------------
252+
224253
@DurationUnit(ChronoUnit.SECONDS)
225254
private Duration keepAliveTime;
226255

@@ -431,6 +460,9 @@ public void copyDefaultsFrom(final NamedChannel config) {
431460
if (this.address == null) {
432461
this.address = config.address;
433462
}
463+
if (this.shutdownGracePeriod == null) {
464+
this.shutdownGracePeriod = config.shutdownGracePeriod;
465+
}
434466
if (this.defaultLoadBalancingPolicy == null) {
435467
this.defaultLoadBalancingPolicy = config.defaultLoadBalancingPolicy;
436468
}

0 commit comments

Comments
 (0)