Skip to content

Commit

Permalink
Merge pull request #116 from natenho/until-succeeded
Browse files Browse the repository at this point in the history
Introduce UntilSucceeded option to improve readability
  • Loading branch information
JaSei committed Apr 18, 2024
2 parents 64083c1 + 88a4c8d commit c7fe1f1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@

Simple library for retry mechanism

slightly inspired by
Slightly inspired by
[Try::Tiny::Retry](https://metacpan.org/pod/Try::Tiny::Retry)

# SYNOPSIS

http get with retry:
HTTP GET with retry:

url := "http://example.com"
var body []byte
Expand All @@ -31,17 +31,17 @@ http get with retry:
if err != nil {
return err
}

return nil
},
)

if err != nil {
// handle error
}

fmt.Println(string(body))

http get with retry with data:
HTTP GET with retry with data:

url := "http://example.com"

Expand All @@ -60,13 +60,14 @@ http get with retry with data:
return body, nil
},
)

if err != nil {
// handle error
}

fmt.Println(string(body))

[next examples](https://github.com/avast/retry-go/tree/master/examples)
[More examples](https://github.com/avast/retry-go/tree/master/examples)

# SEE ALSO

Expand Down Expand Up @@ -241,10 +242,10 @@ used with that library.
#### type OnRetryFunc

```go
type OnRetryFunc func(n uint, err error)
type OnRetryFunc func(attempt uint, err error)
```

Function signature of OnRetry function n = count of attempts
Function signature of OnRetry function

#### type Option

Expand Down Expand Up @@ -366,7 +367,7 @@ skip retry if special error example:
return false
}
return true
}),
})
)
By default RetryIf stops execution if the error is wrapped using
Expand All @@ -378,6 +379,14 @@ By default RetryIf stops execution if the error is wrapped using
}
)
#### func UntilSucceeded
```go
func UntilSucceeded() Option
```
UntilSucceeded will retry until the retried function succeeds. Equivalent to
setting Attempts(0).
#### func WithTimer
```go
Expand Down
10 changes: 8 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import (
type RetryIfFunc func(error) bool

// Function signature of OnRetry function
// n = count of attempts
type OnRetryFunc func(n uint, err error)
type OnRetryFunc func(attempt uint, err error)

// DelayTypeFunc is called to return the next delay to wait after the retriable function fails on `err` after `n` attempts.
type DelayTypeFunc func(n uint, err error, config *Config) time.Duration
Expand Down Expand Up @@ -60,6 +59,13 @@ func Attempts(attempts uint) Option {
}
}

// UntilSucceeded will retry until the retried function succeeds. Equivalent to setting Attempts(0).
func UntilSucceeded() Option {
return func(c *Config) {
c.attempts = 0
}
}

// AttemptsForError sets count of retry in case execution results in given `err`
// Retries for the given `err` are also counted against total retries.
// The retry will stop if any of given retries is exhausted.
Expand Down
11 changes: 6 additions & 5 deletions retry.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
Simple library for retry mechanism
slightly inspired by [Try::Tiny::Retry](https://metacpan.org/pod/Try::Tiny::Retry)
Slightly inspired by [Try::Tiny::Retry](https://metacpan.org/pod/Try::Tiny::Retry)
# SYNOPSIS
http get with retry:
HTTP GET with retry:
url := "http://example.com"
var body []byte
Expand All @@ -21,17 +21,17 @@ http get with retry:
if err != nil {
return err
}
return nil
},
)
if err != nil {
// handle error
}
fmt.Println(string(body))
http get with retry with data:
HTTP GET with retry with data:
url := "http://example.com"
Expand All @@ -50,13 +50,14 @@ http get with retry with data:
return body, nil
},
)
if err != nil {
// handle error
}
fmt.Println(string(body))
[next examples](https://github.com/avast/retry-go/tree/master/examples)
[More examples](https://github.com/avast/retry-go/tree/master/examples)
# SEE ALSO
Expand Down

0 comments on commit c7fe1f1

Please sign in to comment.