diff --git a/bouncer.go b/bouncer.go index a70b9dc..cc0d312 100644 --- a/bouncer.go +++ b/bouncer.go @@ -118,13 +118,16 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h cache: ttl_map.New(), } if config.CrowdsecMode == streamMode || config.CrowdsecMode == aloneMode { - rand.Seed(time.Now().UnixNano()) - if config.CrowdsecMode == aloneMode { - getToken(bouncer) - } - ticker := time.NewTicker(time.Duration(config.UpdateIntervalSeconds) * time.Second) go func() { + 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 { + getToken(bouncer) + } go handleStreamCache(bouncer) + ticker := time.NewTicker(time.Duration(config.UpdateIntervalSeconds) * time.Second) for range ticker.C { go handleStreamCache(bouncer) } @@ -273,20 +276,8 @@ func handleNoStreamCache(a *Bouncer, rw http.ResponseWriter, req *http.Request, 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) { - logger(fmt.Sprintf("Start handleStreamCache with health=%v", a.crowdsecStreamHealthy)) // TODO clean properly on exit. - - waitStream() - // wait random number of sec var rawQuery string var path string if a.crowdsecMode == aloneMode { @@ -320,7 +311,6 @@ func handleStreamCache(a *Bouncer) { a.cache.Del(decision.Value) } a.crowdsecStreamHealthy = true - logger(fmt.Sprintf("End handleStreamCache with health=%v", a.crowdsecStreamHealthy)) } func getToken(a *Bouncer) {