| 1 |
deadline |
| 2 |
======== |
| 3 |
|
| 4 |
[](https://github.com/eapache/go-resiliency/actions/workflows/golang-ci.yml) |
| 5 |
[](https://godoc.org/github.com/eapache/go-resiliency/deadline) |
| 6 |
[](https://eapache.github.io/conduct.html) |
| 7 |
|
| 8 |
The deadline/timeout resiliency pattern for golang. |
| 9 |
|
| 10 |
Creating a deadline takes one parameter: how long to wait. |
| 11 |
|
| 12 |
```go |
| 13 |
dl := deadline.New(1 * time.Second) |
| 14 |
|
| 15 |
err := dl.Run(func(stopper <-chan struct{}) error { |
| 16 |
// do something potentially slow |
| 17 |
// give up when the `stopper` channel is closed (indicating a time-out) |
| 18 |
return nil |
| 19 |
}) |
| 20 |
|
| 21 |
switch err { |
| 22 |
case deadline.ErrTimedOut: |
| 23 |
// execution took too long, oops |
| 24 |
default: |
| 25 |
// some other error |
| 26 |
} |
| 27 |
``` |