Skip to content

Commit

Permalink
feat: add method for retry #12
Browse files Browse the repository at this point in the history
  • Loading branch information
isaqueveras committed Sep 5, 2024
1 parent 88ab330 commit 0cc7026
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
24 changes: 24 additions & 0 deletions retry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package outis

// retry allowes a given method to be retried x amount of times.
type retry struct{ amount, retries int8 }

// Go returns the settings for using the Attempt method
func (ctx *Context) Retry(retries int8) *retry {
return &retry{retries: retries}
}

// The attempt attempts the given method for a given amount of retries
// If the method still fails after the set limit is a error returned.
func (r *retry) Attempt(method func() error) (err error) {
if err = method(); err == nil {
return nil
}

if r.retries >= r.amount {
return err
}

r.retries++
return r.Attempt(method)
}
4 changes: 2 additions & 2 deletions watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (watch *Watch) Go(opts ...Option) {

defer func() {
if r := recover(); r != nil {
ctx.log.Panicf("RECOVER: %s", r)
ctx.log.Panicf(fmt.Sprintf("%v", r))
}
}()

Expand All @@ -120,7 +120,7 @@ func (ctx *Context) execute() error {
ctx.sleep(now)
defer func() {
if r := recover(); r != nil {
ctx.log.Panicf("RECOVER: %v", r)
ctx.log.Panicf(fmt.Sprintf("%v", r))
}
}()

Expand Down

0 comments on commit 0cc7026

Please sign in to comment.