forked from wangaoone/redeo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
157 lines (132 loc) · 2.86 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
package redeo
import (
"context"
"github.com/wangaoone/redeo/resp"
"io"
"net"
"sync"
"sync/atomic"
)
var (
clientInc = uint64(0)
readerPool sync.Pool
writerPool sync.Pool
)
type ctxKeyClient struct{}
// Client contains information about a client connection
type Client struct {
id uint64
cn net.Conn
rd *resp.RequestReader
wr resp.ResponseWriter
ctx context.Context
closed bool
done chan struct{}
cmd *resp.Command
scmd *resp.CommandStream
responses chan interface{}
}
func newClient(cn net.Conn) *Client {
c := new(Client)
c.reset(cn)
return c
}
// GetClient retrieves the client from a the context.
// This function may return nil if a client is not set.
func GetClient(ctx context.Context) *Client {
if ctx != nil {
if c, ok := ctx.Value(ctxKeyClient{}).(*Client); ok {
return c
}
}
return nil
}
// ID return the unique client id
func (c *Client) ID() uint64 { return c.id }
// Context return the client context
func (c *Client) Context() context.Context {
if c.ctx != nil {
return c.ctx
}
return context.Background()
}
// SetContext sets the client's context
func (c *Client) SetContext(ctx context.Context) {
c.ctx = ctx
}
// RemoteAddr return the remote client address
func (c *Client) RemoteAddr() net.Addr {
return c.cn.RemoteAddr()
}
func (c *Client) Responses() chan interface{} {
return c.responses
}
// Close will disconnect as soon as all pending replies have been written
// to the client
func (c *Client) Close() {
c.closed = true
}
func (c *Client) readCmd(cmd *resp.Command) (*resp.Command, error) {
var err error
if cmd, err = c.rd.ReadCmd(cmd); err == nil {
cmd.SetContext(context.WithValue(cmd.Context(), ctxKeyClient{}, c))
}
return cmd, err
}
func (c *Client) streamCmd(cmd *resp.CommandStream) (*resp.CommandStream, error) {
var err error
if cmd, err = c.rd.StreamCmd(cmd); err == nil {
cmd.SetContext(context.WithValue(cmd.Context(), ctxKeyClient{}, c))
}
return cmd, err
}
func (c *Client) pipeline(fn func(string) error) error {
for more := true; more; more = c.rd.Buffered() != 0 {
name, err := c.rd.PeekCmd()
if err != nil {
if err == io.EOF {
return err
}
_ = c.rd.SkipCmd()
return err
}
if err := fn(name); err != nil {
return err
}
}
return nil
}
func (c *Client) release() {
select {
case <-c.done:
// closed
return
default:
}
close(c.done)
_ = c.cn.Close()
readerPool.Put(c.rd)
writerPool.Put(c.wr)
}
func (c *Client) reset(cn net.Conn) {
*c = Client{
id: atomic.AddUint64(&clientInc, 1),
cn: cn,
}
if v := readerPool.Get(); v != nil {
rd := v.(*resp.RequestReader)
rd.Reset(cn)
c.rd = rd
} else {
c.rd = resp.NewRequestReader(cn)
}
if v := writerPool.Get(); v != nil {
wr := v.(resp.ResponseWriter)
wr.Reset(cn)
c.wr = wr
} else {
c.wr = resp.NewResponseWriter(cn)
}
c.done = make(chan struct{})
c.responses = make(chan interface{}, 1)
}