-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
61 lines (48 loc) · 1.15 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
package redconcli
import (
"fmt"
"errors"
"context"
"time"
"io"
"net"
)
var (
_ io.Closer = &Client{}
)
type Client struct {
conn net.Conn
reader *Reader
writer *Writer
}
func (c *Client) Close() error {
return c.conn.Close()
}
func (c *Client) Send(values []interface{}) (*Result, error) {
if((values == nil) || (len(values) <= 0)) {
return nil, errors.New("failed to execute operation: values are null or empty !")
}
c.conn.SetDeadline(time.Now().Add(time.Second * 5))
if err := c.writer.WriteArray(values); err != nil {
return nil, errors.New(fmt.Sprintf("failed to execute operation: %v\n%v", err, values[0]))
}
return c.reader.Read()
}
func Connect(ctx context.Context, address string) (*Client, error) {
if(address == "") {
return nil, errors.New("failed to connect: address is empty")
}
dialer := net.Dialer{
Timeout: time.Second * 5,
KeepAlive: time.Second * 10,
}
conn, err := dialer.DialContext(ctx, "tcp4", address)
if err != nil {
return nil, errors.New(fmt.Sprintf("failed to connect to [%s]: %v", address, err))
}
return &Client{
conn: conn,
reader: NewReader(conn),
writer: NewWriter(conn),
}, nil
}