forked from tidwall/evio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
179 lines (173 loc) · 4.4 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
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"strings"
"github.com/tidwall/redcon"
"github.com/tidwall/shiny"
)
func main() {
var port int
var appendonly string
flag.IntVar(&port, "port", 6380, "server port")
flag.StringVar(&appendonly, "appendonly", "no", "use appendonly file (yes or no)")
flag.Parse()
var resp []byte
var aofw []byte
var args [][]byte
var shutdown bool
var started bool
var bufs = make(map[int][]byte)
var keys = make(map[string]string)
var f *os.File
if appendonly == "yes" {
var err error
f, err = os.OpenFile("appendonly.aof", os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
log.Fatal(err)
}
defer f.Close()
rd := redcon.NewReader(f)
for {
cmd, err := rd.ReadCommand()
if err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
switch strings.ToUpper(string(cmd.Args[0])) {
default:
log.Fatal("bad aof")
case "SET":
keys[string(cmd.Args[1])] = string(cmd.Args[2])
}
}
}
log.Fatal(shiny.Serve("tcp", fmt.Sprintf(":%d", port),
func(id int, data []byte, ctx interface{}) (send []byte, keepopen bool) {
if shutdown {
return nil, false
}
buf := bufs[id]
if len(buf) > 0 {
data = append(buf, data...)
}
keepopen = true
resp = resp[:0]
aofw = aofw[:0]
var complete bool
var err error
for {
complete, args, _, data, err = redcon.ReadNextCommand(data, args[:0])
if err != nil {
keepopen = false
resp = redcon.AppendError(resp, err.Error())
break
}
if !complete {
break
}
switch strings.ToUpper(string(args[0])) {
default:
resp = redcon.AppendError(resp, fmt.Sprintf("ERR unknown command '%s'", args[0]))
case "PING":
if len(args) > 2 {
resp = redcon.AppendError(resp, fmt.Sprintf("ERR wrong number of arguments for '%s' command", args[0]))
continue
} else if len(args) == 1 {
resp = redcon.AppendString(resp, "PONG")
} else {
resp = redcon.AppendBulk(resp, args[1])
}
case "QUIT":
keepopen = false
resp = redcon.AppendOK(resp)
case "SHUTDOWN":
shutdown = true
keepopen = false
resp = redcon.AppendOK(resp)
case "SET":
if len(args) != 3 {
resp = redcon.AppendError(resp, fmt.Sprintf("ERR wrong number of arguments for '%s' command", args[0]))
continue
}
keys[string(args[1])] = string(args[2])
resp = redcon.AppendOK(resp)
if appendonly == "yes" {
// create the append only entry
aofw = redcon.AppendArray(aofw, 3)
aofw = redcon.AppendBulkString(aofw, "SET")
aofw = redcon.AppendBulk(aofw, args[1])
aofw = redcon.AppendBulk(aofw, args[2])
}
case "GET":
if len(args) != 2 {
resp = redcon.AppendError(resp, fmt.Sprintf("ERR wrong number of arguments for '%s' command", args[0]))
continue
}
val, ok := keys[string(args[1])]
if !ok {
resp = redcon.AppendNull(resp)
} else {
resp = redcon.AppendBulkString(resp, val)
}
case "DEL":
if len(args) < 2 {
resp = redcon.AppendError(resp, fmt.Sprintf("ERR wrong number of arguments for '%s' command", args[0]))
continue
}
var n int64
for i := 1; i < len(args); i++ {
if _, ok := keys[string(args[i])]; ok {
delete(keys, string(args[i]))
n++
}
}
resp = redcon.AppendInt(resp, n)
}
}
if len(data) > 0 {
bufs[id] = append(buf[:0], data...)
} else if len(buf) > 0 {
bufs[id] = buf[:0]
}
if len(aofw) > 0 {
if _, err := f.Write(aofw); err != nil {
log.Fatal(err)
}
}
return resp, keepopen
},
// accept - a new client socket has opened
func(id int, addr string, wake func(), ctx interface{}) (send []byte, keepopen bool) {
if shutdown {
return nil, false
}
// create a new socket context here
return nil, true
},
// closed - a client socket has closed
func(id int, err error, ctx interface{}) {
// teardown the socket context here
delete(bufs, id)
},
// ticker - a ticker that fires between 1 and 1/20 of a second
// depending on the traffic.
func(ctx interface{}) (keepserveropen bool) {
if shutdown {
// do server teardown here
return false
}
if !started {
fmt.Printf("redis(ish) server started on port %d\n", port)
started = true
}
// perform various non-socket-io related operation here
return true
},
// an optional user-defined context
nil))
}