resourcestore

package
v1.27.1 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StageUnknown = "unknown"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type IdentifiableCreatable

type IdentifiableCreatable interface {
	ID() string
	SetCreated()
}

IdentifiableCreatable are the qualities needed by the caller of the resource. Once a resource is retrieved, SetCreated() will be called, indicating to the server that resource is ready to be listed and operated upon, and ID() will be used to identify the newly created resource to the server.

type Resource

type Resource struct {
	// contains filtered or unexported fields
}

Resource contains the actual resource itself (which must implement the IdentifiableCreatable interface), as well as stores function pointers that pertain to how that resource should be cleaned up, and keeps track of other requests that are watching for the successful creation of this resource.

type ResourceCleaner added in v1.20.4

type ResourceCleaner struct {
	// contains filtered or unexported fields
}

ResourceCleaner is a structure that tracks how to cleanup a resource. CleanupFuncs can be added to it, and it can be told to Cleanup the resource

func NewResourceCleaner added in v1.20.4

func NewResourceCleaner() *ResourceCleaner

NewResourceCleaner creates a new ResourceCleaner

func (*ResourceCleaner) Add added in v1.20.4

func (r *ResourceCleaner) Add(ctx context.Context, description string, fn func() error)

Add adds a new CleanupFunc to the ResourceCleaner

func (*ResourceCleaner) Cleanup added in v1.20.4

func (r *ResourceCleaner) Cleanup() error

Cleanup cleans up the resource, running the cleanup funcs in opposite chronological order

type ResourceStore

type ResourceStore struct {
	// contains filtered or unexported fields
}

ResourceStore is a structure that saves information about a recently created resource. Resources can be added and retrieved from the store. A retrieval (Get) also removes the Resource from the store. The ResourceStore comes with a cleanup routine that loops through the resources and marks them as stale, or removes them if they're already stale, then sleeps for `timeout`. Thus, it takes between `timeout` and `2*timeout` for unrequested resources to be cleaned up. Another routine can request a watcher for a resource by calling WatcherForResource. All watchers will be notified when the resource has successfully been created.

func New

func New() *ResourceStore

New creates a new ResourceStore, with a default timeout, and starts the cleanup function

func NewWithTimeout

func NewWithTimeout(timeout time.Duration) *ResourceStore

NewWithTimeout is used for testing purposes. It allows the caller to set the timeout, allowing for faster tests. Most callers should use New instead.

func (*ResourceStore) Close

func (rc *ResourceStore) Close()

func (*ResourceStore) Delete added in v1.25.3

func (rc *ResourceStore) Delete(name string)

Delete deletes the specified resource from the store. Any resource that has a stage set, but was never Put should have Delete called, or else it will leak.

func (*ResourceStore) Get

func (rc *ResourceStore) Get(name string) string

Get attempts to look up a resource by its name. If it's found, it's removed from the store, and it is set as created. Get returns an empty ID if the resource is not found, and returns the value of the Resource's ID() method if it is.

func (*ResourceStore) Put

func (rc *ResourceStore) Put(name string, resource IdentifiableCreatable, cleaner *ResourceCleaner) error

Put takes a unique resource name (retrieved from the client request, not generated by the server), a newly created resource, and functions to clean up that newly created resource. It adds the Resource to the ResourceStore. It expects name to be unique, and returns an error if a duplicate name is detected.

func (*ResourceStore) SetStageForResource added in v1.23.4

func (rc *ResourceStore) SetStageForResource(ctx context.Context, name, stage string)

func (*ResourceStore) WatcherForResource

func (rc *ResourceStore) WatcherForResource(name string) (watcher chan struct{}, stage string)

WatcherForResource looks up a Resource by name, and gives it a watcher. If no entry exists for that resource, a placeholder is created and a watcher is given to that placeholder resource. A watcher can be used for concurrent processes to wait for the resource to be created. This is useful for situations where clients retry requests quickly after they "fail" because they've taken too long. Adding a watcher allows the server to slow down the client, but still return the resource in a timely manner once it's actually created.

Jump to

Keyboard shortcuts

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