-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
149 lines (129 loc) · 3.32 KB
/
handler.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
package main
import (
"bufio"
"bytes"
"log"
"net"
"strings"
"github.com/tidwall/redcon"
)
type Handler struct {
config Config
netMode string
}
type HandlerContext struct {
upstreamConn net.Conn
username string
detached bool
}
func NewHandler(config Config) *Handler {
netMode := "tcp"
if strings.HasPrefix(config.UpstreamRedis, "/") || strings.HasPrefix(config.UpstreamRedis, "@") {
netMode = "unix"
}
return &Handler{
config: config,
netMode: netMode,
}
}
func (m *Handler) ServeRESP(conn redcon.Conn, cmd redcon.Command) {
context, ok := conn.Context().(HandlerContext)
if !ok || context.upstreamConn == nil {
conn.Close()
return
}
upConn := context.upstreamConn
command := strings.ToUpper(string(cmd.Args[0]))
newArgs, reviver := modSingleCommand(command, context.username, cmd.Args)
cmd.Args = newArgs
// Construct RESP command & send to redis
request := buildRESPCommand(cmd.Args)
_, err := upConn.Write(request)
if err != nil {
log.Printf("Failed to send command: %v", err)
conn.Close()
return
}
// Read response from Redis
var b bytes.Buffer
reader := newRespReader(bufio.NewReader(upConn), &b, reviver)
if err = reader.ReadReply(); err != nil {
log.Printf("Failed to read response: %v", err)
conn.Close()
return
}
response := b.Bytes()
if len(response) > 0 && response[0] != '-' {
if command == "AUTH" && len(cmd.Args) == 3 {
context.username = string(cmd.Args[1])
conn.SetContext(context)
} else if command == "SUBSCRIBE" || command == "PSUBSCRIBE" {
// this will detach connection from this loop
context.detached = true
conn.SetContext(context)
dconn := conn.Detach()
go m.subscriptionLoop(dconn, upConn)
}
}
// Send response back to client
conn.WriteRaw(response)
}
func (m *Handler) AcceptConn(conn redcon.Conn) bool {
redisConn, err := net.Dial(m.netMode, m.config.UpstreamRedis)
if err != nil {
log.Printf("Failed to connect to Redis: %v", err)
return false
}
conn.SetContext(HandlerContext{
upstreamConn: redisConn,
username: "default", // TODO: Fetch username dynamically
})
return true
}
func (m *Handler) ClosedConn(conn redcon.Conn, err error) {
context, ok := conn.Context().(HandlerContext)
if ok && context.upstreamConn != nil && !context.detached {
context.upstreamConn.Close()
context.upstreamConn = nil
}
}
func (m *Handler) subscriptionLoop(conn redcon.DetachedConn, upConn net.Conn) {
defer conn.Close()
context, ok := conn.Context().(HandlerContext)
if !ok || context.upstreamConn == nil {
conn.Close()
return
}
go (func() {
for {
cmd, err := conn.ReadCommand()
if err != nil {
log.Printf("Failed to read command: %v", err)
conn.Close()
return
}
command := strings.ToUpper(string(cmd.Args[0]))
newArgs, _ := modSingleCommand(command, context.username, cmd.Args)
cmd.Args = newArgs
// Construct RESP command & send to redis
request := buildRESPCommand(cmd.Args)
_, err = upConn.Write(request)
if err != nil {
log.Printf("Failed to send command: %v", err)
conn.Close()
return
}
}
})()
for {
conn.Flush()
var b bytes.Buffer
reader := newRespReader(bufio.NewReader(upConn), &b, nil)
if err := reader.ReadReply(); err != nil {
log.Printf("Error reading subscription response: %v", err)
conn.Close()
return
}
conn.WriteRaw(b.Bytes())
}
}