Separate connect logic
* break out connect functionality out of OnStart * introduce max retries
This commit is contained in:
parent
8da2a6a147
commit
82b1a34a36
|
@ -17,6 +17,16 @@ import (
|
|||
"github.com/tendermint/tendermint/types"
|
||||
)
|
||||
|
||||
const (
|
||||
dialRetryIntervalSeconds = 1
|
||||
dialRetryMax = 10
|
||||
)
|
||||
|
||||
// Socket errors.
|
||||
var (
|
||||
ErrDialRetryMax = errors.New("Error max client retries")
|
||||
)
|
||||
|
||||
//-----------------------------------------------------------------
|
||||
|
||||
var _ types.PrivValidator2 = (*PrivValidatorSocketClient)(nil)
|
||||
|
@ -33,10 +43,6 @@ type PrivValidatorSocketClient struct {
|
|||
SocketAddress string
|
||||
}
|
||||
|
||||
const (
|
||||
dialRetryIntervalSeconds = 1
|
||||
)
|
||||
|
||||
// NewPrivValidatorSocketClient returns an instance of
|
||||
// PrivValidatorSocketClient.
|
||||
func NewPrivValidatorSocketClient(
|
||||
|
@ -60,40 +66,15 @@ func (pvsc *PrivValidatorSocketClient) OnStart() error {
|
|||
return err
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
conn net.Conn
|
||||
)
|
||||
|
||||
RETRY_LOOP:
|
||||
for {
|
||||
conn, err = cmn.Connect(pvsc.SocketAddress)
|
||||
conn, err := pvsc.connect()
|
||||
if err != nil {
|
||||
pvsc.Logger.Error(
|
||||
"OnStart",
|
||||
"addr", pvsc.SocketAddress,
|
||||
"err", errors.Wrap(err, "connection failed"),
|
||||
)
|
||||
|
||||
time.Sleep(time.Second * dialRetryIntervalSeconds)
|
||||
continue RETRY_LOOP
|
||||
}
|
||||
|
||||
if pvsc.privKey != nil {
|
||||
conn, err = p2pconn.MakeSecretConnection(conn, pvsc.privKey.Wrap())
|
||||
if err != nil {
|
||||
pvsc.Logger.Error(
|
||||
"OnStart",
|
||||
"err", errors.Wrap(err, "encrypting connection failed"),
|
||||
)
|
||||
continue RETRY_LOOP
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
pvsc.conn = conn
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// OnStop implements cmn.Service.
|
||||
func (pvsc *PrivValidatorSocketClient) OnStop() {
|
||||
|
@ -175,6 +156,46 @@ func (pvsc *PrivValidatorSocketClient) SignHeartbeat(chainID string, heartbeat *
|
|||
return nil
|
||||
}
|
||||
|
||||
func (pvsc *PrivValidatorSocketClient) connect() (net.Conn, error) {
|
||||
retries := dialRetryMax
|
||||
|
||||
RETRY_LOOP:
|
||||
for retries > 0 {
|
||||
if retries != dialRetryMax {
|
||||
time.Sleep(time.Second * dialRetryIntervalSeconds)
|
||||
}
|
||||
|
||||
retries--
|
||||
|
||||
conn, err := cmn.Connect(pvsc.SocketAddress)
|
||||
if err != nil {
|
||||
pvsc.Logger.Error(
|
||||
"OnStart",
|
||||
"addr", pvsc.SocketAddress,
|
||||
"err", errors.Wrap(err, "connection failed"),
|
||||
)
|
||||
|
||||
continue RETRY_LOOP
|
||||
}
|
||||
|
||||
if pvsc.privKey != nil {
|
||||
conn, err = p2pconn.MakeSecretConnection(conn, pvsc.privKey.Wrap())
|
||||
if err != nil {
|
||||
pvsc.Logger.Error(
|
||||
"OnStart",
|
||||
"err", errors.Wrap(err, "encrypting connection failed"),
|
||||
)
|
||||
|
||||
continue RETRY_LOOP
|
||||
}
|
||||
}
|
||||
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
return nil, ErrDialRetryMax
|
||||
}
|
||||
|
||||
//---------------------------------------------------------
|
||||
|
||||
// PrivValidatorSocketServer implements PrivValidator.
|
||||
|
|
Loading…
Reference in New Issue