-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
coordinator.go
75 lines (63 loc) · 1.45 KB
/
coordinator.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
package main
import (
"container/ring"
"log"
"sync"
"time"
)
type tailCoordinator struct {
targets *ring.Ring
sync.RWMutex
log *log.Logger
}
func (f *tailCoordinator) start(targets []chan<- time.Time) {
f.targets = ring.New(len(targets))
for i := 0; i < f.targets.Len(); i++ {
f.targets.Value = targets[i]
f.targets = f.targets.Next()
}
//AWS API accepts 5 reqs/sec for account
ticker := time.NewTicker(250 * time.Millisecond)
go func() {
for range ticker.C {
if f.targets == nil {
f.log.Println("coordinator: ring buffer is empty, exiting scheduler.")
return
}
f.Lock()
x := f.targets.Value.(chan<- time.Time)
x <- time.Now()
f.targets = f.targets.Next()
f.Unlock()
}
}()
}
func (f *tailCoordinator) remove(c chan<- time.Time) {
f.RLock()
initialLen := f.targets.Len()
f.RUnlock()
var visited int
f.Lock()
defer f.Unlock()
if f.targets.Len() == 1 {
f.targets = ring.New(0)
f.log.Println("coordinator: single node buffer: reset", f.targets.Len())
return
}
for visited = 0; visited < f.targets.Len(); visited++ {
if f.targets.Value == c {
targetChan := f.targets.Value.(chan<- time.Time)
f.targets = f.targets.Prev()
f.targets.Unlink(1)
close(targetChan)
f.log.Printf("coordinator: channel found and removed at index: %d\n", visited)
break
}
f.targets = f.targets.Next()
}
if f.targets.Len() < initialLen {
for i := 0; i < visited; i++ {
f.targets = f.targets.Prev()
}
}
}