From aee939a887974495b3fc841d9a1fb81f9ed38435 Mon Sep 17 00:00:00 2001 From: ghubstan <36207203+ghubstan@users.noreply.github.com> Date: Sat, 25 Jun 2022 14:11:06 -0300 Subject: [PATCH] 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(). --- .../src/main/java/bisq/bots/GrpcStubs.java | 27 ++++++++++++------- .../bots/RegtestTradePaymentSimulator.java | 2 ++ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/java-examples/src/main/java/bisq/bots/GrpcStubs.java b/java-examples/src/main/java/bisq/bots/GrpcStubs.java index c6ed9fb..2f6d101 100644 --- a/java-examples/src/main/java/bisq/bots/GrpcStubs.java +++ b/java-examples/src/main/java/bisq/bots/GrpcStubs.java @@ -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); + } + } } diff --git a/java-examples/src/main/java/bisq/bots/RegtestTradePaymentSimulator.java b/java-examples/src/main/java/bisq/bots/RegtestTradePaymentSimulator.java index 3bac466..7d2d8c8 100644 --- a/java-examples/src/main/java/bisq/bots/RegtestTradePaymentSimulator.java +++ b/java-examples/src/main/java/bisq/bots/RegtestTradePaymentSimulator.java @@ -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() {