runutil

package
v0.0.0-...-98dad3e Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

The main 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 CloseWithLogOnErr

func CloseWithLogOnErr(logger log.Logger, closer io.Closer, format string)

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

func ExhaustCloseRequestBodyHandler

func ExhaustCloseRequestBodyHandler(logger log.Logger, next http.Handler) http.Handler

ExhaustCloseRequestBodyHandler ensures that request body is well closed and exhausted at the end of server call.

func ExhaustCloseWithLogOnErr

func ExhaustCloseWithLogOnErr(logger log.Logger, r io.ReadCloser, format string)

ExhaustCloseWithLogOnErr closes the io.ReadCloser with a log message on error but exhausts the reader before.

Types

This section is empty.

Jump to

Keyboard shortcuts

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