forked from segmentio/kafka-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
100 lines (78 loc) · 1.91 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
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
package protocol
import (
"bufio"
"fmt"
"net"
"sync/atomic"
"time"
)
type Conn struct {
buffer *bufio.Reader
conn net.Conn
clientID string
idgen int32
versions atomic.Value // map[ApiKey]int16
}
func NewConn(conn net.Conn, clientID string) *Conn {
return &Conn{
buffer: bufio.NewReader(conn),
conn: conn,
clientID: clientID,
}
}
func (c *Conn) String() string {
return fmt.Sprintf("kafka://%s@%s->%s", c.clientID, c.LocalAddr(), c.RemoteAddr())
}
func (c *Conn) Close() error {
return c.conn.Close()
}
func (c *Conn) Discard(n int) (int, error) {
return c.buffer.Discard(n)
}
func (c *Conn) Peek(n int) ([]byte, error) {
return c.buffer.Peek(n)
}
func (c *Conn) Read(b []byte) (int, error) {
return c.buffer.Read(b)
}
func (c *Conn) Write(b []byte) (int, error) {
return c.conn.Write(b)
}
func (c *Conn) LocalAddr() net.Addr {
return c.conn.LocalAddr()
}
func (c *Conn) RemoteAddr() net.Addr {
return c.conn.RemoteAddr()
}
func (c *Conn) SetDeadline(t time.Time) error {
return c.conn.SetDeadline(t)
}
func (c *Conn) SetReadDeadline(t time.Time) error {
return c.conn.SetReadDeadline(t)
}
func (c *Conn) SetWriteDeadline(t time.Time) error {
return c.conn.SetWriteDeadline(t)
}
func (c *Conn) SetVersions(versions map[ApiKey]int16) {
connVersions := make(map[ApiKey]int16, len(versions))
for k, v := range versions {
connVersions[k] = v
}
c.versions.Store(connVersions)
}
func (c *Conn) RoundTrip(msg Message) (Message, error) {
correlationID := atomic.AddInt32(&c.idgen, +1)
versions, _ := c.versions.Load().(map[ApiKey]int16)
apiVersion := versions[msg.ApiKey()]
if p, _ := msg.(PreparedMessage); p != nil {
p.Prepare(apiVersion)
}
if raw, ok := msg.(RawExchanger); ok && raw.Required(versions) {
return raw.RawExchange(c)
}
return RoundTrip(c, apiVersion, correlationID, c.clientID, msg)
}
var (
_ net.Conn = (*Conn)(nil)
_ bufferedReader = (*Conn)(nil)
)