generated from cybozu-go/neco-template
/
server.go
89 lines (76 loc) · 2.07 KB
/
server.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
package server
import (
"sync"
"time"
"github.com/cybozu-go/moco-agent/metrics"
"github.com/cybozu-go/moco-agent/proto"
"github.com/go-logr/logr"
"github.com/jmoiron/sqlx"
"github.com/prometheus/client_golang/prometheus"
)
// NewAgentService creates a new AgentServer
func NewAgentService(agent *Agent) proto.AgentServer {
return agentService{agent: agent}
}
type agentService struct {
agent *Agent
proto.UnimplementedAgentServer
}
// New returns an Agent
func New(config MySQLAccessorConfig, clusterName, socket, logDir string, maxDelay, transactionQueueingWait time.Duration, logger logr.Logger) (*Agent, error) {
db, err := getMySQLConn(config)
if err != nil {
return nil, err
}
return &Agent{
config: config,
db: db,
logger: logger,
mysqlSocketPath: socket,
logDir: logDir,
maxDelayThreshold: maxDelay,
transactionQueueingWait: transactionQueueingWait,
cloneLock: make(chan struct{}, 1),
}, nil
}
// Agent is the agent to executes some MySQL commands of the own Pod
type Agent struct {
config MySQLAccessorConfig
db *sqlx.DB
logger logr.Logger
mysqlSocketPath string
logDir string
maxDelayThreshold time.Duration
transactionQueueingWait time.Duration
cloneLock chan struct{}
registryLock sync.Mutex
registered bool
}
func (a *Agent) configureReplicationMetrics(enable bool) {
a.registryLock.Lock()
defer a.registryLock.Unlock()
if enable {
if a.registered {
return
}
metrics.RegisterReplicationMetrics(prometheus.DefaultRegisterer)
a.registered = true
return
}
if !a.registered {
return
}
metrics.UnregisterReplicationMetrics(prometheus.DefaultRegisterer)
a.registered = false
}
type MySQLAccessorConfig struct {
Host string
Port int
Password string
ConnMaxIdleTime time.Duration
ConnectionTimeout time.Duration
ReadTimeout time.Duration
}
func (a *Agent) CloseDB() error {
return a.db.Close()
}