forked from xuyu/goredis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
358 lines (328 loc) · 10.6 KB
/
server.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
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
package goredis
import (
"errors"
"io"
"net"
"strconv"
)
// BgRewriteAof Instruct Redis to start an Append Only File rewrite process.
// The rewrite will create a small optimized version of the current Append Only File.
func (r *Redis) BgRewriteAof() error {
_, err := r.ExecuteCommand("BGREWRITEAOF")
return err
}
// BgSave save the DB in background.
// The OK code is immediately returned.
// Redis forks, the parent continues to serve the clients, the child saves the DB on disk then exits.
// A client my be able to check if the operation succeeded using the LASTSAVE command.
func (r *Redis) BgSave() error {
_, err := r.ExecuteCommand("BGSAVE")
return err
}
// ClientKill closes a given client connection identified by ip:port.
// Due to the single-treaded nature of Redis,
// it is not possible to kill a client connection while it is executing a command.
// However, the client will notice the connection has been closed
// only when the next command is sent (and results in network error).
// Status code reply: OK if the connection exists and has been closed
func (r *Redis) ClientKill(ip string, port int) error {
rp, err := r.ExecuteCommand("CLIENT", "KILL", net.JoinHostPort(ip, strconv.Itoa(port)))
if err != nil {
return err
}
return rp.OKValue()
}
// ClientList returns information and statistics
// about the client connections server in a mostly human readable format.
// Bulk reply: a unique string, formatted as follows:
// One client connection per line (separated by LF)
// Each line is composed of a succession of property=value fields separated by a space character.
func (r *Redis) ClientList() (string, error) {
rp, err := r.ExecuteCommand("CLIENT", "LIST")
if err != nil {
return "", err
}
return rp.StringValue()
}
// ClientGetName returns the name of the current connection as set by CLIENT SETNAME.
// Since every new connection starts without an associated name,
// if no name was assigned a null bulk reply is returned.
func (r *Redis) ClientGetName() ([]byte, error) {
rp, err := r.ExecuteCommand("CLIENT", "GETNAME")
if err != nil {
return nil, err
}
return rp.BytesValue()
}
// ClientPause stops the server processing commands from clients for some time.
func (r *Redis) ClientPause(timeout uint64) error {
rp, err := r.ExecuteCommand("CLIENT", "PAUSE", timeout)
if err != nil {
return err
}
return rp.OKValue()
}
// ClientSetName assigns a name to the current connection.
func (r *Redis) ClientSetName(name string) error {
rp, err := r.ExecuteCommand("CLIENT", "SETNAME", name)
if err != nil {
return err
}
return rp.OKValue()
}
// ConfigGet is used to read the configuration parameters of a running Redis server.
// Not all the configuration parameters are supported in Redis 2.4,
// while Redis 2.6 can read the whole configuration of a server using this command.
// CONFIG GET takes a single argument, which is a glob-style pattern.
func (r *Redis) ConfigGet(parameter string) (map[string]string, error) {
rp, err := r.ExecuteCommand("CONFIG", "GET", parameter)
if err != nil {
return nil, err
}
return rp.HashValue()
}
// ConfigRewrite rewrites the redis.conf file the server was started with,
// applying the minimal changes needed to make it reflecting the configuration currently used by the server,
// that may be different compared to the original one because of the use of the CONFIG SET command.
// Available since 2.8.0.
func (r *Redis) ConfigRewrite() error {
rp, err := r.ExecuteCommand("CONFIG", "REWRITE")
if err != nil {
return err
}
return rp.OKValue()
}
// ConfigSet is used in order to reconfigure the server at run time without the need to restart Redis.
// You can change both trivial parameters or switch from one to another persistence option using this command.
func (r *Redis) ConfigSet(parameter, value string) error {
rp, err := r.ExecuteCommand("CONFIG", "SET")
if err != nil {
return err
}
return rp.OKValue()
}
// ConfigResetStat resets the statistics reported by Redis using the INFO command.
// These are the counters that are reset:
// Keyspace hits
// Keyspace misses
// Number of commands processed
// Number of connections received
// Number of expired keys
// Number of rejected connections
// Latest fork(2) time
// The aof_delayed_fsync counter
func (r *Redis) ConfigResetStat() error {
_, err := r.ExecuteCommand("CONFIG", "RESETSTAT")
return err
}
// DBSize return the number of keys in the currently-selected database.
func (r *Redis) DBSize() (int64, error) {
rp, err := r.ExecuteCommand("DBSIZE")
if err != nil {
return 0, err
}
return rp.IntegerValue()
}
// DebugObject is a debugging command that should not be used by clients.
func (r *Redis) DebugObject(key string) (string, error) {
rp, err := r.ExecuteCommand("DEBUG", "OBJECT", key)
if err != nil {
return "", err
}
return rp.StatusValue()
}
// FlushAll delete all the keys of all the existing databases,
// not just the currently selected one.
// This command never fails.
func (r *Redis) FlushAll() error {
_, err := r.ExecuteCommand("FLUSHALL")
return err
}
// FlushDB delete all the keys of the currently selected DB.
// This command never fails.
func (r *Redis) FlushDB() error {
_, err := r.ExecuteCommand("FLUSHDB")
return err
}
// Info returns information and statistics about the server
// in a format that is simple to parse by computers and easy to read by humans.
// format document at http://redis.io/commands/info
func (r *Redis) Info(section string) (string, error) {
args := packArgs("INFO", section)
rp, err := r.ExecuteCommand(args...)
if err != nil {
return "", err
}
return rp.StringValue()
}
// LastSave return the UNIX TIME of the last DB save executed with success.
// A client may check if a BGSAVE command succeeded reading the LASTSAVE value,
// then issuing a BGSAVE command and checking at regular intervals every N seconds if LASTSAVE changed.
// Integer reply: an UNIX time stamp.
func (r *Redis) LastSave() (int64, error) {
rp, err := r.ExecuteCommand("LASTSAVE")
if err != nil {
return 0, err
}
return rp.IntegerValue()
}
// MonitorCommand is a debugging command that streams back every command processed by the Redis server.
type MonitorCommand struct {
redis *Redis
conn *connection
}
// Monitor sned MONITOR command to redis server.
func (r *Redis) Monitor() (*MonitorCommand, error) {
c, err := r.pool.Get()
if err != nil {
return nil, err
}
if err := c.SendCommand("MONITOR"); err != nil {
return nil, err
}
rp, err := c.RecvReply()
if err != nil {
return nil, err
}
if err := rp.OKValue(); err != nil {
return nil, err
}
return &MonitorCommand{r, c}, nil
}
// Receive read from redis server and return the reply.
func (m *MonitorCommand) Receive() (string, error) {
rp, err := m.conn.RecvReply()
if err != nil {
return "", err
}
return rp.StatusValue()
}
// Close closes current monitor command.
func (m *MonitorCommand) Close() error {
return m.conn.SendCommand("QUIT")
}
// Save performs a synchronous save of the dataset
// producing a point in time snapshot of all the data inside the Redis instance,
// in the form of an RDB file.
// You almost never want to call SAVE in production environments
// where it will block all the other clients. Instead usually BGSAVE is used.
func (r *Redis) Save() error {
rp, err := r.ExecuteCommand("SAVE")
if err != nil {
return err
}
return rp.OKValue()
}
// Shutdown behavior is the following:
// Stop all the clients.
// Perform a blocking SAVE if at least one save point is configured.
// Flush the Append Only File if AOF is enabled.
// Quit the server.
func (r *Redis) Shutdown(save, noSave bool) error {
args := packArgs("SHUTDOWN")
if save {
args = append(args, "SAVE")
} else if noSave {
args = append(args, "NOSAVE")
}
rp, err := r.ExecuteCommand(args...)
if err == io.EOF {
return nil
}
if err != nil {
return err
}
return errors.New(rp.Status)
}
// SlaveOf can change the replication settings of a slave on the fly.
// If a Redis server is already acting as slave, the command SLAVEOF NO ONE will turn off the replication,
// turning the Redis server into a MASTER.
// In the proper form SLAVEOF hostname port will make the server a slave of
// another server listening at the specified hostname and port.
//
// If a server is already a slave of some master,
// SLAVEOF hostname port will stop the replication against the old server
// and start the synchronization against the new one, discarding the old dataset.
// The form SLAVEOF NO ONE will stop replication, turning the server into a MASTER,
// but will not discard the replication.
// So, if the old master stops working,
// it is possible to turn the slave into a master and set the application to use this new master in read/write.
// Later when the other Redis server is fixed, it can be reconfigured to work as a slave.
func (r *Redis) SlaveOf(host, port string) error {
rp, err := r.ExecuteCommand("SLAVEOF", host, port)
if err != nil {
return err
}
return rp.OKValue()
}
// SlowLog is used in order to read and reset the Redis slow queries log.
type SlowLog struct {
ID int64
Timestamp int64
Microseconds int64
Command []string
}
// SlowLogGet returns slow logs.
func (r *Redis) SlowLogGet(n int) ([]*SlowLog, error) {
rp, err := r.ExecuteCommand("SLOWLOG", "GET", n)
if err != nil {
return nil, err
}
if rp.Type == ErrorReply {
return nil, errors.New(rp.Error)
}
if rp.Type != MultiReply {
return nil, errors.New("slowlog get protocol error")
}
var slow []*SlowLog
for _, subrp := range rp.Multi {
if subrp.Multi == nil || len(subrp.Multi) != 4 {
return nil, errors.New("slowlog get protocol error")
}
id, err := subrp.Multi[0].IntegerValue()
if err != nil {
return nil, err
}
timestamp, err := subrp.Multi[1].IntegerValue()
if err != nil {
return nil, err
}
microseconds, err := subrp.Multi[2].IntegerValue()
if err != nil {
return nil, err
}
command, err := subrp.Multi[3].ListValue()
if err != nil {
return nil, err
}
slow = append(slow, &SlowLog{id, timestamp, microseconds, command})
}
return slow, nil
}
// SlowLogLen Obtaining the current length of the slow log
func (r *Redis) SlowLogLen() (int64, error) {
rp, err := r.ExecuteCommand("SLOWLOG", "LEN")
if err != nil {
return 0, err
}
return rp.IntegerValue()
}
// SlowLogReset resetting the slow log.
// Once deleted the information is lost forever.
func (r *Redis) SlowLogReset() error {
rp, err := r.ExecuteCommand("SLOWLOG", "RESET")
if err != nil {
return err
}
return rp.OKValue()
}
// Time returns a multi bulk reply containing two elements:
// unix time in seconds,
// microseconds.
func (r *Redis) Time() ([]string, error) {
rp, err := r.ExecuteCommand("TIME")
if err != nil {
return nil, err
}
return rp.ListValue()
}