-
Notifications
You must be signed in to change notification settings - Fork 68
/
pool.go
205 lines (178 loc) · 5.19 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package client
import (
"context"
"fmt"
"io"
"sync"
"time"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/weaveworks/common/user"
"google.golang.org/grpc/health/grpc_health_v1"
"github.com/grafana/dskit/ring/util"
"github.com/grafana/dskit/services"
)
// PoolClient is the interface that should be implemented by a
// client managed by the pool.
type PoolClient interface {
grpc_health_v1.HealthClient
io.Closer
}
// PoolFactory defines the signature for a client factory.
type PoolFactory func(addr string) (PoolClient, error)
// PoolServiceDiscovery defines the signature of a function returning the list
// of known service endpoints. This function is used to remove stale clients from
// the pool (a stale client is a client connected to a service endpoint no more
// active).
type PoolServiceDiscovery func() ([]string, error)
// PoolConfig is config for creating a Pool.
type PoolConfig struct {
CheckInterval time.Duration
HealthCheckEnabled bool
HealthCheckTimeout time.Duration
}
// Pool holds a cache of grpc_health_v1 clients.
type Pool struct {
services.Service
cfg PoolConfig
discovery PoolServiceDiscovery
factory PoolFactory
logger log.Logger
clientName string
sync.RWMutex
clients map[string]PoolClient
clientsMetric prometheus.Gauge
}
// NewPool creates a new Pool.
func NewPool(clientName string, cfg PoolConfig, discovery PoolServiceDiscovery, factory PoolFactory, clientsMetric prometheus.Gauge, logger log.Logger) *Pool {
p := &Pool{
cfg: cfg,
discovery: discovery,
factory: factory,
logger: logger,
clientName: clientName,
clients: map[string]PoolClient{},
clientsMetric: clientsMetric,
}
p.Service = services.
NewTimerService(cfg.CheckInterval, nil, p.iteration, nil).
WithName(fmt.Sprintf("%s client pool", p.clientName))
return p
}
func (p *Pool) iteration(ctx context.Context) error {
p.removeStaleClients()
if p.cfg.HealthCheckEnabled {
p.cleanUnhealthy()
}
return nil
}
func (p *Pool) fromCache(addr string) (PoolClient, bool) {
p.RLock()
defer p.RUnlock()
client, ok := p.clients[addr]
return client, ok
}
// GetClientFor gets the client for the specified address. If it does not exist it will make a new client
// at that address
func (p *Pool) GetClientFor(addr string) (PoolClient, error) {
client, ok := p.fromCache(addr)
if ok {
return client, nil
}
p.Lock()
defer p.Unlock()
client, ok = p.clients[addr]
if ok {
return client, nil
}
client, err := p.factory(addr)
if err != nil {
return nil, err
}
p.clients[addr] = client
if p.clientsMetric != nil {
p.clientsMetric.Add(1)
}
return client, nil
}
// RemoveClientFor removes the client with the specified address
func (p *Pool) RemoveClientFor(addr string) {
p.Lock()
defer p.Unlock()
client, ok := p.clients[addr]
if ok {
delete(p.clients, addr)
if p.clientsMetric != nil {
p.clientsMetric.Add(-1)
}
// Close in the background since this operation may take awhile and we have a mutex
go func(addr string, closer PoolClient) {
if err := closer.Close(); err != nil {
level.Error(p.logger).Log("msg", fmt.Sprintf("error closing connection to %s", p.clientName), "addr", addr, "err", err)
}
}(addr, client)
}
}
// RegisteredAddresses returns all the service addresses for which there's an active client.
func (p *Pool) RegisteredAddresses() []string {
result := []string{}
p.RLock()
defer p.RUnlock()
for addr := range p.clients {
result = append(result, addr)
}
return result
}
// Count returns how many clients are in the cache
func (p *Pool) Count() int {
p.RLock()
defer p.RUnlock()
return len(p.clients)
}
func (p *Pool) removeStaleClients() {
// Only if service discovery has been configured.
if p.discovery == nil {
return
}
serviceAddrs, err := p.discovery()
if err != nil {
level.Error(p.logger).Log("msg", "error removing stale clients", "err", err)
return
}
for _, addr := range p.RegisteredAddresses() {
if util.StringsContain(serviceAddrs, addr) {
continue
}
level.Info(p.logger).Log("msg", "removing stale client", "addr", addr)
p.RemoveClientFor(addr)
}
}
// cleanUnhealthy loops through all servers and deletes any that fails a healthcheck.
func (p *Pool) cleanUnhealthy() {
for _, addr := range p.RegisteredAddresses() {
client, ok := p.fromCache(addr)
// not ok means someone removed a client between the start of this loop and now
if ok {
err := healthCheck(client, p.cfg.HealthCheckTimeout)
if err != nil {
level.Warn(p.logger).Log("msg", fmt.Sprintf("removing %s failing healthcheck", p.clientName), "addr", addr, "reason", err)
p.RemoveClientFor(addr)
}
}
}
}
// healthCheck will check if the client is still healthy, returning an error if it is not
func healthCheck(client PoolClient, timeout time.Duration) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
ctx = user.InjectOrgID(ctx, "0")
resp, err := client.Check(ctx, &grpc_health_v1.HealthCheckRequest{})
if err != nil {
return err
}
if resp.Status != grpc_health_v1.HealthCheckResponse_SERVING {
return fmt.Errorf("failing healthcheck status: %s", resp.Status)
}
return nil
}