-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.go
193 lines (163 loc) · 3.67 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package hlld
import (
"bufio"
"fmt"
"net"
"sync"
"time"
)
var (
// ErrClientClosed is used if the client is closed
ErrClientClosed = fmt.Errorf("client closed")
)
// Command is used to represent any command that can be sent to
// HLLD. It must be able to encode and decode from the wire.
type Command interface {
Encode(*bufio.Writer) error
Decode(*bufio.Reader) error
}
// Client is used to interact with an hlld server
type Client struct {
config *Config
conn net.Conn
bufR *bufio.Reader
bufW *bufio.Writer
writeLock sync.Mutex
decodeCh chan *Future
closed bool
closedCh chan struct{}
closedLock sync.Mutex
}
// Config is used to parameterize the client
type Config struct {
// MaxPipeline is the maximum number of commands to pipeline
MaxPipeline int
// Timeout is the read or write timeout
Timeout time.Duration
}
// Validate is used to sanity check the configuration
func (c *Config) Validate() error {
if c.MaxPipeline <= 0 {
return fmt.Errorf("max pipeline must be positive")
}
if c.Timeout <= 0 {
return fmt.Errorf("timeout must be positive")
}
return nil
}
// DefaultConfig is used as the default client configuration
func DefaultConfig() *Config {
return &Config{
MaxPipeline: 8192,
Timeout: 5 * time.Second,
}
}
// Dial is a short hand to dial a new connection
func Dial(addr string) (*Client, error) {
conn, err := net.Dial("tcp", addr)
if err != nil {
return nil, err
}
return NewClient(conn, nil)
}
// NewClient is used to create a new client by wrapping an existing connection
func NewClient(conn net.Conn, config *Config) (*Client, error) {
// Default config if none given
if config == nil {
config = DefaultConfig()
}
if err := config.Validate(); err != nil {
return nil, err
}
c := &Client{
config: config,
conn: conn,
bufR: bufio.NewReader(conn),
bufW: bufio.NewWriter(conn),
decodeCh: make(chan *Future, config.MaxPipeline),
closedCh: make(chan struct{}),
}
go c.reader()
return c, nil
}
// Close is used to shut down the client
func (c *Client) Close() error {
c.closedLock.Lock()
defer c.closedLock.Unlock()
if c.closed {
return nil
}
c.closed = true
close(c.closedCh)
c.conn.Close()
return nil
}
// isClosed checks if the client is closed
func (c *Client) isClosed() bool {
select {
case <-c.closedCh:
return true
default:
return false
}
}
// reader is used to read the commands and decode them in an async manner
func (c *Client) reader() {
for {
select {
case next := <-c.decodeCh:
// Set the read deadline
c.conn.SetReadDeadline(time.Now().Add(c.config.Timeout))
// Decode the next command
err := next.Command().Decode(c.bufR)
next.respond(err)
// Shutdown if there was an error
if err != nil {
c.Close()
goto DRAIN
}
case <-c.closedCh:
goto DRAIN
}
}
// After the main loop, drain the decode channel
DRAIN:
for {
select {
case next := <-c.decodeCh:
next.respond(ErrClientClosed)
default:
return
}
}
}
// Execute starts command execution and returns a future
func (c *Client) Execute(cmd Command) (*Future, error) {
c.writeLock.Lock()
defer c.writeLock.Unlock()
// Check if the client is closed
if c.isClosed() {
return nil, ErrClientClosed
}
// Set the write deadline
c.conn.SetWriteDeadline(time.Now().Add(c.config.Timeout))
// Encode the command
err := cmd.Encode(c.bufW)
// Flush the writter
if err == nil {
err = c.bufW.Flush()
}
// Respond and do not enqueue on error, close the socket
if err != nil {
c.Close()
return nil, err
}
// Push the future to the decode channel
f := NewFuture(cmd)
select {
case c.decodeCh <- f:
case <-c.closedCh:
f.respond(ErrClientClosed)
}
return f, nil
}