-
Notifications
You must be signed in to change notification settings - Fork 14
/
opts.go
97 lines (84 loc) · 2.74 KB
/
opts.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
package gorums
import (
"log"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/backoff"
"google.golang.org/grpc/metadata"
)
type managerOptions struct {
grpcDialOpts []grpc.DialOption
nodeDialTimeout time.Duration
logger *log.Logger
noConnect bool
backoff backoff.Config
sendBuffer uint
metadata metadata.MD
perNodeMD func(uint32) metadata.MD
}
func newManagerOptions() managerOptions {
return managerOptions{
backoff: backoff.DefaultConfig,
sendBuffer: 0,
nodeDialTimeout: 50 * time.Millisecond,
}
}
// ManagerOption provides a way to set different options on a new Manager.
type ManagerOption func(*managerOptions)
// WithDialTimeout returns a ManagerOption which is used to set the dial
// context timeout to be used when initially connecting to each node in its pool.
func WithDialTimeout(timeout time.Duration) ManagerOption {
return func(o *managerOptions) {
o.nodeDialTimeout = timeout
}
}
// WithGrpcDialOptions returns a ManagerOption which sets any gRPC dial options
// the Manager should use when initially connecting to each node in its pool.
func WithGrpcDialOptions(opts ...grpc.DialOption) ManagerOption {
return func(o *managerOptions) {
o.grpcDialOpts = append(o.grpcDialOpts, opts...)
}
}
// WithLogger returns a ManagerOption which sets an optional error logger for
// the Manager.
func WithLogger(logger *log.Logger) ManagerOption {
return func(o *managerOptions) {
o.logger = logger
}
}
// WithNoConnect returns a ManagerOption which instructs the Manager not to
// connect to any of its nodes. Mainly used for testing purposes.
func WithNoConnect() ManagerOption {
return func(o *managerOptions) {
o.noConnect = true
}
}
// WithBackoff allows for changing the backoff delays used by Gorums.
func WithBackoff(backoff backoff.Config) ManagerOption {
return func(o *managerOptions) {
o.backoff = backoff
}
}
// WithSendBufferSize allows for changing the size of the send buffer used by Gorums.
// A larger buffer might achieve higher throughput for asynchronous calltypes, but at
// the cost of latency.
func WithSendBufferSize(size uint) ManagerOption {
return func(o *managerOptions) {
o.sendBuffer = size
}
}
// WithMetadata returns a ManagerOption that sets the metadata that is sent to each node
// when the connection is initially established. This metadata can be retrieved from the
// server-side method handlers.
func WithMetadata(md metadata.MD) ManagerOption {
return func(o *managerOptions) {
o.metadata = md
}
}
// WithPerNodeMetadata returns a ManagerOption that allows you to set metadata for each
// node individually.
func WithPerNodeMetadata(f func(uint32) metadata.MD) ManagerOption {
return func(o *managerOptions) {
o.perNodeMD = f
}
}