-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
71 lines (60 loc) · 1.55 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
package memcache
import (
"log"
"net"
"time"
)
type memcacheOptions struct {
retryDuration time.Duration
bufferSize int
tcpKeepAliveDuration time.Duration
dialErrorLogger func(err error)
dialFunc func(network, address string) (net.Conn, error)
}
// Option ...
type Option func(opts *memcacheOptions)
func computeOptions(options ...Option) *memcacheOptions {
opts := &memcacheOptions{
retryDuration: 10 * time.Second,
bufferSize: 16 * 1024,
tcpKeepAliveDuration: 5 * time.Minute,
dialErrorLogger: func(err error) {
log.Println("[ERROR] Memcache dial error:", err)
},
dialFunc: net.Dial,
}
for _, o := range options {
o(opts)
}
return opts
}
// WithRetryDuration duration between TCP connection retry
func WithRetryDuration(d time.Duration) Option {
return func(opts *memcacheOptions) {
opts.retryDuration = d
}
}
// WithBufferSize change receiving & sending buffer size
func WithBufferSize(size int) Option {
return func(opts *memcacheOptions) {
opts.bufferSize = size
}
}
// WithDialErrorLogger set the dial error logger
func WithDialErrorLogger(fn func(err error)) Option {
return func(opts *memcacheOptions) {
opts.dialErrorLogger = fn
}
}
// WithTCPKeepAliveDuration sets the tcp keep alive duration
func WithTCPKeepAliveDuration(d time.Duration) Option {
return func(opts *memcacheOptions) {
opts.tcpKeepAliveDuration = d
}
}
// WithDialFunc ...
func WithDialFunc(dialFunc func(network, address string) (net.Conn, error)) Option {
return func(opts *memcacheOptions) {
opts.dialFunc = dialFunc
}
}