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