-
Notifications
You must be signed in to change notification settings - Fork 5
/
peer_options.go
81 lines (69 loc) · 1.96 KB
/
peer_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
package p2p
import (
"fmt"
"net"
"time"
)
type PeerOptions func(p *Peer) error
func WithDialer(dial func(network, address string) (net.Conn, error)) PeerOptions {
return func(p *Peer) error {
p.dial = dial
return nil
}
}
func WithBatchDelay(batchDelay time.Duration) PeerOptions {
return func(p *Peer) error {
p.batchDelay = batchDelay
return nil
}
}
func WithIncomingConnection(conn net.Conn) PeerOptions {
return func(p *Peer) error {
p.incomingConn = conn
return nil
}
}
func WithMaximumMessageSize(maximumMessageSize int64) PeerOptions {
return func(p *Peer) error {
p.maximumMessageSize = maximumMessageSize
return nil
}
}
func WithUserAgent(userAgentName string, userAgentVersion string) PeerOptions {
return func(p *Peer) error {
if userAgentName == "" || userAgentVersion == "" {
return fmt.Errorf("both user agent name '%s' and version '%s' must not be empty strings", userAgentName, userAgentVersion)
}
p.userAgentName = &userAgentName
p.userAgentVersion = &userAgentVersion
return nil
}
}
func WithRetryReadWriteMessageInterval(d time.Duration) PeerOptions {
return func(p *Peer) error {
p.retryReadWriteMessageInterval = d
return nil
}
}
func WithNrOfWriteHandlers(NrWriteHandlers int) PeerOptions {
return func(p *Peer) error {
p.nrWriteHandlers = NrWriteHandlers
return nil
}
}
// WithPingInterval sets the optional time duration ping interval and connection health threshold
// ping interval is the time interval in which the peer sends a ping
// connection health threshold is the time duration after which the connection is marked unhealthy if no signal is received
func WithPingInterval(pingInterval time.Duration, connectionHealthThreshold time.Duration) PeerOptions {
return func(p *Peer) error {
p.pingInterval = pingInterval
p.connectionHealthThreshold = connectionHealthThreshold
return nil
}
}
func WithReadBufferSize(size int) PeerOptions {
return func(p *Peer) error {
p.buffSize = size
return nil
}
}