-
Notifications
You must be signed in to change notification settings - Fork 10
/
submitter.go
75 lines (65 loc) · 1.17 KB
/
submitter.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
// +build linux
package iouring
import "time"
type submitter interface {
submit(uint64)
stop()
}
type ringSubmitter struct {
r *Ring
done chan struct{}
work chan struct{}
deadline time.Duration
}
func newRingSubmitter(r *Ring, deadline time.Duration) *ringSubmitter {
return &ringSubmitter{
r: r,
done: make(chan struct{}),
work: make(chan struct{}, 128),
deadline: deadline,
}
}
func (s *ringSubmitter) submit(reqID uint64) {
// We don't actually care about the request id.
s.work <- struct{}{}
}
func (s *ringSubmitter) run() {
timer := time.NewTimer(s.deadline)
if !timer.Stop() {
<-timer.C
}
count := 0
seen := 0
timerActive := false
for {
select {
case <-timer.C:
enter:
n, err := s.r.Enter(uint(count), uint(0), EnterGetEvents, nil)
if err != nil {
continue
}
seen += n
if seen < count {
goto enter
}
seen = 0
count = 0
timerActive = false
case <-s.work:
if !timerActive {
timerActive = true
timer.Reset(s.deadline)
}
count++
case <-s.done:
if !timer.Stop() {
<-timer.C
}
return
}
}
}
func (s *ringSubmitter) stop() {
s.done <- struct{}{}
}