-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.go
More file actions
177 lines (144 loc) · 4.01 KB
/
Copy pathoptions.go
File metadata and controls
177 lines (144 loc) · 4.01 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package dchan
import (
"context"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
var (
DefaultSendTimeout = 2 * time.Minute
DefaultClusterTimeout = 2 * time.Minute
DefaultSnapshotCount = 3
DefaultMaxJoinRetries = 5
DefaultRetryDelay = 3 * time.Second
DefaultCallOptions = []grpc.CallOption{
grpc.WaitForReady(true),
}
DefaultDialOptions = []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
}
DefaultServerOptions = []grpc.ServerOption{
grpc.Creds(insecure.NewCredentials()),
}
)
type Option func(*Chan) error
type Config struct {
// Server ID of this server.
Id ServerID
// Base directory for the store.
StoreDir string
// Every node must have this unique cluster ID.
//
// Currently used for file store names, but possibly we should
// append it to the address as an ID.
ClusterId string
// Cluster addresses of initial cluster members (e.g. possibly itself).
ClusterAddresses []string
// Timeout for sending messages
// Default to 2 minute
//
// For large messages, suggest setting a larger timeout OR
// using a WithTimeout message.
SendTimeout time.Duration
// Timeout for cluster operations
// Default to 2 minute
ClusterTimeout time.Duration
// Dial options for the gRPC clients to connect to other servers.
DialOptions []grpc.DialOption
// We should have WaitForReady option for the gRPC clients to connect to other servers.
// Used for messages and cluster operations.
CallOptions []grpc.CallOption
// Server options for the gRPC server.
ServerOptions []grpc.ServerOption
// Snapshot count for the raft snapshot store.
SnapshotCount int
// Max number of retries to join the cluster.
MaxJoinRetries int
// Delay between retries.
RetryDelay time.Duration
}
// sendCtx returns a context with the send timeout.
func (c *Config) sendCtx() (context.Context, context.CancelFunc) {
if c.SendTimeout > 0 {
return context.WithTimeout(context.Background(), c.SendTimeout)
}
return context.Background(), func() {}
}
// clusterCtx returns a context with the cluster timeout.
func (c *Config) clusterCtx() (context.Context, context.CancelFunc) {
if c.ClusterTimeout > 0 {
return context.WithTimeout(context.Background(), c.ClusterTimeout)
}
return context.Background(), func() {}
}
func DefaultConfig() Config {
return Config{
SendTimeout: DefaultSendTimeout,
ClusterTimeout: DefaultClusterTimeout,
SnapshotCount: DefaultSnapshotCount,
MaxJoinRetries: DefaultMaxJoinRetries,
RetryDelay: DefaultRetryDelay,
DialOptions: DefaultDialOptions,
CallOptions: DefaultCallOptions,
ServerOptions: DefaultServerOptions,
}
}
// WithSendTimeout configures the send timeout.
// Default to 2 minute.
//
// Don't recommend using a very small timeout.
func WithSendTimeout(timeout time.Duration) Option {
return func(d *Chan) error {
d.SendTimeout = timeout
return nil
}
}
// WithClusterTimeout configures the cluster timeout.
// Default to 2 minute.
//
// Don't recommend using a very small timeout.
func WithClusterTimeout(timeout time.Duration) Option {
return func(d *Chan) error {
d.ClusterTimeout = timeout
return nil
}
}
// WithConfig configures the dChan with the given config.
func WithConfig(config *Config) Option {
return func(d *Chan) error {
d.Config = *config
return nil
}
}
// Optionally reuse a gRPC server that exists.
func WithGrpcServer(addr string, server *grpc.Server) Option {
return func(d *Chan) error {
d.Id = ServerID(addr)
d.grpcServer = server
return nil
}
}
func WithDialOptions(options ...grpc.DialOption) Option {
return func(d *Chan) error {
d.DialOptions = options
return nil
}
}
func WithCallOptions(options ...grpc.CallOption) Option {
return func(d *Chan) error {
d.CallOptions = options
return nil
}
}
func WithServerOptions(options ...grpc.ServerOption) Option {
return func(d *Chan) error {
d.ServerOptions = options
return nil
}
}
func WithSnapshotCount(count int) Option {
return func(d *Chan) error {
d.SnapshotCount = count
return nil
}
}