Skip to content

Repository files navigation

AtomicGo



AtomicGo | timeout

DownloadsLatest ReleaseTestsCoverageUnit test countLicense: MITGo report


Documentation | Contributing | Code of Conduct


go get atomicgo.dev/timeout

timeout

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.

package main
import (
"fmt""time""atomicgo.dev/timeout"
)
funcmain() {
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>
package main
import (
"fmt""time""atomicgo.dev/timeout"
)
funcmain() {
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
package main
import (
"errors""fmt""time""atomicgo.dev/timeout"
)
funcmain() {
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

ErrTimeoutReached is returned when the timeout is reached.

varErrTimeoutReached=errors.New("timeout reached")

func Execute

funcExecute[Tany](duration time.Duration, fnFunction[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.

typeFunction[Tany] func() (T, error)

Generated by gomarkdoc


AtomicGo.dev · with ❤️ by @MarvinJWendt | mjw.dev

About

⏱️ Timeout helpers for Go - limit functions to a maximum execution time

Topics

Resources

Code of conduct

Stars

5 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages

Generated from atomicgo/template