github.com/sunrise-zone/sunrise-node@v0.13.1-sr2/share/p2p/params.go (about)

     1  package p2p
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/libp2p/go-libp2p/core/protocol"
     8  )
     9  
    10  // Parameters is the set of parameters that must be configured for the shrex/eds protocol.
    11  type Parameters struct {
    12  	// ServerReadTimeout sets the timeout for reading messages from the stream.
    13  	ServerReadTimeout time.Duration
    14  
    15  	// ServerWriteTimeout sets the timeout for writing messages to the stream.
    16  	ServerWriteTimeout time.Duration
    17  
    18  	// HandleRequestTimeout defines the deadline for handling request.
    19  	HandleRequestTimeout time.Duration
    20  
    21  	// ConcurrencyLimit is the maximum number of concurrently handled streams
    22  	ConcurrencyLimit int
    23  
    24  	// networkID is prepended to the protocolID and represents the network the protocol is
    25  	// running on.
    26  	networkID string
    27  }
    28  
    29  func DefaultParameters() *Parameters {
    30  	return &Parameters{
    31  		ServerReadTimeout:    5 * time.Second,
    32  		ServerWriteTimeout:   time.Minute, // based on max observed sample time for 256 blocks (~50s)
    33  		HandleRequestTimeout: time.Minute,
    34  		ConcurrencyLimit:     10,
    35  	}
    36  }
    37  
    38  const errSuffix = "value should be positive and non-zero"
    39  
    40  func (p *Parameters) Validate() error {
    41  	if p.ServerReadTimeout <= 0 {
    42  		return fmt.Errorf("invalid stream read timeout: %v, %s", p.ServerReadTimeout, errSuffix)
    43  	}
    44  	if p.ServerWriteTimeout <= 0 {
    45  		return fmt.Errorf("invalid write timeout: %v, %s", p.ServerWriteTimeout, errSuffix)
    46  	}
    47  	if p.HandleRequestTimeout <= 0 {
    48  		return fmt.Errorf("invalid handle request timeout: %v, %s", p.HandleRequestTimeout, errSuffix)
    49  	}
    50  	if p.ConcurrencyLimit <= 0 {
    51  		return fmt.Errorf("invalid concurrency limit: %s", errSuffix)
    52  	}
    53  	return nil
    54  }
    55  
    56  // WithNetworkID sets the value of networkID in params
    57  func (p *Parameters) WithNetworkID(networkID string) {
    58  	p.networkID = networkID
    59  }
    60  
    61  // NetworkID returns the value of networkID stored in params
    62  func (p *Parameters) NetworkID() string {
    63  	return p.networkID
    64  }
    65  
    66  // ProtocolID creates a protocol ID string according to common format
    67  func ProtocolID(networkID, protocolString string) protocol.ID {
    68  	return protocol.ID(fmt.Sprintf("/%s%s", networkID, protocolString))
    69  }