Version: v1.5.0 Latest Latest

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

Go to latest
Published: Apr 7, 2022 License: Apache-2.0 Imports: 3 Imported by: 12



Package resource describes require for object lifecycle management. Both Finalizer and Closer have similar concepts, they both exist so that different types can be used for resource cleanup with different method names as for some things like iterators, the verb close makes more sense than finalize and is more consistent with other types.



This section is empty.


View Source
var (
	// ErrNotCloseable is returned when trying to close a resource
	// that does not conform to a closeable interface.
	ErrNotCloseable = errors.New("not a closeable resource")


func CloseAll added in v1.0.0

func CloseAll(closers ...Closer) error

CloseAll closes all closers and combines any errors.

func TryClose added in v1.0.0

func TryClose(r interface{}) error

TryClose attempts to close a resource, the resource is expected to implement either Closeable or CloseableResult.


type CancellableLifetime

type CancellableLifetime struct {
	// contains filtered or unexported fields

CancellableLifetime describes a lifetime for a resource that allows checking out the resource and returning it and once cancelled will not allow any further checkouts.

func NewCancellableLifetime

func NewCancellableLifetime() *CancellableLifetime

NewCancellableLifetime returns a new cancellable resource lifetime.

func (*CancellableLifetime) Cancel

func (l *CancellableLifetime) Cancel()

Cancel will wait for all current checkouts to be returned and then will cancel the lifetime so that it cannot be checked out any longer.

func (*CancellableLifetime) ReleaseCheckout

func (l *CancellableLifetime) ReleaseCheckout()

ReleaseCheckout will decrement the number of current checkouts, it MUST only be called after a call to TryCheckout and must not be called more than once per call to TryCheckout or else it will panic as it will try to unlock an unlocked resource.

func (*CancellableLifetime) TryCheckout

func (l *CancellableLifetime) TryCheckout() bool

TryCheckout will try to checkout the resource, if the lifetime is already cancelled this will return false, otherwise it will return true and guarantee the lifetime is not cancelled until the checkout is returned. If this returns true you MUST call ReleaseCheckout later, otherwise the lifetime will never close and any caller calling Cancel will be blocked indefinitely.

type Closer added in v0.8.2

type Closer interface {
	Close() error

Closer is an object that can be closed which returns an error.

type CloserFn added in v0.8.2

type CloserFn func() error

CloserFn is a function literal that is a closer which returns an error.

func (CloserFn) Close added in v0.8.2

func (fn CloserFn) Close() error

Close will call the function literal as a closer.

type Finalizer added in v0.8.2

type Finalizer interface {

Finalizer finalizes a checked resource.

type FinalizerFn added in v0.8.2

type FinalizerFn func()

FinalizerFn is a function literal that is a finalizer.

func (FinalizerFn) Finalize added in v0.8.2

func (fn FinalizerFn) Finalize()

Finalize will call the function literal as a finalizer.

type NoopCloser added in v1.4.2

type NoopCloser struct {
	// Calls is the number of times this closer was called.
	Calls int

NoopCloser is a no-op closer.

func (*NoopCloser) Close added in v1.4.2

func (c *NoopCloser) Close()

Close closes the no-op closer.

type SimpleCloser added in v1.0.0

type SimpleCloser interface {

SimpleCloser is an object that can be closed.

type SimpleCloserFn added in v1.0.0

type SimpleCloserFn func()

SimpleCloserFn is a function literal that is a closer.

func (SimpleCloserFn) Close added in v1.0.0

func (fn SimpleCloserFn) Close()

Close will call the function literal as a closer.

Jump to

Keyboard shortcuts

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