-
Notifications
You must be signed in to change notification settings - Fork 118
/
client.go
177 lines (154 loc) · 3.76 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package titan
import (
"bufio"
"io"
"io/ioutil"
"net"
"strings"
"sync"
"time"
"github.com/distributedio/titan/command"
"github.com/distributedio/titan/context"
"github.com/distributedio/titan/encoding/resp"
"go.uber.org/zap"
)
type client struct {
cliCtx *context.ClientContext
server *Server
conn net.Conn
exec *command.Executor
r *bufio.Reader
eofLock sync.Mutex //the lock of reading_writing 'eof'
eof bool //is over when read data from socket
}
func newClient(cliCtx *context.ClientContext, s *Server, exec *command.Executor) *client {
return &client{
cliCtx: cliCtx,
server: s,
exec: exec,
eof: false,
}
}
func (c *client) readEof() {
c.eofLock.Lock()
defer c.eofLock.Unlock()
c.eof = true
}
func (c *client) isEof() bool {
c.eofLock.Lock()
defer c.eofLock.Unlock()
return c.eof
}
// Write to conn and log error if needed
func (c *client) Write(p []byte) (int, error) {
zap.L().Debug("write to client", zap.Int64("clientid", c.cliCtx.ID), zap.String("msg", string(p)))
n, err := c.conn.Write(p)
if err != nil {
c.conn.Close()
if err == io.EOF {
zap.L().Info("close connection", zap.String("addr", c.cliCtx.RemoteAddr),
zap.Int64("clientid", c.cliCtx.ID))
} else {
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),
zap.String("error", err.Error()))
return 0, err
}
}
return n, nil
}
func (c *client) serve(conn net.Conn) error {
c.conn = conn
c.r = bufio.NewReader(conn)
var cmd []string
var err error
for {
select {
case <-c.cliCtx.Done:
return c.conn.Close()
default:
cmd, err = c.readCommand()
if err != nil {
c.conn.Close()
if err == io.EOF {
zap.L().Info("close connection", zap.String("addr", c.cliCtx.RemoteAddr),
zap.Int64("clientid", c.cliCtx.ID))
return nil
}
zap.L().Error("read command failed", zap.String("addr", c.cliCtx.RemoteAddr),
zap.Int64("clientid", c.cliCtx.ID), zap.Error(err))
return err
}
}
if c.server.servCtx.Pause > 0 {
time.Sleep(c.server.servCtx.Pause)
c.server.servCtx.Pause = 0
}
if len(cmd) == 0 {
continue
}
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 = context.New(c.cliCtx, c.server.servCtx)
zap.L().Debug("recv msg", zap.String("command", ctx.Name), zap.Strings("arguments", ctx.Args))
// 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
}