-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn_request.go
50 lines (46 loc) · 1.04 KB
/
conn_request.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
package mc
import (
"context"
"time"
"github.com/kinescope/mc/protocol"
)
func (c *Client) request(ctx context.Context, opcode protocol.Opcode, key string, data, extras []byte, cas uint64) (_ []byte, _ []byte, _ uint64, retErr error) {
select {
case <-ctx.Done():
return nil, nil, 0, ctx.Err()
default:
}
if !checkKey(key) {
return nil, nil, 0, ErrMalformedKey
}
conn, err := c.pickServer(key)
if err != nil {
return nil, nil, 0, err
}
defer func() {
c.pool.condRelease(conn, retErr)
}()
if deadline, ok := ctx.Deadline(); ok {
conn.nc.SetDeadline(deadline)
defer conn.nc.SetDeadline(time.Time{})
}
if err := conn.sendPacket(opcode, c.opts.KeyHashFunc(key), data, extras, cas); err != nil {
return nil, nil, 0, err
}
packet, err := conn.readPacket()
if err != nil {
return nil, nil, 0, err
}
return packet.Data, packet.Extras, packet.CAS, nil
}
func checkKey(key string) bool {
if len(key) > 250 {
return false
}
for i := 0; i < len(key); i++ {
if key[i] <= ' ' || key[i] > 0x7e {
return false
}
}
return true
}