-
Notifications
You must be signed in to change notification settings - Fork 616
/
watch.go
46 lines (38 loc) · 886 Bytes
/
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
package cert
import (
"crypto/tls"
"log"
"reflect"
"time"
)
// watch monitors the result of the loadFn function for changes.
func watch(ch chan []tls.Certificate, refresh time.Duration, path string, loadFn func(path string) (map[string][]byte, error)) {
once := refresh <= 0
// do not refresh more often than once a second to prevent busy loops
if refresh < time.Second {
refresh = time.Second
}
var last map[string][]byte
for {
next, err := loadFn(path)
if err != nil {
log.Printf("[ERROR] cert: Cannot load certificates from %s. %s", path, err)
time.Sleep(refresh)
continue
}
if reflect.DeepEqual(next, last) {
time.Sleep(refresh)
continue
}
certs, err := loadCertificates(next)
if err != nil {
log.Printf("[ERROR] cert: Cannot make certificates: %s", err)
continue
}
ch <- certs
last = next
if once {
return
}
}
}