dinolith

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 19, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddDependency

func AddDependency[T any](dm *DependencyManager, depenency Acquirable[T]) (T, error)

Acquires dependency and adds it to the DependencyManager.

func EnsureType

func EnsureType[T any](anyConf any) (T, error)

Types

type Acquirable

type Acquirable[T any] interface {
	// Underlying context is canceled once parent context
	// was canceled and all users released their value.
	ContextHolder

	// Acquires underlying value. Returns error if parent context was canceled.
	Acquire() (T, error)

	// Releases underlying value.
	Releasable
}

func NewAcquirable

func NewAcquirable[T any](parent context.Context, getStored func() T) Acquirable[T]

type ContextHolder

type ContextHolder interface {
	Context() context.Context
}

type DependencyManager

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

func (*DependencyManager) ReleaseAll

func (dm *DependencyManager) ReleaseAll()

Releases all held dependencies.

type Initializable

type Initializable interface {
	// Initializes the component. Subsequent calls to Initialize are
	// prohibited, so if the component is initialized for the second
	// time, call to Initialize is undefined behaviour.
	Initialize(config any, ctx context.Context, log logr.Logger) error
}

type Lifecycle

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

func (*Lifecycle) Initialize

func (lc *Lifecycle) Initialize(anyConf any, ctx context.Context, log logr.Logger) error

Initializes underlying services. anyConf must be of type LifecycleConfig. Provided ctx and log are used only for managed services; logger for lifecycle is provided via config.

func (*Lifecycle) Run

func (lc *Lifecycle) Run() error

Runs all underlying services and aborts if running any of them resulted in error. Blocks until all services are stopped.

type LifecycleConfig

type LifecycleConfig struct {
	// Logger, used by lifecycle itself. If not set, mocked by dummy logger.
	Logger logr.Logger

	// List of services, managed by lifecycle.
	Services []ServiceConfig
}

type Provider

type Provider[T any] interface {
	Runnable
	Acquirable[T]
}

type Releasable

type Releasable interface {
	Release()
}

type Runnable

type Runnable interface {
	// Runnable is always initialized exactly once before being started.
	// In initialization phaze not all services might be available yet, so it's
	// not allowed to acquire any dependencies at this point. Any dependencies
	// must be acquired at the beginning of call to Run().
	Initializable

	// Runs the service. If it depends on any other services, it must acquire them
	// right after call to Run(), just before running. If any fatal error occures
	// during this process initialization, returns it; otherwise blocks until service
	// is stopped and returns nil.
	Run() error
}

type ServiceConfig

type ServiceConfig struct {
	// Name of the service. Used for identifying service in lifecycle logs
	// and adding name for service logger.
	Name string

	// Instance of the service.
	Instance Runnable

	// Config for service. Must be acceptable by Instance.Initialize.
	Config any
}

Jump to

Keyboard shortcuts

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