-
Notifications
You must be signed in to change notification settings - Fork 5
/
pool.go
107 lines (88 loc) · 3.34 KB
/
pool.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
package prom
import "time"
// IBasePoolOpts is the base interface to define configurations for a connection pool.
//
// @Available since v1.0.0
type IBasePoolOpts interface {
}
// BasePoolOpts is an abstract implementation of IBasePoolOpts.
//
// @Available since v1.0.0
type BasePoolOpts struct {
// Maximum number of connections.
// Set to zero or negative value to use default value.
MaxPoolSize int `json:"max_size"`
// Minimum number of idle connections. Default value is 1.
MinPoolSize int `json:"min_size"`
// Maximum amount of time a connection may be reused.
// Set to zero or negative value to use default value.
ConnLifetime time.Duration `json:"conn_lifetime"`
}
// IBaseConnection is the base interface to define a connection.
//
// @Available since v1.0.0
type IBaseConnection interface {
// PoolOpts returns the pooling options attached to this connection.
PoolOpts() IBasePoolOpts
// MetricsLogger returns the metrics logger attached to this connection.
MetricsLogger() IMetricsLogger
// RegisterMetricsLogger attaches a metrics logger to this connection.
// This function returns the connection itself for chaining.
RegisterMetricsLogger(logger IMetricsLogger) IBaseConnection
// NewCmdExecInfo is the convenient function to create a new CmdExecInfo instance.
// The returned CmdExecInfo should have its 'id' and 'begin-time' fields initialized.
NewCmdExecInfo() *CmdExecInfo
// LogMetrics is the convenient function to put the CmdExecInfo to the metrics log.
// This function is silently no-op if the input iis nil or there is no associated metrics logger.
LogMetrics(category string, cmd *CmdExecInfo) error
// Metrics is the convenient function to capture the snapshot of command execution metrics.
// This function is silently no-op if there is no associated metrics logger.
Metrics(category string, opts ...MetricsOpts) (*Metrics, error)
}
// BaseConnection is an abstract implementation of IBaseConnection.
//
// @Available since v1.0.0
type BaseConnection struct {
poolOpts IBasePoolOpts
metricsLogger IMetricsLogger
}
// PoolOpts implements IBaseConnection.PoolOpts.
func (c *BaseConnection) PoolOpts() IBasePoolOpts {
return c.poolOpts
}
// SetPoolOpts attaches a pooling options object to the connection.
func (c *BaseConnection) SetPoolOpts(opts IBasePoolOpts) IBaseConnection {
c.poolOpts = opts
return c
}
// MetricsLogger implements IBaseConnection.MetricsLogger.
func (c *BaseConnection) MetricsLogger() IMetricsLogger {
return c.metricsLogger
}
// RegisterMetricsLogger implements IBaseConnection.RegisterMetricsLogger.
func (c *BaseConnection) RegisterMetricsLogger(logger IMetricsLogger) IBaseConnection {
c.metricsLogger = logger
return c
}
// NewCmdExecInfo implements IBaseConnection.NewCmdExecInfo.
func (c *BaseConnection) NewCmdExecInfo() *CmdExecInfo {
return &CmdExecInfo{
Id: NewId(),
BeginTime: time.Now(),
Cost: -1,
}
}
// LogMetrics implements IBaseConnection.LogMetrics.
func (c *BaseConnection) LogMetrics(category string, cmd *CmdExecInfo) error {
if cmd != nil && c.metricsLogger != nil {
return c.metricsLogger.Put(category, cmd)
}
return nil
}
// Metrics implements IBaseConnection.Metrics.
func (c *BaseConnection) Metrics(category string, opts ...MetricsOpts) (*Metrics, error) {
if c.metricsLogger != nil {
return c.metricsLogger.Metrics(category, opts...)
}
return nil, nil
}