This repository has been archived by the owner on Dec 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
radix_pool.go
131 lines (109 loc) · 2.68 KB
/
radix_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
package transport
import (
"context"
"errors"
"fmt"
"sync"
"sync/atomic"
"time"
"github.com/mediocregopher/radix/v3"
"github.com/mediocregopher/radix/v3/resp/resp2"
"github.com/tidwall/gjson"
)
// ErrClosedClient error
var ErrClosedClient = errors.New("closed client")
// RadixPool struct
type RadixPool struct {
addr string
password *string
pool *radix.Pool
wg sync.WaitGroup
closed uint32
}
// NewRadixPool return radix pool dialer with provided pool size.
func NewRadixPool(addr string, size int, password *string) (*RadixPool, error) {
connFn := poolConnFn(password)
pool, err := radix.NewPool("tcp", addr, size,
radix.PoolConnFunc(connFn),
)
if err != nil {
return nil, err
}
return &RadixPool{
addr: addr,
password: password,
pool: pool,
}, nil
}
// Execute command
func (rad *RadixPool) Execute(command string, args ...string) ([]byte, error) {
if rad.isClosed() {
return nil, ErrClosedClient
}
var resp []byte
err := rad.pool.Do(radix.Cmd(&resp, command, args...))
if err != nil {
return nil, err
}
return resp, nil
}
// ExecuteStream used for commands with streaming response.
// Creates a new connection for each stream.
func (rad *RadixPool) ExecuteStream(ctx context.Context, handler func([]byte) error, command string, args ...string) error {
if rad.isClosed() {
return ErrClosedClient
}
rad.wg.Add(1)
defer rad.wg.Done()
conn, err := radix.Dial("tcp", rad.addr,
radix.DialConnectTimeout(time.Second*10),
radix.DialReadTimeout(0),
)
if err != nil {
return err
}
defer conn.Close()
if err := radixPrepareConn(conn, rad.password); err != nil {
return err
}
var resp []byte
if err := conn.Do(radix.Cmd(&resp, command, args...)); err != nil {
return err
}
if !gjson.GetBytes(resp, "ok").Bool() {
return fmt.Errorf(gjson.GetBytes(resp, "err").String())
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
var forcedDisconnect int32
go func() {
<-ctx.Done()
atomic.StoreInt32(&forcedDisconnect, 1)
conn.Close()
}()
for atomic.LoadInt32(&forcedDisconnect) == 0 {
var response resp2.BulkStringBytes
if err := conn.Decode(&response); err != nil {
forced := atomic.LoadInt32(&forcedDisconnect) == 1
if forced {
return nil
}
return fmt.Errorf("resp decode: %v", err)
}
if err := handler(response.B); err != nil {
return err
}
}
return nil
}
func (rad *RadixPool) isClosed() bool {
return atomic.LoadUint32(&rad.closed) == 1
}
// Close closes all connections in the pool and rejects future execution calls.
// Blocks until all streams are closed.
func (rad *RadixPool) Close() error {
atomic.StoreUint32(&rad.closed, 1)
err := rad.pool.Close()
rad.wg.Wait()
return err
}