Separate connect logic

* break out connect functionality out of OnStart
* introduce max retries
This commit is contained in:
Alexander Simmerl 2018-02-13 19:08:21 +01:00
parent 8da2a6a147
commit 82b1a34a36
1 changed files with 57 additions and 36 deletions

View File

@ -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,39 +66,14 @@ func (pvsc *PrivValidatorSocketClient) OnStart() error {
return err
}
var (
err error
conn net.Conn
)
RETRY_LOOP:
for {
conn, err = cmn.Connect(pvsc.SocketAddress)
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
}
}
pvsc.conn = conn
return nil
conn, err := pvsc.connect()
if err != nil {
return err
}
pvsc.conn = conn
return nil
}
// OnStop implements cmn.Service.
@ -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.