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 ¶
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 ¶
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 ¶
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]) Reset ¶
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 ¶
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.