runutil

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2021 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package runutil provides helpers to advanced function scheduling control like repeat or retry.

It's very often the case when you need to excutes some code every fixed intervals or have it retried automatically. To make it reliably with proper timeout, you need to carefully arrange some boilerplate for this. Below function does it for you.

For repeat executes, use Repeat:

err := runutil.Repeat(10*time.Second, stopc, func() error {
	// ...
})

Retry starts executing closure function f until no error is returned from f:

err := runutil.Retry(10*time.Second, stopc, func() error {
	// ...
})

For logging an error on each f error, use RetryWithLog:

err := runutil.RetryWithLog(logger, 10*time.Second, stopc, func() error {
	// ...
})

Another use case for runutil package is when you want to close a `Closer` interface. As we all know, we should close all implements of `Closer`, such as *os.File. Commonly we will use:

defer closer.Close()

The problem is that Close() usually can return important error e.g for os.File the actual file flush might happen (and fail) on `Close` method. It's important to *always* check error. Thanos provides utility functions to log every error like those, allowing to put them in convenient `defer`:

defer runutil.CloseWithLogOnErr(logger, closer, "log format message")

For capturing error, use CloseWithErrCapture:

var err error
defer runutil.CloseWithErrCapture(&err, closer, "log format message")

// ...

If Close() returns error, err will capture it and return by argument.

The rununtil.Exhaust* family of functions provide the same functionality but they take an io.ReadCloser and they exhaust the whole reader before closing them. They are useful when trying to use http keep-alive connections because for the same connection to be re-used the whole response body needs to be exhausted.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloseWithLogOnErrf added in v0.2.0

func CloseWithLogOnErrf(logger *log.Logger, closer io.Closer, format string, a ...interface{})

CloseWithLogOnErrf is making sure we log every error, even those from best effort tiny closers.

func GoSafe

func GoSafe(fn func(), logger *log.Logger)

GoSafe run function in goroutine with Recover handler.

func Recover

func Recover(logger *log.Logger, cleanups ...func())

Recover handle and log panic.

func Repeat

func Repeat(interval time.Duration, stopC <-chan struct{}, f func() error) error

Repeat executes f every interval seconds until stopC is closed or f returns an error. It executes f once right after being called.

func Retry

func Retry(interval time.Duration, stopC <-chan struct{}, f func() error) error

Retry executes f every interval seconds until timeout or no error is returned from f.

func RetryWithLog

func RetryWithLog(logger *log.Logger, interval time.Duration, stopC <-chan struct{}, f func() error) error

RetryWithLog executes f every interval seconds until timeout or no error is returned from f. It logs an error on each f error.

func WithRecover

func WithRecover(fn func(), logger *log.Logger)

WithRecover run function with Recover handler.

Types

This section is empty.

Jump to

Keyboard shortcuts

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