From d4ac7c1704ad39760e061a76a052007f5219b79c Mon Sep 17 00:00:00 2001 From: shawn Date: Mon, 25 Mar 2024 17:38:59 +0800 Subject: [PATCH] fix: add timeout and view blockNumber as the sucessful dial --- chain/chain.go | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/chain/chain.go b/chain/chain.go index 49a12fa4a..4f98adf99 100644 --- a/chain/chain.go +++ b/chain/chain.go @@ -84,26 +84,35 @@ func InitChain( for _, endpoint := range chainconfig.MultiEndpoint { go func(e string) { b, err := ethclient.Dial(e) - if err == nil { - backendChan <- b + if err != nil { + return } + _, err = backend.BlockNumber(context.Background()) + if err != nil { + return + } + backendChan <- b }(endpoint) } - backend = <-backendChan + select { + case backend = <-backendChan: + case <-time.After(time.Second * 60): + return nil, errors.New("could not connect all rpc configuration after 1 min, please try again or check your network") + } } else { backend, err = ethclient.Dial(chainconfig.Endpoint) if err != nil { return nil, fmt.Errorf("dial eth client: %w", err) } - } - _, err = backend.BlockNumber(context.Background()) - if err != nil { - errMsg := "Could not connect to blockchain rpc, please check your network connection" - if err == io.EOF { - return nil, errors.New(errMsg) + _, err = backend.BlockNumber(context.Background()) + if err != nil { + errMsg := "Could not connect to blockchain rpc, please check your network connection" + if err == io.EOF { + return nil, errors.New(errMsg) + } + return nil, fmt.Errorf("%s.%w", errMsg, err) } - return nil, fmt.Errorf("%s.%w", errMsg, err) } overlayEthAddress, err := signer.EthereumAddress()