-
Notifications
You must be signed in to change notification settings - Fork 0
/
mnet.go
333 lines (283 loc) · 9.45 KB
/
mnet.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package mnet
import (
"errors"
"io"
"net"
"github.com/influx6/faux/metrics"
)
// ConnHandler defines a function which will process a incoming net.Conn,
// else if error is returned then the net.Conn is closed.
type ConnHandler func(Client) error
// ReaderFunc defines a function which takes giving incoming Client and returns associated
// data and error.
type ReaderFunc func(Client) ([]byte, error)
// ReaderFromFunc defines a function which takes giving incoming Client and returns associated
// data and error.
type ReaderFromFunc func(Client) ([]byte, net.Addr, error)
// WriteToFunc defines a function type which takes a client and writes out data.
type WriteToFunc func(Client, net.Addr, int) (io.WriteCloser, error)
// WriteFunc defines a function type which takes a client and writes out data.
type WriteFunc func(Client, int) (io.WriteCloser, error)
// ClientSiblingsFunc defines a function which returns a list of sibling funcs.
type ClientSiblingsFunc func(Client) ([]Client, error)
// ClientFunc defines a function type which receives a Client type and
// returns a possible error.
type ClientFunc func(Client) error
// ClientWithAddrFunc defines a function type which receives a Client type and
// and net.Addr and returns a possible error.
type ClientWithAddrFunc func(Client, net.Addr) error
// ClientAddrFunc returns a net.Addr associated with a given client else
// an error.
type ClientAddrFunc func(Client) (net.Addr, error)
// ClientExpandBufferFunc expands the internal client collecting buffer.
type ClientExpandBufferFunc func(Client, int) error
// ClientReconnectionFunc defines a function type which receives a Client pointer type and
// is responsible for the reconnection of the client client connection.
type ClientReconnectionFunc func(Client, string) error
// ClientStatisticsFunc defines a function type which returns a Statistics
// structs related to the user.
type ClientStatisticsFunc func(Client) (ClientStatistic, error)
// errors ...
var (
ErrNoDataYet = errors.New("data is not yet available for reading")
ErrNoHostNameInAddr = errors.New("addr must have hostname")
ErrStillConnected = errors.New("connection still active")
ErrAlreadyClosed = errors.New("already closed connection")
ErrReadNotAllowed = errors.New("reading not allowed")
ErrReadFromNotAllowed = errors.New("reading from a addr not allowed")
ErrLiveCheckNotAllowed = errors.New("live status not allowed or supported")
ErrWriteNotAllowed = errors.New("data writing not allowed")
ErrWriteToAddrNotAllowed = errors.New("data writing to a addr not allowed")
ErrCloseNotAllowed = errors.New("closing not allowed")
ErrBufferExpansionNotAllowed = errors.New("buffer expansion not allowed")
ErrFlushNotAllowed = errors.New("write flushing not allowed")
ErrFlushToAddrNotAllowed = errors.New("write flushing to target addr not allowed")
ErrSiblingsNotAllowed = errors.New("siblings retrieval not allowed")
ErrStatisticsNotProvided = errors.New("statistics not provided")
ErrAddrNotProvided = errors.New("net.Addr is not provided")
ErrClientReconnectionUnavailable = errors.New("client reconnection not available")
)
// NetworkStatistic defines a struct ment to hold granular information regarding
// network activities.
type NetworkStatistic struct {
ID string
LocalAddr net.Addr
RemoteAddr net.Addr
TotalClients int64
TotalClosed int64
TotalOpened int64
TotalActive int64
}
// ClientStatistic defines a struct ment to hold granular information regarding
// network activities.
type ClientStatistic struct {
ID string
Local net.Addr
Remote net.Addr
MessagesRead int64
MessagesWritten int64
BytesWritten int64
BytesRead int64
BytesFlushed int64
Reconnects int64
}
// Client holds a given information regarding a given network connection.
type Client struct {
ID string
NID string
Metrics metrics.Metrics
LocalAddrFunc ClientAddrFunc
RemoteAddrFunc ClientAddrFunc
ReaderFunc ReaderFunc
WriteFunc WriteFunc
CloseFunc ClientFunc
LiveFunc ClientFunc
FlushFunc ClientFunc
FlushAddrFunc ClientWithAddrFunc
SiblingsFunc ClientSiblingsFunc
StatisticFunc ClientStatisticsFunc
ReconnectionFunc ClientReconnectionFunc
//ReaderFromFunc ReaderFromFunc
//WriteToFunc WriteToFunc
}
// LocalAddr returns local address associated with given client.
func (c Client) LocalAddr() (net.Addr, error) {
c.Metrics.Emit(
metrics.WithID(c.ID),
metrics.With("network", c.NID),
metrics.Message("Client.LocalAddr"),
)
if c.LocalAddrFunc == nil {
return nil, ErrAddrNotProvided
}
addr, err := c.LocalAddrFunc(c)
if err != nil {
c.Metrics.Emit(
metrics.Error(err),
metrics.WithID(c.ID),
metrics.With("network", c.NID),
metrics.Message("Client.LocalAddr"),
)
return nil, err
}
return addr, nil
}
// RemoteAddr returns remote address associated with given client.
func (c Client) RemoteAddr() (net.Addr, error) {
c.Metrics.Emit(
metrics.WithID(c.ID),
metrics.With("network", c.NID),
metrics.Message("Client.RemoteAddr"),
)
if c.RemoteAddrFunc == nil {
return nil, ErrAddrNotProvided
}
addr, err := c.RemoteAddrFunc(c)
if err != nil {
c.Metrics.Emit(
metrics.Error(err),
metrics.WithID(c.ID),
metrics.With("network", c.NID),
metrics.Message("Client.RemoteAddr"),
)
return nil, err
}
return addr, nil
}
// Live returns an error if client is not currently live or connected to
// network. Allows to know current status of client.
func (c Client) Live() error {
c.Metrics.Emit(
metrics.WithID(c.ID),
metrics.Message("Client.Live"),
metrics.With("network", c.NID),
)
if c.LiveFunc == nil {
return ErrLiveCheckNotAllowed
}
if err := c.LiveFunc(c); err != nil {
c.Metrics.Emit(
metrics.Error(err),
metrics.WithID(c.ID),
metrics.Message("Client.Live"),
metrics.With("network", c.NID),
)
return err
}
return nil
}
// Reconnect attempts to reconnect with external endpoint.
// Also allows provision of alternate address to reconnect with.
// NOTE: Not all may implement this has it's optional.
func (c Client) Reconnect(altAddr string) error {
c.Metrics.Emit(
metrics.WithID(c.ID),
metrics.Message("Client.Reconnect"),
metrics.With("network", c.NID),
metrics.With("alternate_addr", altAddr),
)
if c.ReconnectionFunc == nil {
return ErrClientReconnectionUnavailable
}
if err := c.ReconnectionFunc(c, altAddr); err != nil {
c.Metrics.Emit(
metrics.WithID(c.ID),
metrics.Message("Client.Reconnect"),
metrics.Error(err),
metrics.With("alternate_addr", altAddr),
metrics.With("network", c.NID),
)
return err
}
return nil
}
// ReadFrom reads the underline data into the provided connection
// returning senders address and data.
// NOTE: Not all may implement this has it's optional.
//func (c Client) ReadFrom() ([]byte, net.Addr, error) {
// if c.ReaderFromFunc == nil {
// return nil, nil, ErrReadFromNotAllowed
// }
//
// return c.ReaderFromFunc(c)
//}
// WriteTo writes provided data into connection targeting giving address.
// NOTE: Not all may implement this has it's optional.
//func (c Client) WriteTo(addr net.Addr, toWriteSize int) (io.WriteCloser, error) {
// if c.WriteFunc == nil {
// return nil, ErrWriteToAddrNotAllowed
// }
//
// return c.WriteToFunc(c, addr, toWriteSize)
//}
// Read reads the underline data into the provided slice.
func (c Client) Read() ([]byte, error) {
if c.ReaderFunc == nil {
return nil, ErrReadNotAllowed
}
return c.ReaderFunc(c)
}
// Write writes provided data into connection without any deadline.
func (c Client) Write(toWriteSize int) (io.WriteCloser, error) {
if c.WriteFunc == nil {
return nil, ErrWriteNotAllowed
}
return c.WriteFunc(c, toWriteSize)
}
// Flush sends all accumulated message within clients buffer into
// connection.
func (c Client) Flush() error {
if c.FlushFunc == nil {
return ErrFlushNotAllowed
}
return c.FlushFunc(c)
}
// Statistics returns statistics associated with client.j
func (c Client) Statistics() (ClientStatistic, error) {
c.Metrics.Emit(
metrics.WithID(c.ID),
metrics.Message("Client.Statistics"),
metrics.With("network", c.NID),
)
if c.StatisticFunc == nil {
return ClientStatistic{}, ErrStatisticsNotProvided
}
return c.StatisticFunc(c)
}
// Close closes the underline client connection.
func (c Client) Close() error {
c.Metrics.Emit(
metrics.WithID(c.ID),
metrics.Message("Client.Close"),
metrics.With("network", c.NID),
)
if c.CloseFunc == nil {
return ErrCloseNotAllowed
}
if err := c.CloseFunc(c); err != nil {
c.Metrics.Emit(
metrics.WithID(c.ID),
metrics.Message("Client.Close"),
metrics.Error(err),
metrics.With("network", c.NID),
)
return err
}
return nil
}
// Others returns other client associated with the source of this client.
// NOTE: Not all may implement this has it's optional.
// WARNING: And those who implement this method must ensure the Read method do not
// exists, to avoid conflict of internal behaviour. More so, only the
// server client handler should ever have access to Read/ReadFrom methods.
func (c Client) Others() ([]Client, error) {
defer c.Metrics.Emit(
metrics.WithID(c.ID),
metrics.Message("Client.Others"),
metrics.With("network", c.NID),
)
if c.SiblingsFunc == nil {
return nil, ErrSiblingsNotAllowed
}
return c.SiblingsFunc(c)
}