This repository has been archived by the owner on Oct 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtimer_queue.go
154 lines (140 loc) · 3.23 KB
/
timer_queue.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
// timer_queue.go - Time delayed queue
// Copyright (C) 2018, 2019 Masala, David Stainton.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package client
import (
"container/heap"
"fmt"
"sync"
"time"
"github.com/katzenpost/core/queue"
"github.com/katzenpost/core/worker"
)
type Item interface {
Priority() uint64
}
type nqueue interface {
Push(Item) error
}
// TimerQueue is a queue that delays messages before forwarding to another queue
type TimerQueue struct {
sync.Mutex
sync.Cond
worker.Worker
priq *queue.PriorityQueue
nextQ nqueue
timer *time.Timer
wakech chan struct{}
}
// NewTimerQueue intantiates a new TimerQueue and starts the worker routine
func NewTimerQueue(nextQueue nqueue) *TimerQueue {
a := &TimerQueue{
nextQ: nextQueue,
timer: time.NewTimer(0),
priq: queue.New(),
}
a.L = new(sync.Mutex)
a.Go(a.worker)
return a
}
// Push adds a message to the TimerQueue
func (a *TimerQueue) Push(i Item) {
a.Lock()
a.priq.Enqueue(i.Priority(), i)
a.Unlock()
a.Signal()
}
// Remove removes a Message from the TimerQueue
func (a *TimerQueue) Remove(i Item) error {
priority := i.Priority()
a.Lock()
defer a.Unlock()
if mo := a.priq.Peek(); mo != nil {
if mo.Value.(Item).Priority() == priority {
_ = heap.Pop(a.priq)
if a.priq.Len() > 0 {
a.Signal()
}
} else {
mo := a.priq.RemovePriority(priority)
if mo == nil {
return fmt.Errorf("failed to remove item with priority %d", priority)
}
}
}
return nil
}
// wakeupCh() returns the channel that fires upon Signal of the TimerQueue's sync.Cond
func (a *TimerQueue) wakeupCh() chan struct{} {
if a.wakech != nil {
return a.wakech
}
c := make(chan struct{})
go func() {
defer close(c)
var v struct{}
for {
a.L.Lock()
a.Wait()
a.L.Unlock()
select {
case <-a.HaltCh():
return
case c <- v:
}
}
}()
a.wakech = c
return c
}
// pop top item from queue and forward to next queue
func (a *TimerQueue) forward() {
a.Lock()
m := heap.Pop(a.priq)
a.Unlock()
if m == nil {
return
}
item := m.(*queue.Entry).Value.(Item)
if err := a.nextQ.Push(item); err != nil {
panic(err)
}
}
func (a *TimerQueue) worker() {
for {
var c <-chan time.Time
a.Lock()
if m := a.priq.Peek(); m != nil {
// Figure out if the message needs to be handled now.
timeLeft := int64(m.Priority) - time.Now().UnixNano()
if timeLeft < 0 || m.Priority < uint64(time.Now().UnixNano()) {
a.Unlock()
a.forward()
continue
} else {
c = time.After(time.Duration(timeLeft))
}
}
a.Unlock()
select {
case <-a.HaltCh():
a.Signal()
return
case <-c:
a.forward()
case <-a.wakeupCh():
}
}
}