forked from distributedio/titan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.go
74 lines (65 loc) · 1.73 KB
/
connection.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
package command
import (
"strconv"
"github.com/meitu/titan/encoding/resp"
"github.com/meitu/titan/metrics"
)
// Auth verifies the client
func Auth(ctx *Context) {
args := ctx.Args
serverauth := []byte(ctx.Server.RequirePass)
if len(serverauth) == 0 {
resp.ReplyError(ctx.Out, "ERR Client sent AUTH, but no password is set")
return
}
token := []byte(args[0])
namespace, err := Verify(token, serverauth)
if err != nil {
resp.ReplyError(ctx.Out, "ERR invalid password")
return
}
metrics.GetMetrics().ConnectionOnlineGaugeVec.WithLabelValues(ctx.Client.Namespace).Dec()
metrics.GetMetrics().ConnectionOnlineGaugeVec.WithLabelValues(string(namespace)).Inc()
ctx.Client.Namespace = string(namespace)
ctx.Client.DB.Namespace = string(namespace)
ctx.Client.Authenticated = true
resp.ReplySimpleString(ctx.Out, OK)
}
// Echo the given string
func Echo(ctx *Context) {
resp.ReplyBulkString(ctx.Out, ctx.Args[0])
}
// Ping the server
func Ping(ctx *Context) {
args := ctx.Args
if len(args) > 0 {
resp.ReplyBulkString(ctx.Out, args[0])
return
}
resp.ReplyBulkString(ctx.Out, "PONG")
}
// Select the logical database
func Select(ctx *Context) {
args := ctx.Args
idx, err := strconv.Atoi(args[0])
if err != nil {
resp.ReplyError(ctx.Out, "ERR invalid DB index")
return
}
if idx < 0 || idx > 255 {
resp.ReplyError(ctx.Out, "ERR invalid DB index")
return
}
namespace := ctx.Client.Namespace
ctx.Client.DB = ctx.Server.Store.DB(namespace, idx)
resp.ReplySimpleString(ctx.Out, OK)
}
// Quit asks the server to close the connection
func Quit(ctx *Context) {
close(ctx.Client.Done)
resp.ReplySimpleString(ctx.Out, OK)
}
// SwapDB swaps two Redis databases
func SwapDB(ctx *Context) {
resp.ReplyError(ctx.Out, "ERR not supported")
}