forked from DiceDB/dicedb-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
190 lines (157 loc) · 3.44 KB
/
main.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
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
package dicedb
import (
"fmt"
"io"
"net"
"strings"
"sync"
"syscall"
"time"
"github.com/dicedb/dicedb-go/ironhawk"
"github.com/dicedb/dicedb-go/wire"
"github.com/google/uuid"
)
var mu sync.Mutex
type Client struct {
id string
conn net.Conn
watchConn net.Conn
watchCh chan *wire.Result
host string
port int
}
type option func(*Client)
func newConn(host string, port int) (net.Conn, error) {
addr := fmt.Sprintf("%s:%d", host, port)
conn, err := net.DialTimeout("tcp", addr, 5*time.Second)
if err != nil {
return nil, err
}
return conn, nil
}
func WithID(id string) option {
return func(c *Client) {
c.id = id
}
}
func NewClient(host string, port int, opts ...option) (*Client, error) {
conn, err := newConn(host, port)
if err != nil {
return nil, err
}
client := &Client{conn: conn, host: host, port: port}
for _, opt := range opts {
opt(client)
}
if client.id == "" {
client.id = uuid.New().String()
}
if resp := client.Fire(&wire.Command{
Cmd: "HANDSHAKE",
Args: []string{client.id, "command"},
}); resp.Status == wire.Status_ERR {
return nil, fmt.Errorf("could not complete the handshake: %s", resp.Message)
}
return client, nil
}
func (c *Client) fire(cmd *wire.Command, co net.Conn) *wire.Result {
if err := ironhawk.Write(co, cmd); err != nil {
return &wire.Result{
Status: wire.Status_ERR,
Message: err.Error(),
}
}
resp, err := ironhawk.Read(co)
if err != nil {
return &wire.Result{
Status: wire.Status_ERR,
Message: err.Error(),
}
}
return resp
}
func (c *Client) Fire(cmd *wire.Command) *wire.Result {
result := c.fire(cmd, c.conn)
if result.Status == wire.Status_ERR {
if c.checkAndReconnect(result.Message) {
return c.Fire(cmd)
}
}
return result
}
func (c *Client) checkAndReconnect(err string) bool {
if err == io.EOF.Error() || strings.Contains(err, syscall.EPIPE.Error()) {
fmt.Println("reconnecting ...")
nc, err := getOrCreateClient(c)
if err != nil {
fmt.Println("failed to reconnect. error:", err)
return false
}
*c = *nc
return true
}
return false
}
func getOrCreateClient(c *Client) (*Client, error) {
mu.Lock()
defer mu.Unlock()
if c == nil {
return NewClient(c.host, c.port)
}
newClient, err := NewClient(c.host, c.port)
if err != nil {
return nil, err
}
if c.conn != nil {
c.conn.Close()
}
return newClient, nil
}
func (c *Client) FireString(cmdStr string) *wire.Result {
cmdStr = strings.TrimSpace(cmdStr)
tokens := strings.Split(cmdStr, " ")
var args []string
var cmd = tokens[0]
if len(tokens) > 1 {
args = tokens[1:]
}
return c.Fire(&wire.Command{
Cmd: cmd,
Args: args,
})
}
func (c *Client) WatchCh() (<-chan *wire.Result, error) {
var err error
if c.watchCh != nil {
return c.watchCh, nil
}
c.watchCh = make(chan *wire.Result)
c.watchConn, err = newConn(c.host, c.port)
if err != nil {
return nil, err
}
if resp := c.fire(&wire.Command{
Cmd: "HANDSHAKE",
Args: []string{c.id, "watch"},
}, c.watchConn); resp.Status == wire.Status_ERR {
return nil, fmt.Errorf("could not complete the handshake: %s", resp.Message)
}
go c.watch()
return c.watchCh, nil
}
func (c *Client) watch() {
for {
resp, err := ironhawk.Read(c.watchConn)
if err != nil {
// TODO: handle this better
// send the error to the user. maybe through context?
if !c.checkAndReconnect(err.Error()) {
panic(err)
}
}
c.watchCh <- resp
}
}
func (c *Client) Close() {
c.conn.Close()
}