forked from benjojo/bondcat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
idleTimeoutConn.go
70 lines (59 loc) · 1.2 KB
/
idleTimeoutConn.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
package main
import (
"net"
"sync"
"time"
)
type idleTimeoutConn struct {
timeoutValue time.Duration
resetTimer chan bool
*sync.Once
net.Conn
}
func (i idleTimeoutConn) waitForTimeout() {
resetSignal := time.NewTimer(i.timeoutValue)
for {
select {
case <-i.resetTimer:
resetSignal.Reset(i.timeoutValue)
case <-resetSignal.C:
i.Close()
}
}
}
func (i idleTimeoutConn) Read(b []byte) (n int, err error) {
i.Do(func() {
go func() {
i.waitForTimeout()
}()
})
i.resetTimer <- true
return i.Conn.Read(b)
}
func (i idleTimeoutConn) Write(b []byte) (n int, err error) {
i.Do(func() {
go func() {
i.waitForTimeout()
}()
})
i.resetTimer <- true
return i.Conn.Write(b)
}
func (i idleTimeoutConn) Close() error {
return i.Conn.Close()
}
func (i idleTimeoutConn) LocalAddr() net.Addr {
return i.Conn.LocalAddr()
}
func (i idleTimeoutConn) RemoteAddr() net.Addr {
return i.Conn.RemoteAddr()
}
func (i idleTimeoutConn) SetDeadline(t time.Time) error {
return i.Conn.SetDeadline(t)
}
func (i idleTimeoutConn) SetReadDeadline(t time.Time) error {
return i.Conn.SetReadDeadline(t)
}
func (i idleTimeoutConn) SetWriteDeadline(t time.Time) error {
return i.Conn.SetWriteDeadline(t)
}