-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ make sync operation retryable with delay
- Loading branch information
1 parent
5075c62
commit 9ebbaf6
Showing
3 changed files
with
114 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package sync | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/AkashRajpurohit/git-sync/pkg/config" | ||
"github.com/AkashRajpurohit/git-sync/pkg/logger" | ||
) | ||
|
||
func retryOperation(cfg config.Config, operation func() error, operationName string) error { | ||
var lastErr error | ||
|
||
// If retry count is 0 or negative, just execute once without retries | ||
if cfg.Retry.Count <= 0 { | ||
return operation() | ||
} | ||
|
||
for attempt := 1; attempt <= cfg.Retry.Count; attempt++ { | ||
err := operation() | ||
if err == nil { | ||
if attempt > 1 { | ||
logger.Warnf("Operation %s succeeded after %d attempts", operationName, attempt) | ||
} | ||
return nil | ||
} | ||
|
||
lastErr = err | ||
if attempt < cfg.Retry.Count { | ||
logger.Warnf("Attempt %d/%d failed for %s: %v. Retrying in %d seconds...", | ||
attempt, cfg.Retry.Count, operationName, err, cfg.Retry.Delay) | ||
time.Sleep(time.Duration(cfg.Retry.Delay) * time.Second) | ||
} | ||
} | ||
|
||
return fmt.Errorf("operation failed after %d attempts: %v", cfg.Retry.Count, lastErr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters