-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
82 lines (68 loc) · 1.86 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
package memcache
import (
"github.com/QuangTung97/go-memcache/memcache/netconn"
"log"
"net"
"time"
)
type memcacheOptions struct {
retryDuration time.Duration
bufferSize int
dialErrorLogger func(err error)
connOptions []netconn.Option
}
func (o *memcacheOptions) addConnOption(options ...netconn.Option) {
o.connOptions = append(o.connOptions, options...)
}
// Option ...
type Option func(opts *memcacheOptions)
func computeOptions(options ...Option) *memcacheOptions {
opts := &memcacheOptions{
retryDuration: 10 * time.Second,
bufferSize: 16 * 1024,
dialErrorLogger: func(err error) {
log.Println("[ERROR] Memcache dial error:", err)
},
}
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
opts.addConnOption(netconn.WithBufferSize(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.addConnOption(netconn.WithTCPKeepAliveDuration(d))
}
}
// WithDialFunc ...
func WithDialFunc(dialFunc func(network, address string, timeout time.Duration) (net.Conn, error)) Option {
return func(opts *memcacheOptions) {
opts.addConnOption(netconn.WithDialFunc(dialFunc))
}
}
// WithNetConnOptions ...
func WithNetConnOptions(options ...netconn.Option) Option {
return func(opts *memcacheOptions) {
opts.addConnOption(options...)
}
}