lifetime

package
v0.0.0-...-54ba007 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package lifetime provides a mechanism for wrapping io.Closer implementations in a lifetime, automatically calling close after a specified duration.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrExpired is the error given when calling Lifetime.Value on an expired lifetime.
	ErrExpired = errors.New("expired")
)

Functions

This section is empty.

Types

type Lifetime

type Lifetime[T io.Closer] struct {
	// contains filtered or unexported fields
}

The Lifetime type wraps an io.Closer implementation, making it accessible via the Value method until its specified lifetime expires. After expiry, calls to Value will return ErrExpired.

func New

func New[T io.Closer](value T, lifetime time.Duration) *Lifetime[T]

New returns a new instance of the Lifetime type that wraps the given io.Closer. The specified lifetime is used to determine how long to wait until Close is called. After the lifetime has expired, calls to Lifetime.Value will return ErrExpired. If an error occurs calling Close it will be returned on the next call to Lifetime.Value.

func (*Lifetime[T]) Expire

func (lt *Lifetime[T]) Expire()

Expire causes immediate expiration of the underlying io.Closer.

func (*Lifetime[T]) Expired

func (lt *Lifetime[T]) Expired() bool

Expired returns true if this lifetime has expired.

func (*Lifetime[T]) Reset

func (lt *Lifetime[T]) Reset(lifetime time.Duration) error

Reset the lifetime to a new duration. This modifies the expiration to occur after the given duration and does not add additional time to any remaining lifetime.

func (*Lifetime[T]) Value

func (lt *Lifetime[T]) Value() (T, error)

Value returns T if it has not expired. Otherwise, it returns ErrExpired. If the lifetime has expired but calling Close failed, this method will return both ErrExpired and the error returned by Close as a joined error. Note that values you wrap may still be usable outside the scope of this lifetime, so you will still need to handle relevant close errors that may occur using the returned value in that scope.

Jump to

Keyboard shortcuts

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