Make GrpcStubs channel closeable by calling bot

Each bot has its own gRPC channel and set of service stubs,
and a single bot's channel & stubs last the bot's lifetime.
However, RegtestTradePaymentSimulator instance is a bot too, with it's
own channel & stubs, which should be closed at the end of each run().
This commit is contained in:
ghubstan 2022-06-25 14:11:06 -03:00
parent bf876a9771
commit aee939a887
No known key found for this signature in database
GPG Key ID: E35592D6800A861E
2 changed files with 19 additions and 10 deletions

View File

@ -19,6 +19,7 @@ package bisq.bots;
import bisq.proto.grpc.*;
import io.grpc.CallCredentials;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import lombok.extern.slf4j.Slf4j;
@ -40,19 +41,13 @@ final class GrpcStubs {
public final TradesGrpc.TradesBlockingStub tradesService;
public final WalletsGrpc.WalletsBlockingStub walletsService;
private final ManagedChannel channel;
public GrpcStubs(String apiHost, int apiPort, String apiPassword) {
CallCredentials credentials = new PasswordCallCredentials(apiPassword);
var channel = ManagedChannelBuilder.forAddress(apiHost, apiPort).usePlaintext().build();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
log.info("Shutting down bot's grpc channel.");
channel.shutdown().awaitTermination(1, SECONDS);
log.info("Bot channel shutdown complete.");
} catch (InterruptedException ex) {
throw new IllegalStateException(ex);
}
}));
this.channel = ManagedChannelBuilder.forAddress(apiHost, apiPort).usePlaintext().build();
Runtime.getRuntime().addShutdownHook(new Thread(this::close));
this.disputeAgentsService = DisputeAgentsGrpc.newBlockingStub(channel).withCallCredentials(credentials);
this.helpService = HelpGrpc.newBlockingStub(channel).withCallCredentials(credentials);
@ -64,4 +59,16 @@ final class GrpcStubs {
this.tradesService = TradesGrpc.newBlockingStub(channel).withCallCredentials(credentials);
this.walletsService = WalletsGrpc.newBlockingStub(channel).withCallCredentials(credentials);
}
public void close() {
try {
if (!channel.isShutdown()) {
log.info("Shutting down bot's grpc channel.");
channel.shutdown().awaitTermination(1, SECONDS);
log.info("Bot channel shutdown complete.");
}
} catch (InterruptedException ex) {
throw new IllegalStateException(ex);
}
}
}

View File

@ -116,6 +116,8 @@ public class RegtestTradePaymentSimulator extends AbstractBot {
sleep(pollingInterval);
log.info("Trade is completed, printing all closed trades and exiting {}.", this.getClass().getSimpleName());
printTradesSummary(CLOSED);
log.info("Closing {}'s gRPC channel.", this.getClass().getSimpleName());
super.grpcStubs.close();
}
private void waitForTakerDepositTxConfirmation() {