-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn.go
105 lines (91 loc) · 2.02 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
101
102
103
104
105
package daemon
import (
"context"
"errors"
"net"
"sync"
"time"
)
// connections is a synchronizes access to to a net.Conn pool.
type connections struct {
m map[net.Conn]struct{}
mu sync.Mutex
}
func (m *connections) Add(c net.Conn) {
m.mu.Lock()
defer m.mu.Unlock()
m.m[c] = struct{}{}
}
func (m *connections) Close(c net.Conn) error {
m.mu.Lock()
defer m.mu.Unlock()
err := c.Close()
delete(m.m, c)
return err
}
func (m *connections) Size() int {
m.mu.Lock()
defer m.mu.Unlock()
return len(m.m)
}
func (m *connections) CloseAll() error {
m.mu.Lock()
defer m.mu.Unlock()
var err error
for c := range m.m {
err = errors.Join(err, c.Close())
delete(m.m, c)
}
return err
}
// serverConn is a wrapper around a net.Conn that closes the connection when
// the one of the timeouts is reached.
type serverConn struct {
net.Conn
initTimeout time.Duration
idleTimeout time.Duration
maxDeadline time.Time
closeCanceler context.CancelFunc
}
var _ net.Conn = (*serverConn)(nil)
func (c *serverConn) Write(p []byte) (n int, err error) {
c.updateDeadline()
n, err = c.Conn.Write(p)
if _, isNetErr := err.(net.Error); isNetErr && c.closeCanceler != nil {
c.closeCanceler()
}
return
}
func (c *serverConn) Read(b []byte) (n int, err error) {
c.updateDeadline()
n, err = c.Conn.Read(b)
if _, isNetErr := err.(net.Error); isNetErr && c.closeCanceler != nil {
c.closeCanceler()
}
return
}
func (c *serverConn) Close() (err error) {
err = c.Conn.Close()
if c.closeCanceler != nil {
c.closeCanceler()
}
return
}
func (c *serverConn) updateDeadline() {
switch {
case c.initTimeout > 0:
initTimeout := time.Now().Add(c.initTimeout)
c.initTimeout = 0
if initTimeout.Unix() < c.maxDeadline.Unix() || c.maxDeadline.IsZero() {
c.Conn.SetDeadline(initTimeout)
return
}
case c.idleTimeout > 0:
idleDeadline := time.Now().Add(c.idleTimeout)
if idleDeadline.Unix() < c.maxDeadline.Unix() || c.maxDeadline.IsZero() {
c.Conn.SetDeadline(idleDeadline)
return
}
}
c.Conn.SetDeadline(c.maxDeadline)
}