@@ -74,7 +74,7 @@ func deleteDecisions(custom *custom.CustomBouncer, decisions []*models.Decision)
7474
7575func 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
120135func 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
0 commit comments