-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.go
129 lines (102 loc) · 3.6 KB
/
read.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package middleware
import (
"github.com/D3rus/go-library/logger"
metric2 "github.com/D3rus/go-library/socket/metric"
"github.com/D3rus/go-library/socket/server"
"github.com/D3rus/go-library/socket/server/metric"
"github.com/prometheus/client_golang/prometheus"
"net"
"strings"
"time"
)
type readBytesLogger struct {
net.Conn
logger logger.Logger
}
func (logger *readBytesLogger) Read(buffer []byte) (n int, err error) {
length, err := logger.Conn.Read(buffer)
if err == nil {
logger.logger.Infof("socket.server: reading a packet from the address '%s' using the protocol '%s'", logger.Conn.RemoteAddr().String(), logger.Conn.RemoteAddr().Network())
logger.logger.Debugf("socket.server. read bytes - '%s'", string(buffer[:length]))
}
return length, err
}
func ReadBytesLogger(logger logger.Logger) server.MiddlewareFunc {
logger.Info("socket.server: added logging of read bytes")
return func(next server.Handler) server.Handler {
return server.HandlerFunc(func(connection *server.Connection) error {
connection.Conn = &readBytesLogger{
Conn: connection.Conn,
logger: logger,
}
return next.Handle(connection)
})
}
}
type readBytesMetric struct {
net.Conn
counter prometheus.Counter
}
func (connection *readBytesMetric) Read(buffer []byte) (int, error) {
length, err := connection.Conn.Read(buffer)
if err == nil {
connection.counter.Add(float64(length))
}
return length, err
}
func ReadBytesMetric(informer logger.Informer, metric *metric.Metric) server.MiddlewareFunc {
informer.Info("socket.server: added metric counter of bytes read")
return func(next server.Handler) server.Handler {
return server.HandlerFunc(func(connection *server.Connection) error {
connection.Conn = &readBytesMetric{
Conn: connection.Conn,
counter: metric.ReadWriteCounterVec.With(prometheus.Labels{
metric2.ActionFieldName: metric2.ActionReadBytes,
metric2.ProtocolFieldName: connection.Conn.LocalAddr().Network(),
metric2.PortFieldName: strings.Split(connection.Conn.LocalAddr().String(), ":")[1],
metric2.ListenerNameFieldName: "",
}),
}
return next.Handle(connection)
})
}
}
func ReadBytesMetricWithName(informer logger.Informer, metric *metric.Metric, name string) server.MiddlewareFunc {
informer.Info("socket.server: added metric counter of bytes read")
return func(next server.Handler) server.Handler {
return server.HandlerFunc(func(connection *server.Connection) error {
connection.Conn = &readBytesMetric{
Conn: connection.Conn,
counter: metric.ReadWriteCounterVec.With(prometheus.Labels{
metric2.ActionFieldName: metric2.ActionReadBytes,
metric2.ProtocolFieldName: connection.Conn.LocalAddr().Network(),
metric2.PortFieldName: strings.Split(connection.Conn.LocalAddr().String(), ":")[1],
metric2.ListenerNameFieldName: name,
}),
}
return next.Handle(connection)
})
}
}
type readTimeout struct {
net.Conn
config *ReadConfig
}
func (connection *readTimeout) Read(buffer []byte) (int, error) {
if err := connection.Conn.SetReadDeadline(time.Now().Add(connection.config.Timeout)); err != nil {
return 0, err
}
return connection.Conn.Read(buffer)
}
func ReadTimeout(config *ReadConfig, informer logger.Informer) server.Middleware {
informer.Infof("socket.server: added read timeout - '%s'", config.Timeout)
return server.MiddlewareFunc(func(next server.Handler) server.Handler {
return server.HandlerFunc(func(connection *server.Connection) error {
connection.Conn = &readTimeout{
Conn: connection.Conn,
config: config,
}
return next.Handle(connection)
})
})
}