Minor improvements to shutdown logic in BTC connectivity in Relay Maintainer
@@ -46,10 +46,6 @@ func Connect(
)
}
- // when we are exiting the program, cancel all requests from the rpc client
- // and disconnect it
- go shutdownClient(ctx, client)
-
err = testConnection(client, connectionTimeout)
if err != nil {
return nil, fmt.Errorf(
@@ -60,6 +56,14 @@ func Connect(
)
}
+ // When the context is done, cancel all requests from the RPC client
+ // and disconnect it.
+ go func() {
+ <-ctx.Done()
+ logger.Info("disconnecting from remote Bitcoin chain")
+ client.Shutdown()
+ }()
+
return &remoteChain{client: client}, nil
}
@@ -122,12 +126,6 @@ func testConnection(client *rpcclient.Client, timeout time.Duration) error {
}
}
-func shutdownClient(ctx context.Context, client *rpcclient.Client) {
- <-ctx.Done()
- logger.Info("Shutting down Bitcoin rpc client")
- client.Shutdown()
-}
-
func serializeHeader(header *wire.BlockHeader) ([]byte, error) {
var buffer bytes.Buffer
