Repository navigation
Expand file tree
/
Copy pathorchestrator.go
More file actions
256 lines (240 loc) · 6.34 KB
/
Copy pathorchestrator.go
File metadata and controls
256 lines (240 loc) · 6.34 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package replication
import (
"bufio"
"errors"
"fmt"
"io"
"net"
"strconv"
"strings"
"time"
)
// RESPClient is a minimal RESP2 client covering the few commands the
// replication scenario needs: PING, SET, GET, INFO, and FLUSHALL.
type RESPClient struct {
conn net.Conn
r *bufio.Reader
w *bufio.Writer
}
// Dial opens a TCP connection to host:port with the given connect timeout.
// Command timeouts are set per call.
func Dial(host string, port int, timeout time.Duration) (*RESPClient, error) {
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", host, port), timeout)
if err != nil {
return nil, fmt.Errorf("dial %s:%d: %w", host, port, err)
}
return newRESPFromConn(conn), nil
}
// Tunneler is the minimal surface DialVia needs from an SSH client.
// Implemented by *remote.SSHClient.
type Tunneler interface {
DialTunnel(network, addr string) (net.Conn, error)
}
// DialVia opens a TCP connection to host:port from the remote side of t,
// tunneling RESP over SSH. Used when the target listens on a private VPC IP.
// host is resolved on the remote side, so "127.0.0.1" targets the instance
// t is connected to.
func DialVia(t Tunneler, host string, port int, timeout time.Duration) (*RESPClient, error) {
// The ssh package has no Dial-with-timeout, so race the dial
// against a timer.
type dialResult struct {
conn net.Conn
err error
}
ch := make(chan dialResult, 1)
go func() {
c, err := t.DialTunnel("tcp", fmt.Sprintf("%s:%d", host, port))
ch <- dialResult{conn: c, err: err}
}()
select {
case r := <-ch:
if r.err != nil {
return nil, fmt.Errorf("tunnel dial %s:%d: %w", host, port, r.err)
}
return newRESPFromConn(r.conn), nil
case <-time.After(timeout):
// Drain a late result so the conn isn't leaked.
go func() {
if r := <-ch; r.conn != nil {
r.conn.Close()
}
}()
return nil, fmt.Errorf("tunnel dial %s:%d: timeout after %s", host, port, timeout)
}
}
func newRESPFromConn(conn net.Conn) *RESPClient {
return &RESPClient{
conn: conn,
r: bufio.NewReader(conn),
w: bufio.NewWriter(conn),
}
}
func (c *RESPClient) Close() error {
if c == nil || c.conn == nil {
return nil
}
return c.conn.Close()
}
// Ping issues a PING and verifies the PONG reply.
func (c *RESPClient) Ping(timeout time.Duration) error {
reply, err := c.Do(timeout, "PING")
if err != nil {
return err
}
if s, ok := reply.(string); !ok || strings.ToUpper(s) != "PONG" {
return fmt.Errorf("unexpected PING reply: %v", reply)
}
return nil
}
// Set issues SET key value.
func (c *RESPClient) Set(timeout time.Duration, key, value string) error {
_, err := c.Do(timeout, "SET", key, value)
return err
}
// Get returns the string value of key, or ("", false, nil) if the key
// does not exist.
func (c *RESPClient) Get(timeout time.Duration, key string) (string, bool, error) {
reply, err := c.Do(timeout, "GET", key)
if err != nil {
return "", false, err
}
if reply == nil {
return "", false, nil
}
s, ok := reply.(string)
if !ok {
return "", false, fmt.Errorf("unexpected GET reply type: %T", reply)
}
return s, true, nil
}
// Info returns the raw text of INFO <section>.
func (c *RESPClient) Info(timeout time.Duration, section string) (string, error) {
reply, err := c.Do(timeout, "INFO", section)
if err != nil {
return "", err
}
s, ok := reply.(string)
if !ok {
return "", fmt.Errorf("unexpected INFO reply type: %T", reply)
}
return s, nil
}
// FlushAll empties every keyspace on the server. It uses FLUSHALL ASYNC
// because the synchronous form can block for tens of seconds after a
// heavily loaded run.
func (c *RESPClient) FlushAll(timeout time.Duration) error {
_, err := c.Do(timeout, "FLUSHALL", "ASYNC")
return err
}
// Do sends one command as a RESP array of bulk strings and reads one reply.
func (c *RESPClient) Do(timeout time.Duration, args ...string) (any, error) {
if len(args) == 0 {
return nil, errors.New("Do: no args")
}
if timeout > 0 {
_ = c.conn.SetDeadline(time.Now().Add(timeout))
defer c.conn.SetDeadline(time.Time{})
}
fmt.Fprintf(c.w, "*%d\r\n", len(args))
for _, a := range args {
fmt.Fprintf(c.w, "$%d\r\n%s\r\n", len(a), a)
}
if err := c.w.Flush(); err != nil {
return nil, fmt.Errorf("write: %w", err)
}
return readReply(c.r)
}
func readReply(r *bufio.Reader) (any, error) {
prefix, err := r.ReadByte()
if err != nil {
return nil, fmt.Errorf("read prefix: %w", err)
}
line, err := readCRLFLine(r)
if err != nil {
return nil, fmt.Errorf("read line: %w", err)
}
switch prefix {
case '+':
return line, nil
case '-':
return nil, fmt.Errorf("RESP error: %s", line)
case ':':
n, err := strconv.ParseInt(line, 10, 64)
if err != nil {
return nil, fmt.Errorf("parse int: %w", err)
}
return n, nil
case '$':
n, err := strconv.Atoi(line)
if err != nil {
return nil, fmt.Errorf("parse bulk length: %w", err)
}
if n < 0 {
return nil, nil // nil bulk
}
buf := make([]byte, n)
if _, err := io.ReadFull(r, buf); err != nil {
return nil, fmt.Errorf("read bulk body: %w", err)
}
// Trailing CRLF.
if _, err := r.Discard(2); err != nil {
return nil, fmt.Errorf("discard trailing crlf: %w", err)
}
return string(buf), nil
case '*':
n, err := strconv.Atoi(line)
if err != nil {
return nil, fmt.Errorf("parse array length: %w", err)
}
if n < 0 {
return nil, nil
}
items := make([]any, 0, n)
for i := 0; i < n; i++ {
v, err := readReply(r)
if err != nil {
return nil, err
}
items = append(items, v)
}
return items, nil
}
return nil, fmt.Errorf("unknown RESP prefix %q", prefix)
}
func readCRLFLine(r *bufio.Reader) (string, error) {
var b strings.Builder
for {
ch, err := r.ReadByte()
if err != nil {
return "", err
}
if ch == '\r' {
next, err := r.ReadByte()
if err != nil {
return "", err
}
if next == '\n' {
return b.String(), nil
}
b.WriteByte(ch)
b.WriteByte(next)
continue
}
b.WriteByte(ch)
}
}
// ConnectedSlaves parses the "connected_slaves:N" line from an INFO
// replication payload. It returns 0 if the field is absent.
func ConnectedSlaves(infoReplication string) int {
for _, line := range strings.Split(infoReplication, "\n") {
line = strings.TrimRight(line, "\r")
if strings.HasPrefix(line, "connected_slaves:") {
n, err := strconv.Atoi(strings.TrimPrefix(line, "connected_slaves:"))
if err != nil {
return 0
}
return n
}
}
return 0
}