forked from izumin5210/nrredigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.go
50 lines (40 loc) · 952 Bytes
/
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
package nrredigo
import (
"context"
"github.com/gomodule/redigo/redis"
"github.com/newrelic/go-agent/v3/newrelic"
)
// Pool is an interface for representing a pool of Redis connections
type Pool interface {
GetContext(ctx context.Context) (redis.Conn, error)
Get() redis.Conn
Close() error
}
// Wrap returns a wrappedPool that can be used like a normal redis Pool, but sends segments to new relic
func Wrap(p Pool, opts ...Option) Pool {
return &wrappedPool{
Pool: p,
cfg: createConfig(opts),
}
}
type wrappedPool struct {
Pool
cfg *Config
}
func (p *wrappedPool) GetContext(ctx context.Context) (conn redis.Conn, err error) {
conn, err = p.Pool.GetContext(ctx)
if err != nil {
return
}
nrtx := newrelic.FromContext(ctx)
if nrtx != nil {
return wrapConn(conn, nrtx, p.cfg), nil
}
return
}
func (p *wrappedPool) Get() redis.Conn {
return p.Pool.Get()
}
func (p *wrappedPool) Close() error {
return p.Pool.Close()
}