Documentation ¶
Overview ¶
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.
Index ¶
Constants ¶
This section is empty.
Variables ¶
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") )
Functions ¶
Types ¶
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.
type Finalizer ¶ added in v0.8.2
type Finalizer interface {
Finalize()
}
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 SimpleCloser ¶ added in v1.0.0
type SimpleCloser interface {
Close()
}
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.