github.com/pokt-network/tendermint@v0.32.11-0.20230426215212-59310158d3e9/privval/signer_dialer_endpoint.go (about)

     1  package privval
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/tendermint/tendermint/libs/log"
     7  	"github.com/tendermint/tendermint/libs/service"
     8  )
     9  
    10  const (
    11  	defaultMaxDialRetries        = 10
    12  	defaultRetryWaitMilliseconds = 100
    13  )
    14  
    15  // SignerServiceEndpointOption sets an optional parameter on the SignerDialerEndpoint.
    16  type SignerServiceEndpointOption func(*SignerDialerEndpoint)
    17  
    18  // SignerDialerEndpointTimeoutReadWrite sets the read and write timeout for connections
    19  // from external signing processes.
    20  func SignerDialerEndpointTimeoutReadWrite(timeout time.Duration) SignerServiceEndpointOption {
    21  	return func(ss *SignerDialerEndpoint) { ss.timeoutReadWrite = timeout }
    22  }
    23  
    24  // SignerDialerEndpointConnRetries sets the amount of attempted retries to acceptNewConnection.
    25  func SignerDialerEndpointConnRetries(retries int) SignerServiceEndpointOption {
    26  	return func(ss *SignerDialerEndpoint) { ss.maxConnRetries = retries }
    27  }
    28  
    29  // SignerDialerEndpointRetryWaitInterval sets the retry wait interval to a custom value
    30  func SignerDialerEndpointRetryWaitInterval(interval time.Duration) SignerServiceEndpointOption {
    31  	return func(ss *SignerDialerEndpoint) { ss.retryWait = interval }
    32  }
    33  
    34  // SignerDialerEndpoint dials using its dialer and responds to any
    35  // signature requests using its privVal.
    36  type SignerDialerEndpoint struct {
    37  	signerEndpoint
    38  
    39  	dialer SocketDialer
    40  
    41  	retryWait      time.Duration
    42  	maxConnRetries int
    43  }
    44  
    45  // NewSignerDialerEndpoint returns a SignerDialerEndpoint that will dial using the given
    46  // dialer and respond to any signature requests over the connection
    47  // using the given privVal.
    48  func NewSignerDialerEndpoint(
    49  	logger log.Logger,
    50  	dialer SocketDialer,
    51  ) *SignerDialerEndpoint {
    52  
    53  	sd := &SignerDialerEndpoint{
    54  		dialer:         dialer,
    55  		retryWait:      defaultRetryWaitMilliseconds * time.Millisecond,
    56  		maxConnRetries: defaultMaxDialRetries,
    57  	}
    58  
    59  	sd.BaseService = *service.NewBaseService(logger, "SignerDialerEndpoint", sd)
    60  	sd.signerEndpoint.timeoutReadWrite = defaultTimeoutReadWriteSeconds * time.Second
    61  
    62  	return sd
    63  }
    64  
    65  func (sd *SignerDialerEndpoint) ensureConnection() error {
    66  	if sd.IsConnected() {
    67  		return nil
    68  	}
    69  
    70  	retries := 0
    71  	for retries < sd.maxConnRetries {
    72  		conn, err := sd.dialer()
    73  
    74  		if err != nil {
    75  			retries++
    76  			sd.Logger.Debug("SignerDialer: Reconnection failed", "retries", retries, "max", sd.maxConnRetries, "err", err)
    77  			// Wait between retries
    78  			time.Sleep(sd.retryWait)
    79  		} else {
    80  			sd.SetConnection(conn)
    81  			sd.Logger.Debug("SignerDialer: Connection Ready")
    82  			return nil
    83  		}
    84  	}
    85  
    86  	sd.Logger.Debug("SignerDialer: Max retries exceeded", "retries", retries, "max", sd.maxConnRetries)
    87  
    88  	return ErrNoConnection
    89  }