Browse Source

Separate connect logic

* break out connect functionality out of OnStart
* introduce max retries
pull/1204/head
Alexander Simmerl 7 years ago
parent
commit
82b1a34a36
1 changed files with 56 additions and 35 deletions
  1. +56
    -35
      types/priv_validator/socket.go

+ 56
- 35
types/priv_validator/socket.go 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
}
conn, err := pvsc.connect()
if err != nil {
return err
}
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
pvsc.conn = conn
return nil
}
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.


Loading…
Cancel
Save