-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.go
126 lines (104 loc) · 2.65 KB
/
background.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
123
124
125
126
package tmpauth
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"runtime/debug"
"sync"
"time"
)
var backgroundWorker = &BackgroundWorker{
once: new(sync.Once),
mutex: new(sync.RWMutex),
minValidationTime: time.Now(),
debug: false,
logger: nil,
}
type BackgroundWorker struct {
once *sync.Once
mutex *sync.RWMutex
minValidationTime time.Time
debug bool
logger *log.Logger
validationHost string
}
func (w *BackgroundWorker) DebugLog(str string) {
if !w.debug {
return
}
w.logger.Output(2, str)
}
func (w *BackgroundWorker) Start(logger *log.Logger, debug bool, validationHost ...string) {
w.once.Do(func() {
w.DebugLog("background worker started")
w.debug = debug
w.logger = logger
if len(validationHost) > 0 {
w.validationHost = validationHost[0]
}
go w.run()
})
}
func (w *BackgroundWorker) MinValidationTime() time.Time {
w.mutex.RLock()
ret := w.minValidationTime
w.mutex.RUnlock()
return ret
}
func MinValidationTime() time.Time {
return backgroundWorker.MinValidationTime()
}
func (w *BackgroundWorker) run() {
w.updateMinimumIat()
ticker := time.NewTicker(10 * time.Second)
for range ticker.C {
w.updateMinimumIat()
}
}
func (w *BackgroundWorker) updateMinimumIat() {
defer func() {
if r := recover(); r != nil {
fmt.Fprintf(os.Stderr, "tmpauth panic: updateMinimumIat: %v\n%s",
r, string(debug.Stack()))
}
}()
w.DebugLog("updating minimum IAT")
validationHost := w.validationHost
if validationHost == "" {
validationHost = "https://" + TmpAuthHost
}
resp, err := http.Get(validationHost + "/tmpauth/cache")
if err != nil {
w.DebugLog(fmt.Sprintf("failed to get /tmpauth/cache: %v", err))
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
w.DebugLog(fmt.Sprintf("failed to get/tmpauth/cache: response not OK: %v", resp.Status))
return
}
var response struct {
CacheMinIat int64 `json:"cacheMinIat"`
}
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
w.DebugLog(fmt.Sprintf("failed to parse /tmpauth/cache response: %v", err))
return
}
newMinIat := time.Unix(0, response.CacheMinIat*int64(time.Millisecond/time.Nanosecond))
var prevMinIat time.Time
w.mutex.Lock()
if newMinIat.After(w.minValidationTime) {
w.minValidationTime = newMinIat
} else {
prevMinIat = w.minValidationTime
}
w.mutex.Unlock()
if !prevMinIat.IsZero() {
w.DebugLog(fmt.Sprintf("new minimum IAT (%v) before previous new minimum IAT (%v), ignoring update", newMinIat, prevMinIat))
} else {
w.DebugLog(fmt.Sprintf("minimum IAT successfully updated to %v (%v)", response.CacheMinIat, newMinIat))
}
}