🍱 fix waiter

This commit is contained in:
Max Lerebourg
2022-10-03 01:23:19 +02:00
parent 952fcd844f
commit 4d7615dd19
+5 -15
View File
@@ -118,13 +118,16 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
cache: ttl_map.New(), cache: ttl_map.New(),
} }
if config.CrowdsecMode == streamMode || config.CrowdsecMode == aloneMode { if config.CrowdsecMode == streamMode || config.CrowdsecMode == aloneMode {
go func() {
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
timeout := rand.Int63n(30)
logger(fmt.Sprintf("Wait: %v", timeout))
time.Sleep(time.Duration(timeout) * time.Second)
if config.CrowdsecMode == aloneMode { if config.CrowdsecMode == aloneMode {
getToken(bouncer) getToken(bouncer)
} }
ticker := time.NewTicker(time.Duration(config.UpdateIntervalSeconds) * time.Second)
go func() {
go handleStreamCache(bouncer) go handleStreamCache(bouncer)
ticker := time.NewTicker(time.Duration(config.UpdateIntervalSeconds) * time.Second)
for range ticker.C { for range ticker.C {
go handleStreamCache(bouncer) go handleStreamCache(bouncer)
} }
@@ -273,20 +276,8 @@ func handleNoStreamCache(a *Bouncer, rw http.ResponseWriter, req *http.Request,
setDecision(a, remoteHost, true, int64(duration.Seconds())) setDecision(a, remoteHost, true, int64(duration.Seconds()))
} }
func waitStream() {
logger("Start Wait Stream")
sec := rand.Int63n(30)
logger(fmt.Sprintf("Sec=%v", sec))
time.Sleep(time.Duration(sec) * time.Second)
logger("End Wait Stream")
}
func handleStreamCache(a *Bouncer) { func handleStreamCache(a *Bouncer) {
logger(fmt.Sprintf("Start handleStreamCache with health=%v", a.crowdsecStreamHealthy))
// TODO clean properly on exit. // TODO clean properly on exit.
waitStream()
// wait random number of sec
var rawQuery string var rawQuery string
var path string var path string
if a.crowdsecMode == aloneMode { if a.crowdsecMode == aloneMode {
@@ -320,7 +311,6 @@ func handleStreamCache(a *Bouncer) {
a.cache.Del(decision.Value) a.cache.Del(decision.Value)
} }
a.crowdsecStreamHealthy = true a.crowdsecStreamHealthy = true
logger(fmt.Sprintf("End handleStreamCache with health=%v", a.crowdsecStreamHealthy))
} }
func getToken(a *Bouncer) { func getToken(a *Bouncer) {