Documentation
¶
Overview ¶
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.
Example (TimeoutNotReached) ¶
package main
import (
"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! <nil>
Example (TimeoutReached) ¶
package main
import (
"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
Example (TimeoutWithError) ¶
package main
import (
"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 ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrTimeoutReached = errors.New("timeout reached")
ErrTimeoutReached is returned when the timeout is reached.
Functions ¶
Types ¶
Click to show internal directories.
Click to hide internal directories.