forked from alicebob/miniredis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
519 lines (443 loc) · 9.76 KB
/
server.go
File metadata and controls
519 lines (443 loc) · 9.76 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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
package server
import (
"bufio"
"crypto/tls"
"fmt"
"net"
"strings"
"sync"
"unicode"
"github.com/alicebob/miniredis/v2/fpconv"
)
func errUnknownCommand(cmd string, args []string) string {
s := fmt.Sprintf("ERR unknown command `%s`, with args beginning with: ", cmd)
if len(args) > 20 {
args = args[:20]
}
for _, a := range args {
s += fmt.Sprintf("`%s`, ", a)
}
return s
}
// Cmd is what Register expects
type Cmd func(c *Peer, cmd string, args []string)
type DisconnectHandler func(c *Peer)
// Hook is can be added to run before every cmd. Return true if the command is done.
type Hook func(*Peer, string, ...string) bool
// Server is a simple redis server
type Server struct {
l net.Listener
cmds map[string]*cmdMeta
preHook Hook
peers map[net.Conn]struct{}
mu sync.Mutex
wg sync.WaitGroup
infoConns int
infoCmds int
}
// NewServer makes a server listening on addr. Close with .Close().
func NewServer(addr string) (*Server, error) {
l, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
}
return newServer(l), nil
}
func NewServerTLS(addr string, cfg *tls.Config) (*Server, error) {
l, err := tls.Listen("tcp", addr, cfg)
if err != nil {
return nil, err
}
return newServer(l), nil
}
func newServer(l net.Listener) *Server {
s := Server{
cmds: map[string]*cmdMeta{},
peers: map[net.Conn]struct{}{},
l: l,
}
s.wg.Add(1)
go func() {
defer s.wg.Done()
s.serve(l)
s.mu.Lock()
for c := range s.peers {
c.Close()
}
s.mu.Unlock()
}()
return &s
}
// (un)set a hook which is ran before every call. It returns true if the command is done.
func (s *Server) SetPreHook(h Hook) {
s.mu.Lock()
s.preHook = h
s.mu.Unlock()
}
func (s *Server) serve(l net.Listener) {
for {
conn, err := l.Accept()
if err != nil {
return
}
s.ServeConn(conn)
}
}
// ServeConn handles a net.Conn. Nice with net.Pipe()
func (s *Server) ServeConn(conn net.Conn) {
s.wg.Add(1)
s.mu.Lock()
s.peers[conn] = struct{}{}
s.infoConns++
s.mu.Unlock()
go func() {
defer s.wg.Done()
defer conn.Close()
s.servePeer(conn)
s.mu.Lock()
delete(s.peers, conn)
s.mu.Unlock()
}()
}
// Addr has the net.Addr struct
func (s *Server) Addr() *net.TCPAddr {
s.mu.Lock()
defer s.mu.Unlock()
if s.l == nil {
return nil
}
return s.l.Addr().(*net.TCPAddr)
}
// Close a server started with NewServer. It will wait until all clients are
// closed.
func (s *Server) Close() {
s.mu.Lock()
if s.l != nil {
s.l.Close()
}
s.l = nil
s.mu.Unlock()
s.wg.Wait()
}
// Register a command. It can't have been registered before. Safe to call on a
// running server.
func (s *Server) Register(cmd string, f Cmd, options ...CmdOption) error {
s.mu.Lock()
defer s.mu.Unlock()
cmd = strings.ToUpper(cmd)
if _, ok := s.cmds[cmd]; ok {
return fmt.Errorf("command already registered: %s", cmd)
}
meta := &cmdMeta{
handler: f,
readOnly: false,
}
for _, option := range options {
option(meta)
}
s.cmds[cmd] = meta
return nil
}
func (s *Server) servePeer(c net.Conn) {
r := bufio.NewReader(c)
peer := &Peer{
w: bufio.NewWriter(c),
}
defer func() {
for _, f := range peer.onDisconnect {
f()
}
}()
readCh := make(chan []string)
go func() {
defer close(readCh)
for {
args, err := readArray(r)
if err != nil {
peer.Close()
return
}
readCh <- args
}
}()
for args := range readCh {
s.Dispatch(peer, args)
peer.Flush()
if peer.Closed() {
c.Close()
}
}
}
func (s *Server) Dispatch(c *Peer, args []string) {
cmd, args := args[0], args[1:]
cmdUp := strings.ToUpper(cmd)
s.mu.Lock()
h := s.preHook
s.mu.Unlock()
if h != nil {
if h(c, cmdUp, args...) {
return
}
}
s.mu.Lock()
cmdMeta, ok := s.cmds[cmdUp]
s.mu.Unlock()
if !ok {
c.WriteError(errUnknownCommand(cmd, args))
return
}
s.mu.Lock()
s.infoCmds++
s.mu.Unlock()
cmdMeta.handler(c, cmdUp, args)
if c.SwitchResp3 != nil {
c.Resp3 = *c.SwitchResp3
c.SwitchResp3 = nil
}
}
// TotalCommands is total (known) commands since this the server started
func (s *Server) TotalCommands() int {
s.mu.Lock()
defer s.mu.Unlock()
return s.infoCmds
}
// IsRegisteredCommand checks if a command is registered
func (s *Server) IsRegisteredCommand(cmd string) bool {
s.mu.Lock()
defer s.mu.Unlock()
cmdUp := strings.ToUpper(cmd)
_, ok := s.cmds[cmdUp]
return ok
}
// IsReadOnlyCommand checks if a command is marked as read-only
func (s *Server) IsReadOnlyCommand(cmd string) bool {
s.mu.Lock()
defer s.mu.Unlock()
cmdUp := strings.ToUpper(cmd)
if cmdMeta, ok := s.cmds[cmdUp]; ok {
return cmdMeta.readOnly
}
return false
}
// ClientsLen gives the number of connected clients right now
func (s *Server) ClientsLen() int {
s.mu.Lock()
defer s.mu.Unlock()
return len(s.peers)
}
// TotalConnections give the number of clients connected since the server
// started, including the currently connected ones
func (s *Server) TotalConnections() int {
s.mu.Lock()
defer s.mu.Unlock()
return s.infoConns
}
// Peer is a client connected to the server
type Peer struct {
w *bufio.Writer
closed bool
Resp3 bool
SwitchResp3 *bool // we'll switch to this version _after_ the command
Ctx interface{} // anything goes, server won't touch this
onDisconnect []func() // list of callbacks
mu sync.Mutex // for Block()
ClientName string // client name set by CLIENT SETNAME
}
func NewPeer(w *bufio.Writer) *Peer {
return &Peer{
w: w,
}
}
// Flush the write buffer. Called automatically after every redis command
func (c *Peer) Flush() {
c.mu.Lock()
defer c.mu.Unlock()
c.w.Flush()
}
// Close the client connection after the current command is done.
func (c *Peer) Close() {
c.mu.Lock()
defer c.mu.Unlock()
c.closed = true
}
// Return true if the peer connection closed.
func (c *Peer) Closed() bool {
c.mu.Lock()
defer c.mu.Unlock()
return c.closed
}
// Register a function to execute on disconnect. There can be multiple
// functions registered.
func (c *Peer) OnDisconnect(f func()) {
c.onDisconnect = append(c.onDisconnect, f)
}
// issue multiple calls, guarded with a mutex
func (c *Peer) Block(f func(*Writer)) {
c.mu.Lock()
defer c.mu.Unlock()
f(&Writer{c.w, c.Resp3})
}
// WriteError writes a redis 'Error'
func (c *Peer) WriteError(e string) {
c.Block(func(w *Writer) {
w.WriteError(e)
})
}
// WriteInline writes a redis inline string
func (c *Peer) WriteInline(s string) {
c.Block(func(w *Writer) {
w.WriteInline(s)
})
}
// WriteOK write the inline string `OK`
func (c *Peer) WriteOK() {
c.WriteInline("OK")
}
// WriteBulk writes a bulk string
func (c *Peer) WriteBulk(s string) {
c.Block(func(w *Writer) {
w.WriteBulk(s)
})
}
// WriteNull writes a redis Null element
func (c *Peer) WriteNull() {
c.Block(func(w *Writer) {
w.WriteNull()
})
}
// WriteLen starts an array with the given length
func (c *Peer) WriteLen(n int) {
c.Block(func(w *Writer) {
w.WriteLen(n)
})
}
// WriteMapLen starts a map with the given length (number of keys)
func (c *Peer) WriteMapLen(n int) {
c.Block(func(w *Writer) {
w.WriteMapLen(n)
})
}
// WriteSetLen starts a set with the given length (number of elements)
func (c *Peer) WriteSetLen(n int) {
c.Block(func(w *Writer) {
w.WriteSetLen(n)
})
}
// WritePushLen starts a push-data array with the given length
func (c *Peer) WritePushLen(n int) {
c.Block(func(w *Writer) {
w.WritePushLen(n)
})
}
// WriteInt writes an integer
func (c *Peer) WriteInt(n int) {
c.Block(func(w *Writer) {
w.WriteInt(n)
})
}
// WriteFloat writes a float
func (c *Peer) WriteFloat(n float64) {
c.Block(func(w *Writer) {
w.WriteFloat(n)
})
}
// WriteRaw writes a raw redis response
func (c *Peer) WriteRaw(s string) {
c.Block(func(w *Writer) {
w.WriteRaw(s)
})
}
// WriteStrings is a helper to (bulk)write a string list
func (c *Peer) WriteStrings(strs []string) {
c.Block(func(w *Writer) {
w.WriteStrings(strs)
})
}
func toInline(s string) string {
return strings.Map(func(r rune) rune {
if unicode.IsSpace(r) {
return ' '
}
return r
}, s)
}
// A Writer is given to the callback in Block()
type Writer struct {
w *bufio.Writer
resp3 bool
}
// WriteError writes a redis 'Error'
func (w *Writer) WriteError(e string) {
fmt.Fprintf(w.w, "-%s\r\n", toInline(e))
}
func (w *Writer) WriteLen(n int) {
fmt.Fprintf(w.w, "*%d\r\n", n)
}
func (w *Writer) WriteMapLen(n int) {
if w.resp3 {
fmt.Fprintf(w.w, "%%%d\r\n", n)
return
}
w.WriteLen(n * 2)
}
func (w *Writer) WriteSetLen(n int) {
if w.resp3 {
fmt.Fprintf(w.w, "~%d\r\n", n)
return
}
w.WriteLen(n)
}
func (w *Writer) WritePushLen(n int) {
if w.resp3 {
fmt.Fprintf(w.w, ">%d\r\n", n)
return
}
w.WriteLen(n)
}
// WriteBulk writes a bulk string
func (w *Writer) WriteBulk(s string) {
fmt.Fprintf(w.w, "$%d\r\n%s\r\n", len(s), s)
}
// WriteStrings writes a list of strings (bulk)
func (w *Writer) WriteStrings(strs []string) {
w.WriteLen(len(strs))
for _, s := range strs {
w.WriteBulk(s)
}
}
// WriteInt writes an integer
func (w *Writer) WriteInt(n int) {
fmt.Fprintf(w.w, ":%d\r\n", n)
}
// WriteFloat writes a float
func (w *Writer) WriteFloat(n float64) {
if w.resp3 {
fmt.Fprintf(w.w, ",%s\r\n", formatFloat(n))
return
}
w.WriteBulk(formatFloat(n))
}
// WriteNull writes a redis Null element
func (w *Writer) WriteNull() {
if w.resp3 {
fmt.Fprint(w.w, "_\r\n")
return
}
fmt.Fprintf(w.w, "$-1\r\n")
}
// WriteInline writes a redis inline string
func (w *Writer) WriteInline(s string) {
fmt.Fprintf(w.w, "+%s\r\n", toInline(s))
}
// WriteRaw writes a raw redis response
func (w *Writer) WriteRaw(s string) {
fmt.Fprint(w.w, s)
}
func (w *Writer) Flush() {
w.w.Flush()
}
// formatFloat formats a float the way redis does.
// Redis uses a method called "grisu2", which we ported from C.
func formatFloat(v float64) string {
return fpconv.Dtoa(v)
}