@@ -39,11 +39,13 @@ type CASBackendChecker struct {
3939
4040type CASBackendCheckerOpts struct {
4141 // Whether to check only default backends or all backends
42- OnlyDefaults bool
42+ OnlyDefaults * bool
4343 // Interval between checks, defaults to 30 minutes
4444 CheckInterval time.Duration
4545 // Timeout for each individual backend validation, defaults to 10 seconds
4646 ValidationTimeout time.Duration
47+ // Initial delay before first validation (includes jitter). If not set, runs immediately.
48+ InitialDelay time.Duration
4749}
4850
4951// NewCASBackendChecker creates a new CAS backend checker that will periodically validate
@@ -65,44 +67,56 @@ func (c *CASBackendChecker) Start(ctx context.Context, opts *CASBackendCheckerOp
6567 }
6668
6769 onlyDefaults := true
68- if opts != nil {
69- onlyDefaults = opts .OnlyDefaults
70+ if opts != nil && opts . OnlyDefaults != nil {
71+ onlyDefaults = * opts .OnlyDefaults
7072 }
7173
7274 // Apply validation timeout from options if provided
7375 if opts != nil && opts .ValidationTimeout > 0 {
7476 c .validationTimeout = opts .ValidationTimeout
7577 }
7678
77- ticker := time .NewTicker (interval )
78- defer ticker .Stop ()
79+ // Apply initial delay from options if provided
80+ var initialDelay = 0 * time .Second
81+ if opts != nil && opts .InitialDelay > 0 {
82+ initialDelay = opts .InitialDelay
83+ }
84+
85+ c .logger .Infow ("msg" , "CAS backend checker configured" , "initialDelay" , initialDelay , "interval" , interval , "allBackends" , ! onlyDefaults , "timeout" , c .validationTimeout )
86+
87+ select {
88+ case <- ctx .Done ():
89+ c .logger .Info ("CAS backend checker stopping due to context cancellation before initial check" )
90+ return
91+ case <- time .After (initialDelay ):
92+ // Continue to first check
93+ }
7994
80- // Run one check immediately
81- if err := c .CheckAllBackends (ctx , onlyDefaults ); err != nil {
95+ // Run first check
96+ if err := c .checkBackends (ctx , onlyDefaults ); err != nil {
8297 c .logger .Errorf ("initial CAS backend check failed: %v" , err )
8398 }
8499
85- c .logger .Infof ("CAS backend checker started with interval %s, checking %s, timeout %s" ,
86- interval ,
87- conditionalString (onlyDefaults , "only default backends" , "all backends" ),
88- c .validationTimeout )
100+ // Start periodic checks
101+ ticker := time .NewTicker (interval )
102+ defer ticker .Stop ()
89103
90104 for {
91105 select {
92106 case <- ctx .Done ():
93107 c .logger .Info ("CAS backend checker stopping due to context cancellation" )
94108 return
95109 case <- ticker .C :
96- if err := c .CheckAllBackends (ctx , onlyDefaults ); err != nil {
110+ if err := c .checkBackends (ctx , onlyDefaults ); err != nil {
97111 c .logger .Errorf ("periodic CAS backend check failed: %v" , err )
98112 }
99113 }
100114 }
101115}
102116
103- // CheckAllBackends validates all CAS backends (or just default ones based on configuration)
117+ // checkBackends validates all CAS backends (or just default ones based on configuration)
104118// using a worker pool for parallel processing with timeouts
105- func (c * CASBackendChecker ) CheckAllBackends (ctx context.Context , onlyDefaults bool ) error {
119+ func (c * CASBackendChecker ) checkBackends (ctx context.Context , onlyDefaults bool ) error {
106120 c .logger .Debug ("starting CAS backend validation check" )
107121
108122 backends , err := c .casBackendRepo .ListBackends (ctx , onlyDefaults )
@@ -138,11 +152,3 @@ func (c *CASBackendChecker) CheckAllBackends(ctx context.Context, onlyDefaults b
138152 c .logger .Debug ("all CAS backend validations completed" )
139153 return nil
140154}
141-
142- // Helper function to return different strings based on a condition
143- func conditionalString (condition bool , trueStr , falseStr string ) string {
144- if condition {
145- return trueStr
146- }
147- return falseStr
148- }
0 commit comments