-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
146 lines (121 loc) · 3.2 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
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
package grpcpool
import (
"log"
"math"
"os"
"time"
"github.com/prometheus/client_golang/prometheus"
)
const (
defaultGrpcPoolSize = math.MaxInt32
defaultMaxStreamsClient = 100
defaultMaxIdle = 3
defaultCleanIntervalTime = time.Second
defaultClientIdleTimeout = time.Minute
)
// Logger is used for logging formatted messages.
type Logger interface {
// Printf must have the same semantics as log.Printf.
Printf(format string, args ...interface{})
}
// Option .
type Option func(*option)
type option struct {
// Pool size
GrpcPoolSize int
// MaxStreamsClient http2 defaultMaxStreamsClient = 100
MaxStreamsClient int
// Maximum number of idle connections in the pool
MaxIdle int
// CleanIntervalTime is the interval time to clean idle connections
CleanIntervalTime time.Duration
// ClientIdleTimeout idle connection timeout
ClientIdleTimeout time.Duration
// When Nonblocking is true, Pool.Get will never be blocked.
// ErrPoolOverload will be returned when Pool is exhausted.
Nonblocking bool
// Logger is the customized logger for logging info, if it is not set,
// default standard logger from log package is used.
Logger Logger
// Debug
Debug bool
}
var defaultOption = option{
GrpcPoolSize: defaultGrpcPoolSize,
MaxStreamsClient: defaultMaxStreamsClient,
MaxIdle: defaultMaxIdle,
ClientIdleTimeout: defaultClientIdleTimeout,
CleanIntervalTime: defaultCleanIntervalTime,
Logger: Logger(log.New(os.Stderr, "", log.LstdFlags)),
}
func getDefaultOpt() *option {
opt := defaultOption
return &opt
}
// WithGrpcPoolSize returns a Option which sets the value for pool size
func WithGrpcPoolSize(size int) Option {
return func(opt *option) {
opt.GrpcPoolSize = size
}
}
// WithMaxStreamsClient returns a Option which set the value for
// http2 client maxConcurrentStreams
func WithMaxStreamsClient(num int) Option {
return func(opt *option) {
opt.MaxStreamsClient = num
}
}
// WithMaxIdle set number of idle connections in the pool.
func WithMaxIdle(num int) Option {
return func(opt *option) {
opt.MaxIdle = num
}
}
// WithCleanIntervalTime set interval time to clean up
// idle connections or create new tcp connection
func WithCleanIntervalTime(t time.Duration) Option {
return func(opt *option) {
opt.CleanIntervalTime = t
}
}
// WithClientIdleTimeout .
func WithClientIdleTimeout(t time.Duration) Option {
return func(opt *option) {
opt.ClientIdleTimeout = t
}
}
// WithNonblocking returns a Option which Pool.Get can never be blocked.
// ErrPoolOverload will be returned when Pool is exhausted.
func WithNonblocking() Option {
return func(opt *option) {
opt.Nonblocking = true
}
}
// WithLogger returns a Option which sets the value for pool logger
func WithLogger(logger Logger) Option {
return func(opt *option) {
opt.Logger = logger
}
}
// WithDebug .
func WithDebug() Option {
return func(opt *option) {
opt.Debug = true
}
}
var (
statistics = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "statistics",
Help: "Number of 'get' and 'put'",
},
[]string{"get_put"},
)
connection = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "connection",
Help: "Number of connection",
},
[]string{"conn"},
)
)