forked from shadowspore/t38c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
51 lines (41 loc) · 993 Bytes
/
utils.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
package transport
import (
"context"
"encoding/json"
"fmt"
"github.com/mediocregopher/radix/v4"
)
func poolConnFn(password *string) func(ctx context.Context, net, addr string) (radix.Client, error) {
return func(ctx context.Context, net, addr string) (radix.Client, error) {
conn, err := radix.Dial(ctx, net, addr)
if err != nil {
return nil, err
}
if err := radixPrepareConn(ctx, conn, password); err != nil {
_ = conn.Close()
return nil, err
}
return conn, nil
}
}
func radixPrepareConn(ctx context.Context, conn radix.Conn, password *string) error {
if password != nil {
if err := conn.Do(ctx, radix.Cmd(nil, "AUTH", *password)); err != nil {
return err
}
}
var b []byte
if err := conn.Do(ctx, radix.Cmd(&b, "OUTPUT", "json")); err != nil {
return err
}
var resp struct {
Ok bool `json:"ok"`
}
if err := json.Unmarshal(b, &resp); err != nil {
return err
}
if !resp.Ok {
return fmt.Errorf("bad response: %s", b)
}
return nil
}