-
Notifications
You must be signed in to change notification settings - Fork 23
/
pool_options.go
80 lines (67 loc) · 1.98 KB
/
pool_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
package connection
import (
"time"
)
type PoolOption func(*PoolOptions) error
type PoolOptions struct {
// ReconnectWait sets the time to wait after first re-connect attempt
// The default is 5 seconds
ReconnectWait time.Duration
// MaxReconnectWait specifies the maximum duration to wait between
// reconnection attempts, serving as the upper bound for exponential
// backoff.
// A zero value means no exponential backoff and ReconnectWait is used
// for each retry.
// The default is 0.
MaxReconnectWait time.Duration
// ErrorHandler is called in a goroutine with the errors that can't be
// returned to the caller. Don't block in this function as it will
// block the connection pool Close() method.
ErrorHandler func(err error)
// MinConnections is the number of connections required to be established when
// we connect the pool.
// A zero value means that Pool will not return error on `Connect` and will
// try to connect to all the addresses in the pool.
// The default is 1.
MinConnections int
// ConnectionsFilter is a function to filter connections in the pool
// when Get() is called
ConnectionsFilter func(*Connection) bool
}
func GetDefaultPoolOptions() PoolOptions {
return PoolOptions{
ReconnectWait: 5 * time.Second,
MaxReconnectWait: 0,
MinConnections: 1,
}
}
func PoolReconnectWait(rw time.Duration) PoolOption {
return func(opts *PoolOptions) error {
opts.ReconnectWait = rw
return nil
}
}
func PoolMaxReconnectWait(rw time.Duration) PoolOption {
return func(opts *PoolOptions) error {
opts.MaxReconnectWait = rw
return nil
}
}
func PoolMinConnections(n int) PoolOption {
return func(opts *PoolOptions) error {
opts.MinConnections = n
return nil
}
}
func PoolErrorHandler(h func(err error)) PoolOption {
return func(opts *PoolOptions) error {
opts.ErrorHandler = h
return nil
}
}
func PoolConnectionsFilter(f func(*Connection) bool) PoolOption {
return func(opts *PoolOptions) error {
opts.ConnectionsFilter = f
return nil
}
}