-
Notifications
You must be signed in to change notification settings - Fork 901
/
options.go
102 lines (83 loc) · 2.94 KB
/
options.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package shrexnd
import (
"fmt"
"time"
logging "github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p/core/protocol"
)
const protocolString = "/shrex/nd/0.0.1"
var log = logging.Logger("shrex/nd")
// Option is the functional option that is applied to the shrex/eds protocol to configure its
// parameters.
type Option func(*Parameters)
// Parameters is the set of parameters that must be configured for the shrex/eds protocol.
type Parameters struct {
// readTimeout sets the timeout for reading messages from the stream.
readTimeout time.Duration
// writeTimeout sets the timeout for writing messages to the stream.
writeTimeout time.Duration
// serveTimeout defines the deadline for serving request.
serveTimeout time.Duration
// networkID is prepended to the protocolID and represents the network the protocol is
// running on.
networkID string
// concurrencyLimit is the maximum number of concurrently handled streams
concurrencyLimit int
}
func DefaultParameters() *Parameters {
return &Parameters{
readTimeout: time.Second * 5,
writeTimeout: time.Second * 10,
serveTimeout: time.Second * 10,
concurrencyLimit: 10,
}
}
const errSuffix = "value should be positive and non-zero"
func (p *Parameters) Validate() error {
if p.readTimeout <= 0 {
return fmt.Errorf("invalid stream read timeout: %v, %s", p.readTimeout, errSuffix)
}
if p.writeTimeout <= 0 {
return fmt.Errorf("invalid write timeout: %v, %s", p.writeTimeout, errSuffix)
}
if p.serveTimeout <= 0 {
return fmt.Errorf("invalid serve timeout: %v, %s", p.serveTimeout, errSuffix)
}
if p.concurrencyLimit <= 0 {
return fmt.Errorf("invalid concurrency limit: %v, %s", p.concurrencyLimit, errSuffix)
}
return nil
}
// WithNetworkID is a functional option that configures the `networkID` parameter
func WithNetworkID(networkID string) Option {
return func(parameters *Parameters) {
parameters.networkID = networkID
}
}
// WithReadTimeout is a functional option that configures the `readTimeout` parameter
func WithReadTimeout(readTimeout time.Duration) Option {
return func(parameters *Parameters) {
parameters.readTimeout = readTimeout
}
}
// WithWriteTimeout is a functional option that configures the `writeTimeout` parameter
func WithWriteTimeout(writeTimeout time.Duration) Option {
return func(parameters *Parameters) {
parameters.writeTimeout = writeTimeout
}
}
// WithServeTimeout is a functional option that configures the `serveTimeout` parameter
func WithServeTimeout(serveTimeout time.Duration) Option {
return func(parameters *Parameters) {
parameters.serveTimeout = serveTimeout
}
}
// WithConcurrencyLimit is a functional option that configures the `concurrencyLimit` parameter
func WithConcurrencyLimit(concurrencyLimit int) Option {
return func(parameters *Parameters) {
parameters.concurrencyLimit = concurrencyLimit
}
}
func protocolID(networkID string) protocol.ID {
return protocol.ID(fmt.Sprintf("%s%s", networkID, protocolString))
}