-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
deadlines.go
71 lines (61 loc) · 1.12 KB
/
deadlines.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
package utp
import (
"time"
"github.com/anacrolix/missinggo"
)
type deadline struct {
t time.Time
passed missinggo.Event
timer *time.Timer
}
func (me *deadline) set(t time.Time) {
me.t = t
me.passed.Clear()
if me.timer != nil {
me.timer.Stop()
}
me.update()
}
func (me *deadline) update() {
if me.t.IsZero() {
return
}
if time.Now().Before(me.t) {
if me.timer == nil {
me.timer = time.AfterFunc(me.t.Sub(time.Now()), me.callback)
} else {
me.timer.Reset(me.t.Sub(time.Now()))
}
return
}
me.passed.Set()
}
func (me *deadline) callback() {
mu.Lock()
defer mu.Unlock()
me.update()
}
// This is embedded in Conn and Socket to provide deadline methods for
// net.Conn.
type connDeadlines struct {
read, write deadline
}
func (c *connDeadlines) SetDeadline(t time.Time) error {
mu.Lock()
defer mu.Unlock()
c.read.set(t)
c.write.set(t)
return nil
}
func (c *connDeadlines) SetReadDeadline(t time.Time) error {
mu.Lock()
defer mu.Unlock()
c.read.set(t)
return nil
}
func (c *connDeadlines) SetWriteDeadline(t time.Time) error {
mu.Lock()
defer mu.Unlock()
c.write.set(t)
return nil
}