forked from gallir/smart-relayer
/
main.go
285 lines (238 loc) · 5.37 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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package rsqs
import (
"errors"
"log"
"net"
"strings"
"sync"
"time"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/gallir/radix.improved/redis"
"github.com/gallir/smart-relayer/lib"
)
// Server is the thread that listen for clients' connections
type Server struct {
sync.Mutex
config lib.RelayerConfig
done chan bool
exiting bool
reseting bool
failing bool
listener net.Listener
tries int
mode int
clients []*Client
recordsCh chan *lib.InterRecord
syncRecordCh chan *syncRecord
awsSvc *sqs.SQS
lastConnection time.Time
lastError time.Time
errors int64
fifo bool
}
type syncRecord struct {
r *lib.InterRecord
syncCh chan bool
}
const (
maxConnections = 1
requestBufferSize = 10 * 2
maxConnectionsTries = 3
connectionRetry = 5 * time.Second
errorsFrame = 10 * time.Second
maxErrors = 10 // Limit of errors to restart the connection
connectTimeout = 5 * time.Second
)
var (
errBadCmd = errors.New("ERR bad command")
errKO = errors.New("fatal error")
errOverloaded = errors.New("Redis overloaded")
respOK = redis.NewRespSimple("OK")
respTrue = redis.NewResp(1)
respBadCommand = redis.NewResp(errBadCmd)
respKO = redis.NewResp(errKO)
commands map[string]*redis.Resp
)
func init() {
commands = map[string]*redis.Resp{
"PING": respOK,
"SET": respOK,
"SADD": respOK,
"HMSET": respOK,
"RAWSET": respOK,
}
}
// New creates a new Redis local server
func New(c lib.RelayerConfig, done chan bool) (*Server, error) {
srv := &Server{
done: done,
errors: 0,
recordsCh: make(chan *lib.InterRecord, requestBufferSize),
syncRecordCh: make(chan *syncRecord, requestBufferSize),
}
srv.Reload(&c)
return srv, nil
}
// Reload the configuration
func (srv *Server) Reload(c *lib.RelayerConfig) (err error) {
srv.Lock()
defer srv.Unlock()
srv.config = *c
if srv.config.MaxConnections <= 0 {
srv.config.MaxConnections = maxConnections
}
if srv.config.MaxRecords <= 0 {
srv.config.MaxRecords = maxBatchRecords
}
if strings.HasSuffix(srv.config.URL, "fifo") {
srv.fifo = true
if srv.config.GroupID == "" {
srv.config.GroupID = srv.config.ListenHost()
}
} else {
srv.fifo = false
srv.config.GroupID = ""
}
srv.mode = c.Type()
go srv.retry()
return nil
}
// Start accepts incoming connections on the Listener
func (srv *Server) Start() (e error) {
srv.Lock()
defer srv.Unlock()
srv.listener, e = lib.NewListener(srv.config)
if e != nil {
return e
}
// Serve clients
go func(l net.Listener) {
defer srv.listener.Close()
for {
netConn, e := l.Accept()
if e != nil {
if netErr, ok := e.(net.Error); ok && netErr.Timeout() {
// Paranoid, ignore timeout errors
log.Println("SQS ERROR: timeout at local listener", srv.config.ListenHost(), e)
continue
}
if srv.exiting {
log.Println("SQS: exiting local listener", srv.config.ListenHost())
return
}
log.Fatalln("SQS ERROR: emergency error in local listener", srv.config.ListenHost(), e)
return
}
go srv.handleConnection(netConn)
}
}(srv.listener)
return
}
// Exit closes the listener and send done to main
func (srv *Server) Exit() {
srv.exiting = true
if srv.listener != nil {
srv.listener.Close()
}
for _, c := range srv.clients {
c.Exit()
}
if len(srv.recordsCh) > 0 {
log.Printf("SQS: messages lost %d", len(srv.recordsCh))
}
// finishing the server
srv.done <- true
}
func (srv *Server) canSend() bool {
if srv.reseting || srv.exiting || srv.failing {
return false
}
return true
}
func (srv *Server) handleConnection(netCon net.Conn) {
defer netCon.Close()
reader := redis.NewRespReader(netCon)
syncConn := &syncRecord{
syncCh: make(chan bool),
r: lib.NewInterRecord(),
}
defer func() {
close(syncConn.syncCh)
}()
for {
r := reader.Read()
if r.IsType(redis.IOErr) {
if redis.IsTimeout(r) {
// Paranoid, don't close it just log it
continue
}
// Connection was closed
return
}
if !srv.canSend() {
respKO.WriteTo(netCon)
return
}
req := lib.NewRequest(r, &srv.config)
if req == nil {
respBadCommand.WriteTo(netCon)
continue
}
fastResponse, ok := commands[req.Command]
if !ok {
respBadCommand.WriteTo(netCon)
continue
}
switch req.Command {
case "PING":
fastResponse.WriteTo(netCon)
continue
case "RAWSET":
if len(req.Items) > 2 {
respKO.WriteTo(netCon)
continue
}
src, _ := req.Items[1].Bytes()
syncConn.r.Types = 1
syncConn.r.Raw = src
case "SET":
k, _ := req.Items[1].Str()
v, _ := req.Items[2].Str()
syncConn.r.Add(k, v)
case "SADD":
k, _ := req.Items[1].Str()
v, _ := req.Items[2].Str()
syncConn.r.Sadd(k, v)
case "HMSET":
var key string
var k string
var v string
for i, o := range req.Items[1:] {
if i == 0 {
key, _ = o.Str()
continue
}
// Now odd elements are the keys
if i%2 != 0 {
k, _ = o.Str()
} else {
v, _ = o.Str()
syncConn.r.Mhset(key, k, v)
}
}
}
// Smart mode, answer immediately and forget
if srv.mode == lib.ModeSmart {
srv.recordsCh <- syncConn.r
fastResponse.WriteTo(netCon)
continue
}
srv.syncRecordCh <- syncConn
b := <-syncConn.syncCh
if b == false {
respKO.WriteTo(netCon)
} else {
fastResponse.WriteTo(netCon)
}
}
}