timeout

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 22, 2023 License: MIT Imports: 2 Imported by: 0

README

AtomicGo | timeout

Downloads Latest Release Tests Coverage Unit test count Go report


Documentation | Contributing | Code of Conduct


AtomicGo

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.

Example (Timeout Not Reached)

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 (Timeout Reached)

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 (Timeout With Error)

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

Variables

ErrTimeoutReached is returned when the timeout is reached.

var ErrTimeoutReached = errors.New("timeout reached")

func Execute

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.

type Function

Function is a function that returns a generic value and an error.

type Function[T any] func() (T, error)

Generated by gomarkdoc


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

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

func Execute

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.

Types

type Function

type Function[T any] func() (T, error)

Function is a function that returns a generic value and an error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL