/
polling.go
75 lines (60 loc) · 1.67 KB
/
polling.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 clients
import (
"fmt"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/asciifaceman/gomo/pkg/models"
"github.com/asciifaceman/gomo/pkg/tmo"
)
// Polling is a client for injecting a return channel and leaving it run in a loop
type Polling struct {
Trashcan *tmo.Trashcan
PollTimeout time.Duration
Signals chan os.Signal
FastmileReturnChannel chan *models.FastmileReturn
}
func NewPolling(hostname string, timeout int, pollFrequency int) (*Polling, error) {
if pollFrequency < 1 {
return nil, fmt.Errorf("poll frequency too fast, may overrun")
}
requestTimeout := time.Duration(timeout) * time.Second
pollTimeout := time.Duration(pollFrequency) * time.Second
t, err := tmo.NewTrashcan(hostname, requestTimeout)
if err != nil {
return nil, err
}
p := &Polling{
Trashcan: t,
PollTimeout: pollTimeout,
Signals: make(chan os.Signal),
FastmileReturnChannel: make(chan *models.FastmileReturn, 1),
}
// In case the caller forgets
signal.Notify(p.Signals, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
return p, nil
}
// Start launches a poller which will return results on ret and terminate on signal
func (p *Polling) Start(ret chan *models.FastmileReturn, signal chan interface{}) {
var wg sync.WaitGroup
t := time.NewTicker(p.PollTimeout).C
go p.Trashcan.FetchRadioStatusAsync(&wg, p.FastmileReturnChannel)
wg.Add(1)
for {
select {
case <-p.Signals:
wg.Wait()
return
case <-signal:
wg.Wait()
return
case <-t:
go p.Trashcan.FetchRadioStatusAsync(&wg, p.FastmileReturnChannel)
wg.Add(1)
case d := <-p.FastmileReturnChannel:
ret <- d
}
}
}