Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephTLyons committed Nov 27, 2024
1 parent 939dc43 commit 93b62ae
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,32 @@ Execute fallible operations multiple times.
gleam add persevero@1
```

```gleam
import gleam/int
import gleam/io
import persevero
Given you have some fallible operation:

pub type NetworkError {
ServerDown
Timeout(Int)
InvalidStatusCode(Int)
InvalidResponseBody(String)
```gleam
pub fn main() {
// Start with a 50ms delay, increasing by 10ms each time, allow all errors, and try 3 times.
use <- persevero.execute(
wait_stream: persevero.linear_backoff(50, 10),
allow: fn(_) { True },
max_attempts: 3,
)
fallible_operation()
}
```

pub fn network_request() -> Result(String, NetworkError) {
Error(Timeout(int.random(5)))
}
A complex example:

```gleam
import persevero
pub fn main() {
persevero.exponential_backoff(500, 2)
persevero.custom_backoff(wait_time: 1000, next_wait_time: fn(previous) {
{ previous + 100 } * 2
})
|> persevero.apply_multiplier(3)
|> persevero.apply_jitter(20)
|> persevero.apply_cap(5000)
|> persevero.apply_cap(10000)
|> persevero.execute(
allow: fn(error) {
case error {
Expand All @@ -43,11 +49,9 @@ pub fn main() {
_ -> False
}
},
max_attempts: 5,
operation: network_request,
max_attempts: 10,
operation: fallible_operation,
)
|> io.debug
// Error(RetriesExhausted([Timeout(3), Timeout(4), Timeout(2), Timeout(3), Timeout(1)]))
}
```

Expand Down

0 comments on commit 93b62ae

Please sign in to comment.