https://github.com/atomicgo/timeout
⏱️ Timeout helpers for Go - limit functions to a maximum execution time
https://github.com/atomicgo/timeout
atomicgo deadline go golang golang-library time timeout
Last synced: about 2 months ago
JSON representation
⏱️ Timeout helpers for Go - limit functions to a maximum execution time
- Host: GitHub
- URL: https://github.com/atomicgo/timeout
- Owner: atomicgo
- License: mit
- Created: 2023-10-22T18:31:41.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-08-08T17:04:41.000Z (11 months ago)
- Last Synced: 2024-11-13T00:40:05.294Z (7 months ago)
- Topics: atomicgo, deadline, go, golang, golang-library, time, timeout
- Language: Go
- Homepage: https://atomicgo.dev
- Size: 36.1 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
AtomicGo | timeout
---
Documentation
|
Contributing
|
Code of Conduct---
![]()
go get atomicgo.dev/timeout
# timeout
```go
import "atomicgo.dev/timeout"
```Package timeout provides a simple way to add timeouts to your Go code. The Execute function is blocking, so you can use it as a drop\-in replacement for your function calls. Timeouts are often useful in scripts, where you want to limit the execution time of a function.
```go
package mainimport (
"atomicgo.dev/timeout"
"fmt"
"time"
)func main() {
res, err := timeout.Execute(time.Second*2, func() (string, error) {
time.Sleep(time.Second * 1)
return "Hello, World!", nil
})fmt.Println(res, err)
}
```#### Output
```
Hello, World!
``````go
package mainimport (
"atomicgo.dev/timeout"
"fmt"
"time"
)func main() {
res, err := timeout.Execute(time.Second*1, func() (string, error) {
time.Sleep(time.Second * 2)
return "Hello, World!", nil
})fmt.Println(res, err)
}
```#### Output
```
timeout reached
``````go
package mainimport (
"atomicgo.dev/timeout"
"errors"
"fmt"
"time"
)func main() {
res, err := timeout.Execute(time.Second*2, func() (string, error) {
time.Sleep(time.Second * 1)
return "", errors.New("some error") // nolint: goerr113
})fmt.Println(res, err)
}
```#### Output
```
some error
```## Index
- [Variables](<#variables>)
- [func Execute\[T any\]\(duration time.Duration, fn Function\[T\]\) \(T, error\)](<#Execute>)
- [type Function](<#Function>)## Variables
ErrTimeoutReached is returned when the timeout is reached.
```go
var ErrTimeoutReached = errors.New("timeout reached")
``````go
func Execute[T any](duration time.Duration, fn Function[T]) (T, error)
```Execute executes a function and returns the result or an error if the timeout is reached. If the timeout is reached, the Function will be interrupted. If the Function returns an error, the error will be returned.
Function is a function that returns a generic value and an error.
```go
type Function[T any] func() (T, error)
```Generated by [gomarkdoc]()
---
> [AtomicGo.dev](https://atomicgo.dev) ·
> with ❤️ by [@MarvinJWendt](https://github.com/MarvinJWendt) |
> [MarvinJWendt.com](https://marvinjwendt.com)