Handle chain server connection errors, if any
This commit is contained in:
parent
966b6b0ec7
commit
2a5e8587f6
1 changed files with 24 additions and 3 deletions
27
btcwallet.go
27
btcwallet.go
|
@ -82,6 +82,10 @@ func walletMain() error {
|
||||||
// Shutdown the server if an interrupt signal is received.
|
// Shutdown the server if an interrupt signal is received.
|
||||||
addInterruptHandler(server.Stop)
|
addInterruptHandler(server.Stop)
|
||||||
|
|
||||||
|
// Create a channel to report unrecoverable errors during chain
|
||||||
|
// server connection
|
||||||
|
chainSvrErrors := make(chan error)
|
||||||
|
|
||||||
// Create channel so that the goroutine which opens the chain server
|
// Create channel so that the goroutine which opens the chain server
|
||||||
// connection can pass the conn to the goroutine which opens the wallet.
|
// connection can pass the conn to the goroutine which opens the wallet.
|
||||||
// Buffer the channel so sends are not blocked, since if the wallet is
|
// Buffer the channel so sends are not blocked, since if the wallet is
|
||||||
|
@ -89,11 +93,14 @@ func walletMain() error {
|
||||||
chainSvrChan := make(chan *chain.Client, 1)
|
chainSvrChan := make(chan *chain.Client, 1)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
defer close(chainSvrErrors)
|
||||||
|
|
||||||
// Read CA certs and create the RPC client.
|
// Read CA certs and create the RPC client.
|
||||||
certs, err := ioutil.ReadFile(cfg.CAFile)
|
certs, err := ioutil.ReadFile(cfg.CAFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Cannot open CA file: %v", err)
|
log.Errorf("Cannot open CA file: %v", err)
|
||||||
close(chainSvrChan)
|
close(chainSvrChan)
|
||||||
|
chainSvrErrors <- err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
rpcc, err := chain.NewClient(activeNet.Params, cfg.RPCConnect,
|
rpcc, err := chain.NewClient(activeNet.Params, cfg.RPCConnect,
|
||||||
|
@ -101,6 +108,7 @@ func walletMain() error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Cannot create chain server RPC client: %v", err)
|
log.Errorf("Cannot create chain server RPC client: %v", err)
|
||||||
close(chainSvrChan)
|
close(chainSvrChan)
|
||||||
|
chainSvrErrors <- err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = rpcc.Start()
|
err = rpcc.Start()
|
||||||
|
@ -146,16 +154,29 @@ func walletMain() error {
|
||||||
|
|
||||||
// Start wallet goroutines and handle RPC client notifications
|
// Start wallet goroutines and handle RPC client notifications
|
||||||
// if the chain server connection was opened.
|
// if the chain server connection was opened.
|
||||||
|
// If the chain server connection failed due to an unrecoverable error,
|
||||||
|
// doesn't matter that the wallet goroutines are not started since the
|
||||||
|
// entire program will exit on receiving chainSvrErrors
|
||||||
select {
|
select {
|
||||||
case chainSvr := <-chainSvrChan:
|
case chainSvr, ok := <-chainSvrChan:
|
||||||
w.Start(chainSvr)
|
if ok {
|
||||||
|
// Start wallet goroutines only when the chain rpc client was created
|
||||||
|
w.Start(chainSvr)
|
||||||
|
}
|
||||||
case <-server.quit:
|
case <-server.quit:
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
// Check for unrecoverable errors during chain server connection, and return
|
||||||
|
// the error, if any.
|
||||||
|
err, ok := <-chainSvrErrors
|
||||||
|
if ok {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// Check for unrecoverable errors during the wallet startup, and return
|
// Check for unrecoverable errors during the wallet startup, and return
|
||||||
// the error, if any.
|
// the error, if any.
|
||||||
err, ok := <-walletOpenErrors
|
err, ok = <-walletOpenErrors
|
||||||
if ok {
|
if ok {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue