aggregate masq sync errors

This commit is contained in:
Sword
2024-10-31 03:38:25 +00:00
committed by GitHub
parent 287d740486
commit 23d571e598

View File

@@ -18,6 +18,7 @@ package main
import (
"context"
"errors"
"fmt"
"time"
@@ -55,23 +56,23 @@ type IPMasqAgent struct {
// SyncRulesForever syncs ip masquerade rules forever
// these rules only needs to be installed once, but we run it periodically to check that are
// not deleted by an external program. It fails if can't sync the rules during 3 iterations
// TODO: aggregate errors
func (ma *IPMasqAgent) SyncRulesForever(ctx context.Context, interval time.Duration) error {
errs := 0
var errs []error
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
if err := ma.SyncRules(); err != nil {
errs++
if errs > 3 {
return fmt.Errorf("Can't synchronize rules after 3 attempts: %v", err)
errs = append(errs, fmt.Errorf("failed to synchronize rules at %s: %v", time.Now(), err))
if len(errs) > 3 {
return fmt.Errorf("Can't synchronize rules after 3 attempts: %w", err)
}
} else {
errs = 0
errs = errs[:0]
}
select {
case <-ctx.Done():
return errors.Join(errs...)
case <-ticker.C:
}
}