-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsender.go
80 lines (66 loc) · 1.64 KB
/
sender.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
package middleware
import (
"time"
"go.eloylp.dev/goomerang/conn"
"go.eloylp.dev/goomerang/message"
)
// Sender is just a handy middleware for gathering
// the message and the bytes sent after the operation.
type Sender struct {
bytes int
msg *message.Message
sender message.Sender
}
func (s *Sender) ConnSlot() *conn.Slot {
return s.sender.ConnSlot()
}
func NewSender(sender message.Sender) *Sender {
return &Sender{sender: sender}
}
func (s *Sender) Msg() *message.Message {
return s.msg
}
func (s *Sender) Bytes() int {
return s.bytes
}
func (s *Sender) Send(msg *message.Message) (int, error) {
w, err := s.sender.Send(msg)
s.msg = msg
s.bytes = w
return w, err
}
// MeteredSender is an instrumented sender. It's very handy
// to know the sent message,size and time. It requires
// the PromConfig parameter, which should be populated beforehand
// with the needed histograms.
type MeteredSender struct {
bytes int
msg *message.Message
sender message.Sender
promConfig PromConfig
}
func (s *MeteredSender) ConnSlot() *conn.Slot {
return s.sender.ConnSlot()
}
func NewMeteredSender(sender message.Sender, promConfig PromConfig) *MeteredSender {
return &MeteredSender{
sender: sender,
promConfig: promConfig,
}
}
func (s *MeteredSender) Msg() *message.Message {
return s.msg
}
func (s *MeteredSender) Bytes() int {
return s.bytes
}
func (s *MeteredSender) Send(msg *message.Message) (int, error) {
start := time.Now()
w, err := s.sender.Send(msg)
if err == nil {
s.promConfig.MessageSentTime.WithLabelValues(msg.Metadata.Kind).Observe(time.Since(start).Seconds())
}
s.msg = msg
s.bytes = w
return w, err
}