Skip to content

Commit 208b715

Browse files
committed
refactor loop/error handling
1 parent 684d9b2 commit 208b715

File tree

4 files changed

+37
-21
lines changed

4 files changed

+37
-21
lines changed

cmd/root.go

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func deleteDecisions(custom *custom.CustomBouncer, decisions []*models.Decision)
7474

7575
func addDecisions(custom *custom.CustomBouncer, decisions []*models.Decision) {
7676
if len(decisions) == 1 {
77-
log.Infof("adding 1 decision")
77+
log.Info("adding 1 decision")
7878
} else {
7979
log.Infof("adding %d decisions", len(decisions))
8080
}
@@ -102,19 +102,34 @@ func feedViaStdin(ctx context.Context, custom *custom.CustomBouncer, config *cfg
102102

103103
return c.Wait()
104104
}
105-
var err error
106-
if config.TotalRetries == -1 {
107-
for {
108-
err = f()
109-
log.Errorf("Binary exited: %s", err)
110-
}
111-
} else {
112-
for i := 1; i <= config.TotalRetries; i++ {
113-
err = f()
114-
log.Errorf("Binary exited (retry %d/%d): %s", i, config.TotalRetries, err)
105+
106+
if config.TotalRetries == 0 {
107+
config.TotalRetries = 1
108+
}
109+
110+
attempt := 1
111+
delay := 0 * time.Second
112+
113+
for config.TotalRetries == -1 || attempt <= config.TotalRetries {
114+
time.Sleep(delay)
115+
err := f()
116+
switch {
117+
case err == nil:
118+
log.Warning("custom program exited with no error -- the command is not supposed to quit when using stdin")
119+
case errors.Is(err, context.Canceled):
120+
log.Info("custom program terminated")
121+
return nil
122+
case config.TotalRetries == 1:
123+
log.Errorf("custom program exited: %s", err.Error())
124+
default:
125+
log.Errorf("custom program exited (retry %d/%d): %s", attempt, config.TotalRetries, err.Error())
115126
}
127+
128+
delay = 2 * time.Second
129+
attempt++
116130
}
117-
return errors.New("maximum retries exceeded for binary. Exiting")
131+
132+
return errors.New("maximum retries exceeded for program execution")
118133
}
119134

120135
func Execute() error {
@@ -164,7 +179,7 @@ func Execute() error {
164179

165180
log.Infof("Starting %s %s", name, version.String())
166181

167-
if err = custom.Init(); err != nil {
182+
if err := custom.Init(); err != nil {
168183
return err
169184
}
170185

@@ -178,8 +193,7 @@ func Execute() error {
178193
bouncer := &csbouncer.StreamBouncer{}
179194
bouncer.UserAgent = fmt.Sprintf("%s/%s", name, version.String())
180195

181-
err = bouncer.ConfigReader(strings.NewReader(configExpanded))
182-
if err != nil {
196+
if err := bouncer.ConfigReader(strings.NewReader(configExpanded)); err != nil {
183197
return fmt.Errorf("unable to configure bouncer: %w", err)
184198
}
185199

@@ -212,7 +226,9 @@ func Execute() error {
212226
prometheus.MustRegister(csbouncer.TotalLAPICalls, csbouncer.TotalLAPIError)
213227
go func() {
214228
log.Infof("Serving metrics at %s", listenOn+"/metrics")
215-
log.Error(promServer.ListenAndServe())
229+
if err := promServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
230+
log.Error(err)
231+
}
216232
// don't need to cancel context here, prometheus is not critical
217233
}()
218234
}
@@ -235,7 +251,7 @@ func Execute() error {
235251
log.Errorf("unable to shutdown prometheus server: %s", err)
236252
}
237253
}
238-
return nil
254+
return ctx.Err()
239255
case decisions := <-bouncer.Stream:
240256
if decisions == nil {
241257
continue

config/crowdsec-custom-bouncer.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ log_compression: true
2020
log_max_size: 100
2121
log_max_backups: 3
2222
log_max_age: 30
23-
api_url: http://localhost:8080/
23+
api_url: ${CROWDSEC_LAPI_URL}
2424
api_key: ${API_KEY}
2525

2626
prometheus:

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.24.1
55
require (
66
github.com/coreos/go-systemd/v22 v22.5.0
77
github.com/crowdsecurity/crowdsec v1.6.8
8-
github.com/crowdsecurity/go-cs-bouncer v0.0.14
8+
github.com/crowdsecurity/go-cs-bouncer v0.0.15-0.20250331125736-2a8a151b96a0
99
github.com/crowdsecurity/go-cs-lib v0.0.16
1010
github.com/prometheus/client_golang v1.18.0
1111
github.com/sirupsen/logrus v1.9.3

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV
1515
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
1616
github.com/crowdsecurity/crowdsec v1.6.8 h1:c9C6Q0yBx6kUB878nH4Ib7TVwYF/1Dzw4LRuJvt0dJU=
1717
github.com/crowdsecurity/crowdsec v1.6.8/go.mod h1:PiTkIhJ55g8jnkObrO55LSaw2dRRd37M5WVnS4rkiNI=
18-
github.com/crowdsecurity/go-cs-bouncer v0.0.14 h1:0hxOaa59pMT274qDzJXNxps4QfMnhSNss+oUn36HTpw=
19-
github.com/crowdsecurity/go-cs-bouncer v0.0.14/go.mod h1:4nSF37v7i98idHM6cw1o0V0XgiY25EjTLfFFXvqg6OA=
18+
github.com/crowdsecurity/go-cs-bouncer v0.0.15-0.20250331125736-2a8a151b96a0 h1:TczufDPouQEJLVRZqRxnFU/Rb7ilRxCwVuTK1FvpeSM=
19+
github.com/crowdsecurity/go-cs-bouncer v0.0.15-0.20250331125736-2a8a151b96a0/go.mod h1:4nSF37v7i98idHM6cw1o0V0XgiY25EjTLfFFXvqg6OA=
2020
github.com/crowdsecurity/go-cs-lib v0.0.16 h1:2/htodjwc/sfsv4deX8F/2Fzg1bOI8w3O1/BPSvvsB0=
2121
github.com/crowdsecurity/go-cs-lib v0.0.16/go.mod h1:XwGcvTt4lMq4Tm1IRMSKMDf0CVrnytTU8Uoofa7AR+g=
2222
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

0 commit comments

Comments
 (0)