-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathistream.go
85 lines (74 loc) · 1.65 KB
/
istream.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
package nq
import (
"bufio"
"context"
"encoding/binary"
"io"
"net"
"time"
"github.com/prometheus/client_golang/prometheus"
)
type istream struct {
r *bufio.Reader
buf []byte
}
func newIStream() *istream {
return &istream{}
}
func (is *istream) Work(ctx context.Context, s *sub, streamID uint32, conn net.Conn) error {
m := newIStreamMetrics(conn.RemoteAddr(), s.metrics)
var scratchpad [4]byte // uint32
binary.LittleEndian.PutUint32(scratchpad[:], streamID)
if is.r == nil {
is.r = bufio.NewReader(conn)
} else {
is.r.Reset(conn)
}
// read loop
for {
if s.KeepaliveTimeout > 0 {
if err := conn.SetReadDeadline(time.Now().Add(s.KeepaliveTimeout)); err != nil {
return err
}
}
size, err := decodeSize(is.r)
if err != nil {
return err
}
if len(is.buf) < size {
is.buf = make([]byte, size)
}
if _, err = io.ReadFull(is.r, is.buf[:size]); err != nil {
return err
}
// synchronized write to the shared ring buffer
s.enqueue(scratchpad[:], is.buf[:size])
// update metrics
m.receivedMsgs.Inc()
m.receivedBytes.Add(float64(size))
select {
case <-ctx.Done():
return ctx.Err()
default:
continue
}
}
}
type istreamMetrics struct {
receivedBytes prometheus.Counter
receivedMsgs prometheus.Counter
}
func newIStreamMetrics(addr net.Addr, m *Metrics) *istreamMetrics {
var host string
// strip remote port to reduce the amount of labels
if h, _, err := net.SplitHostPort(addr.String()); err == nil {
host = h
} else {
host = addr.String()
}
l := prometheus.Labels{lAddr: host}
return &istreamMetrics{
receivedBytes: m.receivedBytes.With(l),
receivedMsgs: m.receivedMsgs.With(l),
}
}