-
Notifications
You must be signed in to change notification settings - Fork 2
/
timer.go
51 lines (44 loc) · 1.02 KB
/
timer.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
package main
import (
"log"
"time"
)
func (s *server) createTimer(job string) {
if _, ok := s.timeKeeper[job]; ok {
log.Print("reseting timer for job ", job)
s.timeKeeper[job].Stop()
delete(s.timeKeeper, job)
}
log.Printf("creating timer for job '%s' with quiet period of %d seconds", job, s.param.proxy.QuietPeriod)
timer := time.AfterFunc(time.Second*time.Duration(s.param.proxy.QuietPeriod), func() {
log.Print("quiet period exceeded for job ", job)
s.triggerJob(job)
if _, ok := s.timeKeeper[job]; ok {
log.Print("deleting timer for job ", job)
delete(s.timeKeeper, job)
}
})
s.timeKeeper[job] = timer
if _, ok := s.timeKeeper[job]; ok {
log.Print("timer saved in time keeper")
}
return
}
func (s *server) createRefreshJob() {
ticker := time.NewTicker(s.mappingRefreshInterval)
quit := make(chan struct{})
go func() {
for {
select {
case <-ticker.C:
err := s.refreshMapping()
if err != nil {
log.Print(err)
}
case <-quit:
ticker.Stop()
return
}
}
}()
}