refcount

package
v1.27.1 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2024 License: MIT Imports: 6 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WaitRefCountContainer

func WaitRefCountContainer[T comparable](
	ctx context.Context,
	target *ccontainer.CContainer[T],
	targetErr *ccontainer.CContainer[*error],
) (T, error)

WaitRefCountContainer waits for a RefCount container handling errors. targetErr can be nil

Types

type Ref

type Ref[T comparable] struct {
	// contains filtered or unexported fields
}

Ref is a reference to a RefCount.

func (*Ref[T]) Release

func (k *Ref[T]) Release()

Release releases the reference.

type RefCount

type RefCount[T comparable] struct {
	// contains filtered or unexported fields
}

RefCount is a refcount driven object container. Wraps a ccontainer with a ref count mechanism. When there are no references, the container contents are released.

func NewRefCount

func NewRefCount[T comparable](
	ctx context.Context,
	keepUnref bool,
	target *ccontainer.CContainer[T],
	targetErr *ccontainer.CContainer[*error],
	resolver RefCountResolver[T],
) *RefCount[T]

NewRefCount builds a new RefCount.

ctx, target and targetErr can be empty

keepUnref sets if the value should be kept if there are zero references. resolver is the resolver function returns the value and a release function call the released callback if the value is no longer valid.

func (*RefCount[T]) Access

func (r *RefCount[T]) Access(ctx context.Context, cb func(ctx context.Context, val T) error) error

Access adds a reference, waits for a value, and calls the callback. Releases the reference once the callback has returned. The context will be canceled if the value is removed / changed. Return context.Canceled if the context is canceled. The callback may be restarted if the context is canceled and a new value is resolved.

func (*RefCount[T]) AddRef

func (r *RefCount[T]) AddRef(cb func(resolved bool, val T, err error)) *Ref[T]

AddRef adds a reference to the RefCount container. cb is an optional callback to call when the value changes. the callback will be called with an empty value when the value becomes empty.

func (*RefCount[T]) AddRefPromise added in v1.2.0

func (r *RefCount[T]) AddRefPromise() (promise.PromiseLike[T], *Ref[T])

AddRefPromise adds a reference and returns a promise with the value.

func (*RefCount[T]) ClearContext added in v1.1.2

func (r *RefCount[T]) ClearContext()

ClearContext clears the context and shuts down all routines.

func (*RefCount[T]) Resolve added in v1.9.0

func (r *RefCount[T]) Resolve(ctx context.Context) (T, func(), error)

Resolve adds a reference and waits for a value. Returns the value, release function, and any error. If err != nil, value and reference will be nil.

func (*RefCount[T]) ResolveWithReleased added in v1.9.0

func (r *RefCount[T]) ResolveWithReleased(ctx context.Context, released func()) (T, func(), error)

ResolveWithReleased adds a reference, waits for a value, returns the value and a release function. Calls the released callback (if set) when the value or reference is released. Note: it's very unlikely, but still possible, that released will be called before the promise resolves. Note: released will always be called from a new goroutine. Note: this matches the signature of the refcount resolver function.

func (*RefCount[T]) SetContext

func (r *RefCount[T]) SetContext(ctx context.Context) bool

SetContext updates the context to use for the RefCount container resolution. If ctx=nil the RefCount will wait until ctx != nil to start. This also restarts resolution, if there are any refs. Returns if the context was updated.

func (*RefCount[T]) Wait

func (r *RefCount[T]) Wait(ctx context.Context) (T, *Ref[T], error)

Wait adds a reference and waits for a value. Returns the value, reference, and any error. If err != nil, value and reference will be nil.

func (*RefCount[T]) WaitWithReleased added in v1.0.4

func (r *RefCount[T]) WaitWithReleased(ctx context.Context, released func()) (promise.PromiseLike[T], *Ref[T])

WaitWithReleased adds a reference, waits for a value, returns the value and a release function. Calls the released callback (if set) when the value or reference is released. Note: it's very unlikely, but still possible, that released will be called before the promise resolves. Note: released will always be called from a new goroutine.

type RefCountResolver added in v1.12.2

type RefCountResolver[T comparable] func(ctx context.Context, released func()) (T, func(), error)

RefCountResolver resolves a value within a RefCount container.

Accepts a released callback which can be called when the returned value is invalid. Returns a release function which will be called when the returned value is no longer needed.

type RefLike added in v1.3.1

type RefLike interface {
	// Release releases the reference.
	Release()
}

RefLike is an interface implemented by Ref.

Jump to

Keyboard shortcuts

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