Skip to content

Commit

Permalink
change the way I shutdown the channel
Browse files Browse the repository at this point in the history
  • Loading branch information
namtzigla committed Jan 30, 2025
1 parent b5b7362 commit 3532a51
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -77,8 +78,7 @@ public void onApplicationEvent(ConfigEvent event) {
log.debug("onApplicationEvent, config changed {}", event);
this.event = event;
if ((channel != null) && !channel.isShutdown() && !channel.isTerminated()) {
channel.shutdown();
channel = null;
this.shutdown();
}
start();
}
Expand All @@ -95,6 +95,24 @@ protected ConfigEvent getConfig() throws UnconfiguredException {
return event;
}

public void shutdown() {
if (channel != null) {
channel.shutdown();
try {
channel.awaitTermination(30, TimeUnit.SECONDS);
} catch (InterruptedException e) {
log.error("Channel shutdown interrupted", e);
channel.shutdownNow();
try {
channel.awaitTermination(30, TimeUnit.SECONDS);
} catch (InterruptedException ex) {
log.error("2nd Attempt to channel shutdown interrupted", ex);
}
}
channel = null;
}
}

/**
* Gets or creates a gRPC channel for communication with the Gate service.
* @return A ManagedChannel instance.
Expand All @@ -104,6 +122,7 @@ public ManagedChannel getChannel() throws UnconfiguredException {
if ((channel != null) && !channel.isShutdown() && !channel.isTerminated()) {
return channel;
}
shutdown();
try {
log.debug("creating a new channel");
var channelCredentials = TlsChannelCredentials.newBuilder()
Expand All @@ -114,6 +133,8 @@ public ManagedChannel getChannel() throws UnconfiguredException {
.build();
channel = Grpc.newChannelBuilderForAddress(
getConfig().getApiHostname(), getConfig().getApiPort(), channelCredentials)
.keepAliveTime(1, TimeUnit.SECONDS)
.keepAliveTimeout(10, TimeUnit.SECONDS)
.overrideAuthority("exile-proxy")
// TODO: add service configuration for retry
// .defaultServiceConfig(null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,14 @@ public void onNext(Service.JobStreamResponse job) {
@Override
public void onError(Throwable throwable) {
log.debug("GateClientJobStream error {}", throwable.getMessage());
if (channel != null) channel.shutdownNow();
if (channel != null) this.shutdown();
eventStream();
}

@Override
public void onCompleted() {
log.debug("GateClientJobStream completed");
channel.shutdownNow();
this.shutdown();
// eventStream();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ public void onNext(Service.ResultsStreamResponse response) {
@Override
public void onError(Throwable throwable) {
log.debug("GateClientResponseStream onError {}", throwable);
if (channel != null) channel.shutdownNow();
if (channel != null) this.shutdown();
responseStream();
}

@Override
public void onCompleted() {
log.debug("GateClientResponseStream onCompleted");
if (channel != null) channel.shutdownNow();
if (channel != null) this.shutdown();
responseStream();
}

Expand Down

0 comments on commit 3532a51

Please sign in to comment.