Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

package org.springframework.grpc.client;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.DisposableBean;

Expand Down Expand Up @@ -50,6 +52,8 @@ public class DefaultGrpcChannelFactory implements GrpcChannelFactory, Disposable

private VirtualTargets targets = VirtualTargets.DEFAULT;

private Duration shutdownGracePeriod;

public DefaultGrpcChannelFactory() {
this(List.of());
}
Expand All @@ -66,6 +70,10 @@ public void setCredentialsProvider(ChannelCredentialsProvider credentials) {
this.credentials = credentials;
}

public void setShutdownGracePeriod(Duration shutdownGracePeriod) {
this.shutdownGracePeriod = shutdownGracePeriod;
}

@Override
public ManagedChannelBuilder<?> createChannel(String authority) {
ManagedChannelBuilder<?> target = this.builders.computeIfAbsent(authority, path -> {
Expand Down Expand Up @@ -93,8 +101,20 @@ protected ManagedChannelBuilder<?> newChannel(String path, ChannelCredentials cr

@Override
public void destroy() {
long millis = this.shutdownGracePeriod.toMillis();

for (ManagedChannel channel : this.channels.values()) {
channel.shutdown();
try {
channel.shutdown();

if (!channel.awaitTermination(millis, TimeUnit.MILLISECONDS)) {
channel.shutdownNow();
}
}
catch (InterruptedException e) {
channel.shutdownNow();
Thread.currentThread().interrupt();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public DefaultGrpcChannelFactory defaultGrpcChannelFactory(final List<GrpcChanne
DefaultGrpcChannelFactory factory = new DefaultGrpcChannelFactory(configurers);
factory.setCredentialsProvider(credentials);
factory.setVirtualTargets(new NamedChannelVirtualTargets(channels));
// Take shutdownGracePeriod from the default channel since it is the same for all
// channels
factory.setShutdownGracePeriod(channels.getDefaultChannel().getShutdownGracePeriod());
return factory;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,35 @@ public void setIdleTimeout(final Duration idleTimeout) {

// --------------------------------------------------

/**
* Maximum time to wait for the client channels to gracefully shutdown. The
* default is 30 seconds.
*/
@DurationUnit(ChronoUnit.SECONDS)
private Duration shutdownGracePeriod = Duration.ofSeconds(30);

/**
* Gets the shutdown grace period.
* @return The shutdown grace period.
*
* @see #setShutdownGracePeriod(Duration)
*/
public Duration getShutdownGracePeriod() {
return this.shutdownGracePeriod;
}

/**
* The shutdown grace period.
* @param shutdownGracePeriod shutdown grace period will client force shutdown
* channels immediately
*
*/
public void setShutdownGracePeriod(final Duration shutdownGracePeriod) {
this.shutdownGracePeriod = shutdownGracePeriod;
}

// --------------------------------------------------

@DurationUnit(ChronoUnit.SECONDS)
private Duration keepAliveTime;

Expand Down Expand Up @@ -431,6 +460,9 @@ public void copyDefaultsFrom(final NamedChannel config) {
if (this.address == null) {
this.address = config.address;
}
if (this.shutdownGracePeriod == null) {
this.shutdownGracePeriod = config.shutdownGracePeriod;
}
if (this.defaultLoadBalancingPolicy == null) {
this.defaultLoadBalancingPolicy = config.defaultLoadBalancingPolicy;
}
Expand Down