Add Wait Stream feature

This commit is contained in:
MathieuHa
2022-10-03 00:37:41 +02:00
parent 6d8e811a03
commit 9cf4827768
2 changed files with 23 additions and 0 deletions
+9
View File
@@ -28,6 +28,15 @@ run_local:
run: run:
docker-compose -f docker-compose.yml up -d --remove-orphans docker-compose -f docker-compose.yml up -d --remove-orphans
restart_docker_dev:
docker-compose -f docker-compose.dev.yml restart
restart_docker_local:
docker-compose -f docker-compose.local.yml restart
restart_docker:
docker-compose -f docker-compose.yml restart
show_metrics: show_metrics:
docker exec crowdsec cscli metrics docker exec crowdsec cscli metrics
+14
View File
@@ -8,6 +8,7 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
"math/rand"
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
@@ -117,6 +118,7 @@ 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 {
rand.Seed(time.Now().UnixNano())
if config.CrowdsecMode == aloneMode { if config.CrowdsecMode == aloneMode {
getToken(bouncer) getToken(bouncer)
} }
@@ -271,9 +273,20 @@ 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 wait_stream() {
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)) logger(fmt.Sprintf("Start handleStreamCache with health=%v", a.crowdsecStreamHealthy))
// TODO clean properly on exit. // TODO clean properly on exit.
wait_stream()
// 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 {
@@ -307,6 +320,7 @@ 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) {