-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathserver.go
157 lines (128 loc) · 3.06 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
package redeo
import (
"net"
"strings"
"sync"
"time"
"github.com/bsm/redeo/v2/resp"
)
// Server configuration
type Server struct {
config *Config
info *ServerInfo
cmds map[string]interface{}
mu sync.RWMutex
}
// NewServer creates a new server instance
func NewServer(config *Config) *Server {
if config == nil {
config = new(Config)
}
return &Server{
config: config,
info: newServerInfo(),
cmds: make(map[string]interface{}),
}
}
// Info returns the server info registry
func (srv *Server) Info() *ServerInfo { return srv.info }
// Handle registers a handler for a command.
func (srv *Server) Handle(name string, h Handler) {
srv.mu.Lock()
srv.cmds[strings.ToLower(name)] = h
srv.mu.Unlock()
}
// HandleFunc registers a handler func for a command.
func (srv *Server) HandleFunc(name string, fn HandlerFunc) {
srv.Handle(name, fn)
}
// HandleStream registers a handler for a streaming command.
func (srv *Server) HandleStream(name string, h StreamHandler) {
srv.mu.Lock()
srv.cmds[strings.ToLower(name)] = h
srv.mu.Unlock()
}
// HandleStreamFunc registers a handler func for a command
func (srv *Server) HandleStreamFunc(name string, fn StreamHandlerFunc) {
srv.HandleStream(name, fn)
}
// Serve accepts incoming connections on a listener, creating a
// new service goroutine for each.
func (srv *Server) Serve(lis net.Listener) error {
for {
cn, err := lis.Accept()
if err != nil {
return err
}
if ka := srv.config.TCPKeepAlive; ka > 0 {
if tc, ok := cn.(*net.TCPConn); ok {
_ = tc.SetKeepAlive(true)
_ = tc.SetKeepAlivePeriod(ka)
}
}
go srv.serveClient(newClient(cn))
}
}
// Starts a new session, serving client
func (srv *Server) serveClient(c *Client) {
// Release client on exit
defer c.release()
// Register client
srv.info.register(c)
defer srv.info.deregister(c.id)
// Create perform callback
perform := func(name string) error {
return srv.perform(c, name)
}
// Init request/response loop
for !c.closed {
// set deadline
if d := srv.config.Timeout; d > 0 {
_ = c.cn.SetDeadline(time.Now().Add(d))
}
// perform pipeline
if err := c.pipeline(perform); err != nil {
c.wr.AppendError("ERR " + err.Error())
if !resp.IsProtocolError(err) {
_ = c.wr.Flush()
return
}
}
// flush buffer, return on errors
if err := c.wr.Flush(); err != nil {
return
}
}
}
func (srv *Server) perform(c *Client, name string) (err error) {
norm := strings.ToLower(name)
// find handler
srv.mu.RLock()
h, ok := srv.cmds[norm]
srv.mu.RUnlock()
if !ok {
c.wr.AppendError(UnknownCommand(name))
_ = c.rd.SkipCmd()
return
}
// register call
srv.info.command(c.id, norm)
switch handler := h.(type) {
case Handler:
if c.cmd, err = c.readCmd(c.cmd); err != nil {
return
}
handler.ServeRedeo(c.wr, c.cmd)
case StreamHandler:
if c.scmd, err = c.streamCmd(c.scmd); err != nil {
return
}
defer c.scmd.Discard()
handler.ServeRedeoStream(c.wr, c.scmd)
}
// flush when buffer is large enough
if n := c.wr.Buffered(); n > resp.MaxBufferSize/2 {
err = c.wr.Flush()
}
return
}