forked from jcoene/riago
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconn.go
71 lines (60 loc) · 1.52 KB
/
conn.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
package riago
import (
"code.google.com/p/goprotobuf/proto"
"github.com/3XX0/pooly"
"io"
"net"
"time"
)
type Conn struct {
conn *net.TCPConn
lastChecked time.Time
writeTimeout time.Duration
readTimeout time.Duration
}
func Riak(c *pooly.Conn) *Conn {
return c.Interface().(*Conn)
}
// Encode and write a request to the Riak server.
func (c *Conn) request(code byte, req proto.Message) error {
buf, err := encode(code, req)
if err != nil {
return err
}
if c.writeTimeout > 0 {
c.conn.SetWriteDeadline(time.Now().Add(c.writeTimeout))
}
_, err = c.conn.Write(buf)
return err
}
// Read and decode a response from the Riak server.
func (c *Conn) response(resp proto.Message) error {
if c.readTimeout > 0 {
c.conn.SetReadDeadline(time.Now().Add(c.readTimeout))
}
sizebuf := make([]byte, 4)
if _, err := io.ReadFull(c.conn, sizebuf); err != nil {
return err
}
size := int(sizebuf[0])<<24 + int(sizebuf[1])<<16 + int(sizebuf[2])<<8 + int(sizebuf[3])
buf := make([]byte, size)
if _, err := io.ReadFull(c.conn, buf); err != nil {
return err
}
return decode(buf, resp)
}
func (c *Conn) do(code byte, req proto.Message, resp proto.Message) error {
if err := c.request(code, req); err != nil {
return err
}
return c.response(resp)
}
func (c *Conn) Ping() error {
return c.do(MsgRpbPingReq, nil, nil)
}
// Performs a Riak Server info request.
func (c *Conn) ServerInfo() (resp *RpbGetServerInfoResp, err error) {
resp = new(RpbGetServerInfoResp)
err = c.do(MsgRpbGetServerInfoReq, nil, resp)
return
}