handle isHealthy in the main function and log error became… (#84)

*  handle isHealthy in the main function and log error became debug

* fix: lint

* fix: lint
This commit is contained in:
maxlerebourg
2023-03-01 14:18:19 +01:00
committed by GitHub
parent 976cbb7d1f
commit b079073ff6
2 changed files with 26 additions and 16 deletions
+18 -13
View File
@@ -143,6 +143,7 @@ func New(ctx context.Context, next http.Handler, config *configuration.Config, n
} }
bouncer.cacheClient.New(config.RedisCacheEnabled, config.RedisCacheHost) bouncer.cacheClient.New(config.RedisCacheEnabled, config.RedisCacheHost)
//nolint:nestif
if (config.CrowdsecMode == configuration.StreamMode || config.CrowdsecMode == configuration.AloneMode) && ticker == nil { if (config.CrowdsecMode == configuration.StreamMode || config.CrowdsecMode == configuration.AloneMode) && ticker == nil {
if config.CrowdsecMode == configuration.AloneMode { if config.CrowdsecMode == configuration.AloneMode {
err = getToken(bouncer) err = getToken(bouncer)
@@ -151,11 +152,18 @@ func New(ctx context.Context, next http.Handler, config *configuration.Config, n
return nil, err return nil, err
} }
} }
ticker = startTicker(config, func() { if err := handleStreamCache(bouncer); err != nil {
handleStreamCache(bouncer) return nil, err
}) }
handleStreamCache(bouncer)
isStartup = false isStartup = false
ticker = startTicker(config, func() {
if err := handleStreamCache(bouncer); err != nil {
isCrowdsecStreamHealthy = false
logger.Error(err.Error())
} else {
isCrowdsecStreamHealthy = true
}
})
} }
logger.Debug(fmt.Sprintf("New initialized mode:%s", config.CrowdsecMode)) logger.Debug(fmt.Sprintf("New initialized mode:%s", config.CrowdsecMode))
@@ -218,7 +226,7 @@ func (bouncer *Bouncer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if isCrowdsecStreamHealthy { if isCrowdsecStreamHealthy {
bouncer.next.ServeHTTP(rw, req) bouncer.next.ServeHTTP(rw, req)
} else { } else {
logger.Error(fmt.Sprintf("ServeHTTP isCrowdsecStreamHealthy:false ip:%s", remoteIP)) logger.Debug(fmt.Sprintf("ServeHTTP isCrowdsecStreamHealthy:false ip:%s", remoteIP))
rw.WriteHeader(http.StatusForbidden) rw.WriteHeader(http.StatusForbidden)
} }
} else { } else {
@@ -348,7 +356,7 @@ func getToken(bouncer *Bouncer) error {
return fmt.Errorf("getToken statusCode:%d", login.Code) return fmt.Errorf("getToken statusCode:%d", login.Code)
} }
func handleStreamCache(bouncer *Bouncer) { func handleStreamCache(bouncer *Bouncer) error {
// TODO clean properly on exit. // TODO clean properly on exit.
// Instead of blocking the goroutine interval for all the secondary node, // Instead of blocking the goroutine interval for all the secondary node,
// if the master service is shut down, other goroutine can take the lead // if the master service is shut down, other goroutine can take the lead
@@ -356,7 +364,7 @@ func handleStreamCache(bouncer *Bouncer) {
_, err := bouncer.cacheClient.GetDecision(cacheTimeoutKey) _, err := bouncer.cacheClient.GetDecision(cacheTimeoutKey)
if err == nil { if err == nil {
logger.Debug("handleStreamCache:alreadyUpdated") logger.Debug("handleStreamCache:alreadyUpdated")
return return nil
} }
bouncer.cacheClient.SetDecision(cacheTimeoutKey, false, bouncer.updateInterval-1) bouncer.cacheClient.SetDecision(cacheTimeoutKey, false, bouncer.updateInterval-1)
streamRouteURL := url.URL{ streamRouteURL := url.URL{
@@ -367,16 +375,12 @@ func handleStreamCache(bouncer *Bouncer) {
} }
body, err := crowdsecQuery(bouncer, streamRouteURL.String(), false) body, err := crowdsecQuery(bouncer, streamRouteURL.String(), false)
if err != nil { if err != nil {
logger.Error(err.Error()) return err
isCrowdsecStreamHealthy = false
return
} }
var stream Stream var stream Stream
err = json.Unmarshal(body, &stream) err = json.Unmarshal(body, &stream)
if err != nil { if err != nil {
logger.Error(fmt.Sprintf("handleStreamCache:parsingBody %s", err.Error())) return fmt.Errorf("handleStreamCache:parsingBody %w", err)
isCrowdsecStreamHealthy = false
return
} }
for _, decision := range stream.New { for _, decision := range stream.New {
duration, err := time.ParseDuration(decision.Duration) duration, err := time.ParseDuration(decision.Duration)
@@ -389,6 +393,7 @@ func handleStreamCache(bouncer *Bouncer) {
} }
logger.Debug("handleStreamCache:updated") logger.Debug("handleStreamCache:updated")
isCrowdsecStreamHealthy = true isCrowdsecStreamHealthy = true
return nil
} }
func crowdsecQuery(bouncer *Bouncer, stringURL string, isPost bool) ([]byte, error) { func crowdsecQuery(bouncer *Bouncer, stringURL string, isPost bool) ([]byte, error) {
+6 -1
View File
@@ -144,12 +144,17 @@ func Test_handleStreamCache(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
args args args args
wantErr bool
}{ }{
// TODO: Add test cases. // TODO: Add test cases.
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
handleStreamCache(tt.args.bouncer) err := handleStreamCache(tt.args.bouncer)
if (err != nil) != tt.wantErr {
t.Errorf("handleStreamCache() error = %v, wantErr %v", err, tt.wantErr)
return
}
}) })
} }
} }