mirror of
https://github.com/bisq-network/bisq-api-reference.git
synced 2026-05-20 12:24:14 +00:00
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:
parent
bf876a9771
commit
aee939a887
@ -19,6 +19,7 @@ package bisq.bots;
|
|||||||
|
|
||||||
import bisq.proto.grpc.*;
|
import bisq.proto.grpc.*;
|
||||||
import io.grpc.CallCredentials;
|
import io.grpc.CallCredentials;
|
||||||
|
import io.grpc.ManagedChannel;
|
||||||
import io.grpc.ManagedChannelBuilder;
|
import io.grpc.ManagedChannelBuilder;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
@ -40,19 +41,13 @@ final class GrpcStubs {
|
|||||||
public final TradesGrpc.TradesBlockingStub tradesService;
|
public final TradesGrpc.TradesBlockingStub tradesService;
|
||||||
public final WalletsGrpc.WalletsBlockingStub walletsService;
|
public final WalletsGrpc.WalletsBlockingStub walletsService;
|
||||||
|
|
||||||
|
private final ManagedChannel channel;
|
||||||
|
|
||||||
public GrpcStubs(String apiHost, int apiPort, String apiPassword) {
|
public GrpcStubs(String apiHost, int apiPort, String apiPassword) {
|
||||||
CallCredentials credentials = new PasswordCallCredentials(apiPassword);
|
CallCredentials credentials = new PasswordCallCredentials(apiPassword);
|
||||||
|
|
||||||
var channel = ManagedChannelBuilder.forAddress(apiHost, apiPort).usePlaintext().build();
|
this.channel = ManagedChannelBuilder.forAddress(apiHost, apiPort).usePlaintext().build();
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
Runtime.getRuntime().addShutdownHook(new Thread(this::close));
|
||||||
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.disputeAgentsService = DisputeAgentsGrpc.newBlockingStub(channel).withCallCredentials(credentials);
|
this.disputeAgentsService = DisputeAgentsGrpc.newBlockingStub(channel).withCallCredentials(credentials);
|
||||||
this.helpService = HelpGrpc.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.tradesService = TradesGrpc.newBlockingStub(channel).withCallCredentials(credentials);
|
||||||
this.walletsService = WalletsGrpc.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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -116,6 +116,8 @@ public class RegtestTradePaymentSimulator extends AbstractBot {
|
|||||||
sleep(pollingInterval);
|
sleep(pollingInterval);
|
||||||
log.info("Trade is completed, printing all closed trades and exiting {}.", this.getClass().getSimpleName());
|
log.info("Trade is completed, printing all closed trades and exiting {}.", this.getClass().getSimpleName());
|
||||||
printTradesSummary(CLOSED);
|
printTradesSummary(CLOSED);
|
||||||
|
log.info("Closing {}'s gRPC channel.", this.getClass().getSimpleName());
|
||||||
|
super.grpcStubs.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void waitForTakerDepositTxConfirmation() {
|
private void waitForTakerDepositTxConfirmation() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user