forked from distributedio/titan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
155 lines (137 loc) · 3.2 KB
/
client.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
package titan
import (
"bufio"
"io/ioutil"
"net"
"strings"
"time"
"github.com/meitu/titan/command"
"github.com/meitu/titan/context"
"github.com/meitu/titan/encoding/resp"
"go.uber.org/zap"
)
type client struct {
cliCtx *context.ClientContext
server *Server
conn net.Conn
exec *command.Executor
r *bufio.Reader
}
func newClient(cliCtx *context.ClientContext, s *Server, exec *command.Executor) *client {
return &client{
cliCtx: cliCtx,
server: s,
exec: exec,
}
}
// Write to conn and log error if needed
func (c *client) Write(p []byte) (int, error) {
n, err := c.conn.Write(p)
if err != nil {
zap.L().Error("write net failed", zap.String("addr", c.cliCtx.RemoteAddr),
zap.Int64("clientid", c.cliCtx.ID),
zap.String("namespace", c.cliCtx.Namespace),
zap.Bool("multi", c.cliCtx.Multi),
zap.Bool("watching", c.cliCtx.Txn != nil),
zap.String("command", c.cliCtx.LastCmd))
c.conn.Close()
}
return n, err
}
func (c *client) serve(conn net.Conn) error {
c.conn = conn
c.r = bufio.NewReader(conn)
rootCtx, rootCancel := context.WithCancel(context.New(c.cliCtx, c.server.servCtx))
// Use a separate goroutine to keep reading commands
// then we can detect a closed connection as soon as possible.
// It only works when the cmd channel is not blocked
cmdc := make(chan []string, 128)
errc := make(chan error)
go func() {
for {
cmd, err := c.readCommand()
if err != nil {
errc <- err
rootCancel()
return
}
cmdc <- cmd
}
}()
var cmd []string
var err error
for {
select {
case <-c.cliCtx.Done:
return c.conn.Close()
case cmd = <-cmdc:
case err = <-errc:
zap.L().Error("read command failed", zap.String("addr", c.cliCtx.RemoteAddr),
zap.Int64("clientid", c.cliCtx.ID), zap.Error(err))
c.conn.Close()
return err
}
if c.server.servCtx.Pause > 0 {
time.Sleep(c.server.servCtx.Pause)
c.server.servCtx.Pause = 0
}
c.cliCtx.Updated = time.Now()
c.cliCtx.LastCmd = cmd[0]
ctx := &command.Context{
Name: cmd[0],
Args: cmd[1:],
In: c.r,
Out: c,
TraceID: GenerateTraceID(),
}
ctx.Context = rootCtx
// Skip reply if necessary
if c.cliCtx.SkipN != 0 {
ctx.Out = ioutil.Discard
if c.cliCtx.SkipN > 0 {
c.cliCtx.SkipN--
}
}
if env := zap.L().Check(zap.DebugLevel, "recv client command"); env != nil {
env.Write(zap.String("addr", c.cliCtx.RemoteAddr),
zap.Int64("clientid", c.cliCtx.ID),
zap.String("traceid", ctx.TraceID),
zap.String("command", ctx.Name))
}
c.exec.Execute(ctx)
}
}
func (c *client) readInlineCommand() ([]string, error) {
buf, err := c.r.ReadBytes('\n')
if err != nil {
return nil, err
}
line := strings.TrimRight(string(buf), "\r\n")
return strings.Fields(line), nil
}
func (c *client) readCommand() ([]string, error) {
p, err := c.r.Peek(1)
if err != nil {
return nil, err
}
// not a bulk string
if p[0] != '*' {
return c.readInlineCommand()
}
argc, err := resp.ReadArray(c.r)
if err != nil {
return nil, err
}
if argc == 0 {
return []string{}, nil
}
argv := make([]string, argc)
for i := 0; i < argc; i++ {
arg, err := resp.ReadBulkString(c.r)
if err != nil {
return nil, err
}
argv[i] = arg
}
return argv, nil
}