Skip to content

Commit 1a01c44

Browse files
committed
POC connection hook
1 parent 14d82a2 commit 1a01c44

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

internal/pool/pool.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,23 @@ var timers = sync.Pool{
2727
},
2828
}
2929

30+
type ConnectEvent struct {
31+
Err error
32+
}
33+
34+
type PoolHook interface {
35+
BeforeConnect(ctx context.Context) (context.Context, error)
36+
AfterConnect(ctx context.Context, event ConnectEvent) error
37+
}
38+
39+
type hooks struct {
40+
hooks []PoolHook
41+
}
42+
43+
func (hs *hooks) AddHook(hook PoolHook) {
44+
hs.hooks = append(hs.hooks, hook)
45+
}
46+
3047
// Stats contains pool state information and accumulated stats.
3148
type Stats struct {
3249
Hits uint32 // number of times free connection was found in the pool
@@ -39,6 +56,8 @@ type Stats struct {
3956
}
4057

4158
type Pooler interface {
59+
AddHook(hook PoolHook)
60+
4261
NewConn(context.Context) (*Conn, error)
4362
CloseConn(*Conn) error
4463

@@ -70,6 +89,8 @@ type lastDialErrorWrap struct {
7089
}
7190

7291
type ConnPool struct {
92+
hooks
93+
7394
opt *Options
7495

7596
dialErrorsNum uint32 // atomic

internal/pool/pool_single.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package pool
33
import "context"
44

55
type SingleConnPool struct {
6+
hooks
67
pool Pooler
78
cn *Conn
89
stickyErr error

internal/pool/pool_sticky.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func (e BadConnError) Unwrap() error {
3434
//------------------------------------------------------------------------------
3535

3636
type StickyConnPool struct {
37+
hooks
3738
pool Pooler
3839
shared int32 // atomic
3940

redis.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"sync/atomic"
88
"time"
9+
"unsafe"
910

1011
"github.com/go-redis/redis/v8/internal"
1112
"github.com/go-redis/redis/v8/internal/pool"
@@ -29,6 +30,11 @@ type Hook interface {
2930
AfterProcessPipeline(ctx context.Context, cmds []Cmder) error
3031
}
3132

33+
type fullHook interface {
34+
Hook
35+
pool.PoolHook
36+
}
37+
3238
type hooks struct {
3339
hooks []Hook
3440
}
@@ -45,6 +51,10 @@ func (hs hooks) clone() hooks {
4551

4652
func (hs *hooks) AddHook(hook Hook) {
4753
hs.hooks = append(hs.hooks, hook)
54+
if hook, ok := hook.(fullHook); ok {
55+
client := *(*Client)(unsafe.Pointer(hs))
56+
client.baseClient.connPool.AddHook(hook)
57+
}
4858
}
4959

5060
func (hs hooks) process(

0 commit comments

Comments
 (0)