-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrticker.go
More file actions
114 lines (97 loc) · 1.93 KB
/
rticker.go
File metadata and controls
114 lines (97 loc) · 1.93 KB
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
package random_ticker
import (
"errors"
"math/rand"
"sync"
"time"
)
type tickerQueue struct {
name string
minWait time.Duration
maxWait time.Duration
stop chan bool
}
type RTicker struct {
lock sync.Mutex
tQueues []*tickerQueue
receiver chan string
}
func New() *RTicker {
return &RTicker{
receiver: make(chan string),
}
}
func (rt *RTicker) Receive() <-chan string {
return rt.receiver
}
func (rt *RTicker) SetQueue(name string, tMin, tMax time.Duration) {
rt.lock.Lock()
defer rt.lock.Unlock()
for i := range rt.tQueues {
if rt.tQueues[i].name == name {
rt.tQueues[i].minWait = tMin
rt.tQueues[i].maxWait = tMax
return
}
}
tq := &tickerQueue{
name: name,
minWait: tMin,
maxWait: tMax,
stop: make(chan bool),
}
rt.tQueues = append(rt.tQueues, tq)
go func(t *tickerQueue) {
stop := false
for !stop {
select {
case <-t.stop:
stop = true
case <-waitRandDuration(t.minWait, t.maxWait):
rt.receiver <- t.name
}
}
}(tq)
}
func (rt *RTicker) RemoveQueue(name string) error {
rt.lock.Lock()
defer rt.lock.Unlock()
index := -1
for i := range rt.tQueues {
if rt.tQueues[i].name == name {
index = i
break
}
}
if index > -1 {
rt.tQueues[index].stop <- true
rt.tQueues[index] = nil
rt.tQueues = append(rt.tQueues[0:index], rt.tQueues[index+1:]...)
return nil
}
return errors.New("ticker queue not found")
}
func (rt *RTicker) Stop() {
rt.lock.Lock()
defer rt.lock.Unlock()
for i := range rt.tQueues {
rt.tQueues[i].stop <- true
rt.tQueues[i] = nil
}
rt.tQueues = nil
close(rt.receiver)
}
func (rt *RTicker) QueueCount() int {
rt.lock.Lock()
defer rt.lock.Unlock()
return len(rt.tQueues)
}
func waitRandDuration(tMin, tMax time.Duration) <-chan time.Time {
minSec := int64(tMin)
maxSec := int64(tMax)
secWait := minSec
if maxSec > minSec {
secWait = minSec + rand.Int63n(maxSec-minSec)
}
return time.After(time.Duration(secWait))
}