This repository has been archived by the owner on Dec 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
watch.go
122 lines (104 loc) · 2.63 KB
/
watch.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
// Copyright 2020 Torben Schinke
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package forms
import (
"io/ioutil"
"log"
"net/http"
"strings"
"sync"
"time"
)
type Watch struct {
url string
interval time.Duration
ticker *time.Ticker
done chan bool
mutex sync.Mutex
expected string
listener []func(found string, expected string)
lastCheck time.Time
}
func NewWatch(expected string) *Watch {
return &Watch{url: "/version", interval: 5 * time.Second, expected: strings.TrimSpace(expected)}
}
func (a *Watch) SetInterval(d time.Duration) {
a.interval = d
}
func (a *Watch) Start() {
if a.ticker != nil {
a.Stop()
}
a.ticker = time.NewTicker(a.interval)
a.done = make(chan bool)
myTicker := a.ticker // create a hard ref for the spawned goroutine
myDone := a.done // create a hard ref for the spawned goroutine
go func() {
for {
select {
case <-myDone:
return
case t := <-myTicker.C:
go a.check(t) // spawn another goroutine to avoid deadlock if calling stop from listener
}
}
}()
}
func (a *Watch) Check() {
go a.check(time.Now())
}
func (a *Watch) check(t time.Time) {
if t.Sub(a.lastCheck) < 5*time.Millisecond {
return // do not check faster than that
}
a.lastCheck = t
client := http.Client{Timeout: 10 * time.Second}
res, err := client.Get(a.url)
if err != nil {
log.Printf("cannot get version from %s:%v", a.url, err)
return
}
defer res.Body.Close()
msg, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Printf("cannot read version from %s:%v", a.url, err)
return
}
if strings.TrimSpace(string(msg)) != a.expected {
a.notifyChanged(string(msg), a.expected)
}
}
func (a *Watch) notifyChanged(found string, expected string) {
for _, listener := range a.listener {
if listener != nil {
listener(found, expected)
}
}
}
func (a *Watch) AddListener(f func(found string, expected string)) Resource {
a.listener = append(a.listener, f)
idx := len(a.listener) - 1
return NewListener(f, func() {
a.listener[idx] = nil
})
}
func (a *Watch) Stop() {
if a.ticker != nil {
a.ticker.Stop()
a.ticker = nil
}
a.done <- true
close(a.done)
a.done = nil
}