Documentation
¶
Overview ¶
Package inmem provides an implementation of state.State in memory.
Index ¶
- Variables
- type BackingStore
- type LoadHandler
- type State
- func (st *State) Create(ctx context.Context, resource resource.Resource, opts ...state.CreateOption) error
- func (st *State) Destroy(ctx context.Context, resourcePointer resource.Pointer, ...) error
- func (st *State) Get(ctx context.Context, resourcePointer resource.Pointer, _ ...state.GetOption) (resource.Resource, error)
- func (st *State) List(ctx context.Context, resourceKind resource.Kind, opts ...state.ListOption) (resource.List, error)
- func (st *State) RunHistoryCleanup(ctx context.Context, interval time.Duration) error
- func (st *State) Update(ctx context.Context, newResource resource.Resource, opts ...state.UpdateOption) error
- func (st *State) Watch(ctx context.Context, resourcePointer resource.Pointer, ch chan<- state.Event, ...) error
- func (st *State) WatchKind(ctx context.Context, resourceKind resource.Kind, ch chan<- state.Event, ...) error
- func (st *State) WatchKindAggregated(ctx context.Context, resourceKind resource.Kind, ch chan<- []state.Event, ...) error
- type StateOption
- type StateOptions
Constants ¶
This section is empty.
Variables ¶
var ErrAlreadyExists = errs.AlreadyExists
ErrAlreadyExists generates error compatible with state.ErrConflict.
var ErrInvalidWatchBookmark = errs.ErrInvalidWatchBookmark
ErrInvalidWatchBookmark generates error compatible with state.ErrInvalidWatchBookmark.
var ErrNotFound = errs.NotFound
ErrNotFound generates error compatible with state.ErrNotFound.
var ErrOwnerConflict = errs.OwnerConflict
ErrOwnerConflict generates error compatible with state.ErrConflict.
var ErrPendingFinalizers = errs.PendingFinalizers
ErrPendingFinalizers generates error compatible with state.ErrConflict.
var ErrPhaseConflict = errs.PhaseConflict
ErrPhaseConflict generates error compatible with ErrConflict.
var ErrUpdateSameVersion = errs.UpdateSameVersion
ErrUpdateSameVersion generates error compatible with state.ErrConflict.
var ErrVersionConflict = errs.VersionConflict
ErrVersionConflict generates error compatible with state.ErrConflict.
Functions ¶
This section is empty.
Types ¶
type BackingStore ¶
type BackingStore interface {
// Load contents of the backing store into the in-memory resource collection.
//
// Handler should be called for each resource in the backing store, across all the namespaces.
Load(ctx context.Context, handler LoadHandler) error
// Put the resource to the backing store.
Put(ctx context.Context, ns resource.Namespace, resourceType resource.Type, resource resource.Resource) error
// Destroy the resource from the backing store.
Destroy(ctx context.Context, ns resource.Namespace, resourceType resource.Type, resourcePointer resource.Pointer) error
}
BackingStore provides a way to persist contents of in-memory resource collection.
All resources are still kept in memory, but the backing store is used to persist the resources across process restarts.
BackingStore is responsible for marshaling/unmarshaling of resources.
BackingStore is optional for in-memory resource collection: a State is either fully backed by the store, or fully ephemeral. Use namespaced.NewState to combine persistent and ephemeral namespaces in a single state.
A BackingStore should back at most one State: Load pulls in every namespace the store holds, so two States sharing a store would each load the full contents and then diverge, as neither sees the writes of the other. To split the persistent namespaces across several States, give each one its own store.
type LoadHandler ¶
type LoadHandler func(ns resource.Namespace, resourceType resource.Type, resource resource.Resource) error
LoadHandler is called for each resource loaded from the backing store.
type State ¶
type State struct {
// contains filtered or unexported fields
}
State implements state.CoreState.
State keeps a single event history buffer shared by all resource collections (see eventbuffer), so the memory dedicated to the event history doesn't grow with the number of namespaces and resource types.
A State is either fully backed by a BackingStore, or fully ephemeral: use namespaced.NewState to combine persistent and ephemeral namespaces in a single state.
func NewStateWithOptions ¶
func NewStateWithOptions(opts ...StateOption) *State
NewStateWithOptions returns new State with options.
func (*State) Create ¶
func (st *State) Create(ctx context.Context, resource resource.Resource, opts ...state.CreateOption) error
Create a resource.
func (*State) Destroy ¶
func (st *State) Destroy(ctx context.Context, resourcePointer resource.Pointer, opts ...state.DestroyOption) error
Destroy a resource.
func (*State) Get ¶
func (st *State) Get(ctx context.Context, resourcePointer resource.Pointer, _ ...state.GetOption) (resource.Resource, error)
Get a resource.
func (*State) List ¶
func (st *State) List(ctx context.Context, resourceKind resource.Kind, opts ...state.ListOption) (resource.List, error)
List resources.
func (*State) RunHistoryCleanup ¶ added in v1.17.0
RunHistoryCleanup sweeps the state event history buffer on every tick of the interval until the context is canceled.
The event history buffer grows up to the max capacity as the events are published, and it never shrinks back. An event held by the buffer keeps the resources it references alive, so a state which went through a burst of changes keeps the resources of that burst in memory long after the consumers are done with them.
A sweep releases the events which every watcher of their resource type has already consumed and which are older than the interval, so that the resources become garbage. The buffer capacity itself is not affected, only the memory the events reference.
The cleanup shortens the effective history: a watch which starts from a bookmark pointing to a released event fails with an error matching state.IsInvalidWatchBookmarkError, and TailEvents returns only the events which are still retained. Callers which resume from bookmarks should already handle that error by restarting the watch from scratch, as the same happens when the events are pushed out of the buffer.
RunHistoryCleanup blocks, so that the caller owns the goroutine it runs in. The history is not cleaned up unless it is running.
func (*State) Update ¶
func (st *State) Update(ctx context.Context, newResource resource.Resource, opts ...state.UpdateOption) error
Update a resource.
func (*State) Watch ¶
func (st *State) Watch(ctx context.Context, resourcePointer resource.Pointer, ch chan<- state.Event, opts ...state.WatchOption) error
Watch a resource.
type StateOption ¶
type StateOption func(options *StateOptions)
StateOption applies settings to StateOptions.
func WithBackingStore ¶
func WithBackingStore(store BackingStore) StateOption
WithBackingStore sets a BackingStore for a in-memory resource collection.
Default value is nil (no backing store).
func WithHistoryCapacity
deprecated
func WithHistoryCapacity(capacity int) StateOption
WithHistoryCapacity sets history depth of the state event buffer.
Deprecated: use WithHistoryMaxCapacity and WithHistoryInitialCapacity instead.
func WithHistoryGap ¶
func WithHistoryGap(gap int) StateOption
WithHistoryGap sets a safety gap between watch events consumers and events producers.
The gap is the number of the slots ahead of the oldest event a new watch is not allowed to start from, so that a watch which starts at the very edge of the history doesn't overrun immediately. A bigger gap reduces the effective history depth (HistoryMaxCapacity - HistoryGap), a smaller one might result in buffer overruns if a consumer can't keep up with the events.
The gap only applies once the buffer has grown to its max capacity: while it is still growing no event can be overwritten, so there is nothing to keep the watches away from.
As the buffer is shared by all namespaces and resource types of the state, the gap is a small absolute number of the slots rather than a share of the capacity: it guards against a consumer falling behind within a single burst, and it doesn't need to scale with the total history depth.
func WithHistoryInitialCapacity ¶ added in v0.3.0
func WithHistoryInitialCapacity(initialCapacity int) StateOption
WithHistoryInitialCapacity sets initial history depth of the state event buffer.
The event buffer is shared by all namespaces and resource types of the state.
Deep history requires more memory, but allows Watch request to return more historical entries, and also acts like a buffer if watch consumer can't keep up with events.
Initial capacity of the history buffer is used at the creation time and grows to the max capacity based on the number of events.
func WithHistoryMaxCapacity ¶ added in v0.3.0
func WithHistoryMaxCapacity(maxCapacity int) StateOption
WithHistoryMaxCapacity sets history depth of the state event buffer.
The event buffer is shared by all namespaces and resource types of the state.
Deep history requires more memory, but allows Watch request to return more historical entries, and also acts like a buffer if watch consumer can't keep up with events.
Max capacity limits the maximum depth of the history buffer.
type StateOptions ¶
type StateOptions struct {
BackingStore BackingStore
HistoryMaxCapacity int
HistoryInitialCapacity int
HistoryGap int
}
StateOptions configure inmem.State.
func DefaultStateOptions ¶
func DefaultStateOptions() StateOptions
DefaultStateOptions returns default value of StateOptions.
As the history buffer is shared by all namespaces and resource types of the state, the default capacity is much bigger than the depth which used to be reserved per resource type.
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
collection
Package collection implements a collection of resources of a single (namespace, type) pair.
|
Package collection implements a collection of resources of a single (namespace, type) pair. |
|
errs
Package errs provides errors compatible with the state error predicates.
|
Package errs provides errors compatible with the state error predicates. |
|
eventbuffer
Package eventbuffer implements a circular buffer of watch events shared by many event feeds.
|
Package eventbuffer implements a circular buffer of watch events shared by many event feeds. |