utils

package
v4.0.0-...-2a44fb3 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2020 License: Apache-2.0 Imports: 11 Imported by: 6

Documentation

Overview

Package utils contains support code that most users of the SDK will not need to access directly. However, they may be useful for anyone developing custom integrations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func UnmarshalItem

func UnmarshalItem(kind ld.VersionedDataKind, raw []byte) (ld.VersionedData, error)

UnmarshalItem attempts to unmarshal an entity that has been stored as JSON in a FeatureStore. The kind parameter indicates what type of entity is expected.

Types

type FeatureStoreCore

type FeatureStoreCore interface {
	FeatureStoreCoreBase
	// InitInternal replaces the entire contents of the data store. This should be done
	// atomically (i.e. within a transaction).
	InitInternal(map[ld.VersionedDataKind]map[string]ld.VersionedData) error
}

FeatureStoreCore is an interface for a simplified subset of the functionality of ldclient.FeatureStore, to be used in conjunction with FeatureStoreWrapper. This allows developers of custom FeatureStore implementations to avoid repeating logic that would commonly be needed in any such implementation, such as caching. Instead, they can implement only FeatureStoreCore and then call NewFeatureStoreWrapper.

This interface assumes that the feature store can update the data set atomically. If not, use NonAtomicFeatureStoreCore instead. FeatureStoreCoreBase defines the common methods.

type FeatureStoreCoreBase

type FeatureStoreCoreBase interface {
	// GetInternal queries a single item from the data store. The kind parameter distinguishes
	// between different categories of data (flags, segments) and the key is the unique key
	// within that category. If no such item exists, the method should return (nil, nil).
	// It should not attempt to filter out any items based on their Deleted property, nor to
	// cache any items.
	GetInternal(kind ld.VersionedDataKind, key string) (ld.VersionedData, error)
	// GetAllInternal queries all items in a given category from the data store, returning
	// a map of unique keys to items. It should not attempt to filter out any items based
	// on their Deleted property, nor to cache any items.
	GetAllInternal(kind ld.VersionedDataKind) (map[string]ld.VersionedData, error)
	// UpsertInternal adds or updates a single item. If an item with the same key already
	// exists, it should update it only if the new item's GetVersion() value is greater
	// than the old one. It should return the final state of the item, i.e. if the update
	// succeeded then it returns the item that was passed in, and if the update failed due
	// to the version check then it returns the item that is currently in the data store
	// (this ensures that caching works correctly).
	//
	// Note that deletes are implemented by using UpsertInternal to store an item whose
	// Deleted property is true.
	UpsertInternal(kind ld.VersionedDataKind, item ld.VersionedData) (ld.VersionedData, error)
	// InitializedInternal returns true if the data store contains a complete data set,
	// meaning that InitInternal has been called at least once. In a shared data store, it
	// should be able to detect this even if InitInternal was called in a different process,
	// i.e. the test should be based on looking at what is in the data store. The method
	// does not need to worry about caching this value; FeatureStoreWrapper will only call
	// it when necessary.
	InitializedInternal() bool
	// GetCacheTTL returns the length of time that data should be retained in an in-memory
	// cache. This cache is maintained by FeatureStoreWrapper. If GetCacheTTL returns zero,
	// there will be no cache. If it returns a negative number, the cache never expires.
	GetCacheTTL() time.Duration
}

FeatureStoreCoreBase defines methods that are common to the FeatureStoreCore and NonAtomicFeatureStoreCore interfaces.

type FeatureStoreCoreStatus

type FeatureStoreCoreStatus interface {
	// Tests whether the data store seems to be functioning normally. This should not be a detailed
	// test of different kinds of operations, but just the smallest possible operation to determine
	// whether (for instance) we can reach the database. FeatureStoreWrapper will call this method
	// at intervals if the store has previously failed, until it returns true.
	IsStoreAvailable() bool
}

FeatureStoreCoreStatus is an optional interface that can be implemented by FeatureStoreCoreBase implementations. It allows FeatureStoreWrapper to request a status check on the availability of the underlying data store.

type FeatureStoreWrapper

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

FeatureStoreWrapper is a partial implementation of ldclient.FeatureStore that delegates basic functionality to an instance of FeatureStoreCore. It provides optional caching, and will automatically provide the proper data ordering when using NonAtomicFeatureStoreCoreInitialization.

Also, if the FeatureStoreCore object implements ldclient.FeatureStoreStatusProvider, the wrapper will make it possible for SDK components to react appropriately if the availability of the store changes (e.g. if we lose a database connection, but then regain it).

func NewFeatureStoreWrapper

func NewFeatureStoreWrapper(core FeatureStoreCore) *FeatureStoreWrapper

NewFeatureStoreWrapper creates an instance of FeatureStoreWrapper that wraps an instance of FeatureStoreCore. Deprecated: Use NewFeatureStoreWrapperWithConfig.

func NewFeatureStoreWrapperWithConfig

func NewFeatureStoreWrapperWithConfig(core FeatureStoreCore, config ld.Config) *FeatureStoreWrapper

NewFeatureStoreWrapperWithConfig creates an instance of FeatureStoreWrapper that wraps an instance of FeatureStoreCore. It takes a Config parameter so that it can use the same logging configuration as the SDK.

func NewNonAtomicFeatureStoreWrapper

func NewNonAtomicFeatureStoreWrapper(core NonAtomicFeatureStoreCore) *FeatureStoreWrapper

NewNonAtomicFeatureStoreWrapper creates an instance of FeatureStoreWrapper that wraps an instance of NonAtomicFeatureStoreCore.

func NewNonAtomicFeatureStoreWrapperWithConfig

func NewNonAtomicFeatureStoreWrapperWithConfig(core NonAtomicFeatureStoreCore, config ld.Config) *FeatureStoreWrapper

NewNonAtomicFeatureStoreWrapperWithConfig creates an instance of FeatureStoreWrapper that wraps an instance of NonAtomicFeatureStoreCore. It takes a Config parameter so that it can use the same logging configuration as the SDK.

func (*FeatureStoreWrapper) All

All retrieves all items of the specified kind, with optional caching.

func (*FeatureStoreWrapper) Close

func (w *FeatureStoreWrapper) Close() error

Close releases any resources being held by the store.

func (*FeatureStoreWrapper) Delete

func (w *FeatureStoreWrapper) Delete(kind ld.VersionedDataKind, key string, version int) error

Delete deletes an item, with optional caching.

func (*FeatureStoreWrapper) Get

Get retrieves a single item by key, with optional caching.

func (*FeatureStoreWrapper) GetDiagnosticsComponentTypeName

func (w *FeatureStoreWrapper) GetDiagnosticsComponentTypeName() string

Used internally to describe this component in diagnostic data.

func (*FeatureStoreWrapper) GetStoreStatus

func (w *FeatureStoreWrapper) GetStoreStatus() internal.FeatureStoreStatus

GetStoreStatus returns the current status of the store.

func (*FeatureStoreWrapper) Init

Init performs an update of the entire data store, with optional caching.

func (*FeatureStoreWrapper) Initialized

func (w *FeatureStoreWrapper) Initialized() bool

Initialized returns true if the feature store contains a data set. To avoid calling the underlying implementation any more often than necessary (since Initialized is called often), FeatureStoreWrapper uses the following heuristic: 1. Once we have received a true result from InitializedInternal, we always return true. 2. If InitializedInternal returns false, and we have a cache, we will cache that result so we won't call it any more frequently than the cache TTL.

func (*FeatureStoreWrapper) StatusSubscribe

StatusSubscribe creates a channel that will receive all changes in store status.

func (*FeatureStoreWrapper) Upsert

Upsert updates or adds an item, with optional caching.

type NonAtomicFeatureStoreCore

type NonAtomicFeatureStoreCore interface {
	FeatureStoreCoreBase
	// InitCollectionsInternal replaces the entire contents of the data store. The SDK will
	// pass a data set with a defined ordering; the collections (kinds) should be processed in
	// the specified order, and the items within each collection should be written in the
	// specified order. The store should delete any obsolete items only after writing all of
	// the items provided.
	InitCollectionsInternal(allData []StoreCollection) error
}

NonAtomicFeatureStoreCore is an interface for a limited subset of the functionality of ldclient.FeatureStore, to be used in conjunction with FeatureStoreWrapper. This allows developers of custom FeatureStore implementations to avoid repeating logic that would commonly be needed in any such implementation, such as caching. Instead, they can implement only FeatureStoreCore and then call NewFeatureStoreWrapper.

This interface assumes that the feature store cannot update the data set atomically and will require the SDK to specify the order of operations. If atomic updates are possible, then use FeatureStoreCore instead. FeatureStoreCoreBase defines the common methods.

Note that this is somewhat different from the way the LaunchDarkly SDK addresses the atomicity issue on most other platforms. There, the feature stores just have one interface, which always receives the data as a map, but the SDK can control the iteration order of the map. That isn't possible in Go where maps never have a defined iteration order.

type StoreCollection

type StoreCollection struct {
	Kind  ld.VersionedDataKind
	Items []ld.VersionedData
}

StoreCollection is used by the NonAtomicFeatureStoreCore interface.

Jump to

Keyboard shortcuts

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