-
Notifications
You must be signed in to change notification settings - Fork 3
/
sock.go
68 lines (58 loc) · 1.54 KB
/
sock.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
package ghord
import (
"net"
"github.com/hermes/ghord/codec"
)
type sock struct {
host string
conn net.Conn
enc codec.Encoder
dec codec.Decoder
}
func (c *Cluster) newSock(conn net.Conn) *sock {
//conn.SetNoDelay(true)
//conn.SetKeepAlive(true)
return &sock{
host: conn.RemoteAddr().String(),
conn: conn,
enc: c.codec.NewEncoder(conn),
dec: c.codec.NewDecoder(conn),
}
}
// write to the sock via an encoder
func (s *sock) write(m *Message) error {
return s.enc.Encode(m)
}
// read an encoded value from the sock
func (s *sock) read(m *Message) error {
return s.dec.Decode(m)
}
// Create a new connection (or get it from the cache) to a node, and add it to the sock pool
// NOTE: the ...int for port is a hack to get overloading (or something like it) working
/*func (c *Cluster) getSock(addr string, port ...int) (*sock, error) {
// Normiliza the address (either given full address as string, or as ip, port components)
var address string
if len(port) == 1 {
address = addr + ":" + strconv.Itoa(port[0])
} else if len(port) > 1 {
return nil, errors.New("Malformed address")
} else {
address = addr
}
s, found := c.connCache.Access(address)
if !found {
conn, err := net.DialTimeout("tcp", address, c.connTimeout)
if err != nil {
c.error("Couldnt get tcp conn to node: %v", err)
c.throwErr(err)
return nil, err
}
return newSock(conn)
}
return s, nil
}
//Put the sock back on to the conn, (thread safe? ...nope lol)
func (c *Cluster) putSock(s *sock) {
s.used = time.Now()
c.connCache.Insert(s.host, s)
}*/