component

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2024 License: BSD-3-Clause Imports: 30 Imported by: 3

Documentation

Index

Constants

View Source
const (
	Submitted stage = iota
	Preinitializing
	Preinitialized
	Initializing
	Initialized
	Starting
	Restarting
	Started
	Stopping
	Stopped
	Tearingdown
	Teareddown
	// Error stages follow
	Aborting
)
View Source
const (
	Inactive state = iota
	Active
)
View Source
const (
	EnablePeerMessaging controlMsg = iota
	DisablePeerMessaging
	NotifyPeers
	Persist
	Restart
	RestartAfter
	RestartMmux
	Shutdown
	ShutdownAfter
	Cancel
	CancelAfter
)

Variables

This section is empty.

Functions

func AttachComponent added in v0.3.0

func AttachComponent(isHead bool, addon Component)

AttachComponent used for activating add-on components which attaches to the root container. Add-on components could be optionally included by means of golang build tags. Refer 'addons' package for all available add-on components.

func ContextWithLogger added in v0.5.0

func ContextWithLogger(ctx context.Context, logger *zap.Logger) context.Context

func LoggerFromContext added in v0.5.0

func LoggerFromContext(ctx context.Context) *zap.Logger

func MarshallToHttpResponseWriter

func MarshallToHttpResponseWriter(w http.ResponseWriter, comp Component)

func UpdateComponentEtag added in v0.6.0

func UpdateComponentEtag(comp Component) error

UpdateComponentEtag calculates the component hash and sets an Entity Tag(ETag) to indicate a version of the component. this function is not thread safe. caller should ensure that the function is called within a critical section or call hiearchy traces back to one.

Types

type ChangeObject added in v1.0.0

type ChangeObject interface {
	GetPreviousObject() interface{}
	GetCurrentObject() interface{}
	fmt.Stringer
}

Immutable copies of the previous and current objects of any type could be encapsulated as ChangeObject interface, which serves as the data format for message processing. The main ChangeObjects are StateChangeObject, StageChangeObject & ComponentChangeObject

type Component

type Component interface {
	GetName() string

	GetURI() string

	GetStage() stage

	GetState() state

	// Init method should include logic which finishes with reasonable amount of time, since it blocks initializing other components for the application
	// unless this has finished executing. Exceptions to this could be infra components which handle database or messaging requirements, which other
	// components might be dependent for proper functioning.
	Init(context.Context) error

	// components which are containers, that embed Container type, should make use of PostInit to initialize aspects specific to it. Same limitations that
	// apply to the Init method, in terms of what all logic could be included, would be applicable to PostInit method as well.
	PostInit(context.Context) error

	// The PreStart stage is suitable for components that initiate other components, as it occurs after all prerequisites, such as converting to canonical form
	// and container assignment, have been completed.
	PreStart(context.Context) error

	Start(context.Context) error
	Stop(context.Context) error

	// SendSyncMessage is a blocking call for sending any type of message/s to a component.
	// msgType could be used by Components' to define its own set of message types. Used in conjunction with msgsLookup in determining the
	// message type associated to the MsgType, and invoking the appropriate handler function registered to process the message type.
	// If handler functions are not registered for specific message types, all messages would be forwarded to the DefaultSyncMessageHandler.
	// Message types could be batched as a slice to process the messages in-order, thereby facilitating atomic transaction processing.
	//
	// Changes made to component fields outside of expected channels or methods could be persisted (if persistence addon is enabled), by sending
	// a "ControlMsgType:Persist" message. In the same way out-of-band component changes could be notified to subscribers by sending "ControlMsgType:NotifyPeers" message
	SendSyncMessage(timeout time.Duration, msgType interface{}, msgsLookup map[interface{}]interface{}) error

	// To set blocking message handler functions for any message types. Components could define its own message types.
	SetSyncMessageHandler(msgType interface{}, msgTypeHandler func(context.Context, interface{}) error)

	// DefaultSyncMessageHandler is a blocking call which synchronously handles all messages except ControMsgType types. Messages gets routed by invoking SendSyncMessage.
	DefaultSyncMessageHandler(context.Context, interface{}) error

	// Callback could be used to register a callback function to receive ChangeObject notifications from a component. Notifications could be State, Stage or immutable Component
	// copies. All registered callback functions would be maintained within a function slice. Any time a callback function is registered with isHead = false would get appended
	// to end of the function slice. While a registration made with isHead = true, would result in the callback function getting added to index 0 (head) of the function slice
	// and shifting any existing functions by 1 index. Repeated adds with isHead = true would be like adding to head of a LIFO stack.
	//
	// Note that the registered callback functions would be sent notifications prior to any subscribers, by iterating across the function slice and executing each
	// function sequentially with a timeout conveyed within the context parameter. function slice index is also passed as a parameter which could be used to
	// de-register the callback function.
	// Callback functions could be registered in this manner, thereby maintaining order of execution to process a notification across components.
	Callback(isHead bool, callback func(ctx context.Context, cbIndx int, changeObject ChangeObject)) error
	RemoveCallback(cbIndx int) error

	// Subscribers could pass a channel to receive ChangeObject notifications of components it might be interested. Notifications could be State, Stage or immutable Component
	// copies.
	//
	// Note that the callback functions registered using the Callback method would be sent notifications prior to any subscribers.
	Subscribe(subscriber string, subscriberCh chan<- ChangeObject) error
	Unsubscribe(subscriber string) error

	// IsRestartableWithDelay indicates if component is to be restarted if Start() fails with error. The method could include logic for exponential backoff
	// to return the delay duration between restarts.
	IsRestartableWithDelay() (bool, time.Duration)

	GetContainer() *Container

	Hash() uint64

	GetEtag() string

	fmt.Stringer
	http.Handler

	SetAsNonRestEntity(bool)
	IsNonRestEntity() bool

	GetLogger() *zap.Logger
	// contains filtered or unexported methods
}

type ComponentChangeObject added in v1.0.0

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

func (ComponentChangeObject) GetCurrentObject added in v1.0.0

func (c ComponentChangeObject) GetCurrentObject() interface{}

func (ComponentChangeObject) GetPreviousObject added in v1.0.0

func (c ComponentChangeObject) GetPreviousObject() interface{}

func (ComponentChangeObject) String added in v1.0.0

func (c ComponentChangeObject) String() string

type Connection added in v0.3.0

type Connection interface {
	Connect(ctx context.Context, options ...interface{}) error
	Disconnect(ctx context.Context, options ...interface{}) error
}

Connection represents an interface for managing database connections.

type Container

type Container struct {
	SimpleComponent
	// contains filtered or unexported fields
}

func (*Container) Add

func (c *Container) Add(comp Component) error

func (*Container) GetComponent

func (c *Container) GetComponent(name string) (Component, error)

GetComponent returns the component within a container.

func (*Container) GetComponentCopy added in v1.0.2

func (c *Container) GetComponentCopy(name string) (Component, error)

GetComponentCopy returns the copy of component within a container. Only exported field values would be copied over. Advisable not to mark pointer fields within a component as an exported field. Reference types such as slice, map, channel, interface, and function types which are exported would be copied over.

func (*Container) GetHttpHandler

func (c *Container) GetHttpHandler(URI string) func(w http.ResponseWriter, r *http.Request)

GetHttpHandler returns the longest matching URI prefix handler

func (*Container) Init

func (c *Container) Init(ctx context.Context) error

func (*Container) Matches added in v0.0.12

func (c *Container) Matches(comp Component) bool

compare if the passed component matches this container type

func (*Container) Stop

func (c *Container) Stop(ctx context.Context) error

1) A container (which is also a component) could be added and maintained within its own data structures to bootstrap itself. 2) It could also be added to a parent container and maintained within that container. 3) There ia a rare possibility that the container which is boostraping itself would appear later in the LIFO order, than a component it holds.

So would have to factor the afore mentioned cases while "Stopping" a container in order that it has clean and consise logic. Note that the method sends Shutdown notification to the encompassed components and relies on removeComponent method to properly update the data structures. Both methods maintain clear separation of responsibilities.

type ContextType added in v1.0.1

type ContextType struct{}
var (
	TraceID ContextType
)

type MsgType added in v0.1.0

type MsgType string

MsgType could be used by Components' to define its own set of message types. Used in conjunction with a lookup map in determining the message classification associated to the MsgType, and invoking the appropriate handler function registered to process the message type.

const (
	ControlMsgType   MsgType = "ControlMsgType"   // ControlMsgType to classify controlMsg
	ComponentMsgType MsgType = "ComponentMsgType" // ComponentMsgType to classify any Component type being passed
)

type NoSQLDB added in v0.3.0

type NoSQLDB interface {
	Connection
	Insert(ctx context.Context, collection string, document interface{}, args ...interface{}) error
	Update(ctx context.Context, collection string, filter interface{}, update interface{}, args ...interface{}) error
	Delete(ctx context.Context, collection string, filter interface{}, args ...interface{}) error
	FindOne(ctx context.Context, collection string, filter interface{}, result interface{}, args ...interface{}) error
	Find(ctx context.Context, collection string, filter interface{}, args ...interface{}) ([]interface{}, error)
}

NoSQLDB represents the interface for a NoSQL database.

type PType added in v0.3.0

type PType struct {
	Name          string      `json:"name"`
	TypeHierarchy string      `json:"typeHierarchy"`
	Value         interface{} `json:"value"`
	IsEncrypted   bool        `json:"isEncrypted"`
}

type PTypes added in v0.3.0

type PTypes []PType

type Persistence added in v0.3.0

type Persistence struct {
	SimpleComponent
	DB Connection
	// contains filtered or unexported fields
}

func (*Persistence) Init added in v0.3.0

func (p *Persistence) Init(ctx context.Context) error

Init includes checks for singleton Persistence

func (*Persistence) Stop added in v0.3.0

func (p *Persistence) Stop(ctx context.Context) error

type RelationalDB added in v0.3.0

type RelationalDB interface {
	Connection
	Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
	QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row
	Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
	BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
}

RelationalDB represents the interface for a relational database.

type SimpleComponent

type SimpleComponent struct {
	Etag string `json:"etag" hash:"ignore"`

	Name string `json:"name"`

	Stage stage `json:"stage"`
	State state `json:"state"`
	// contains filtered or unexported fields
}

func (*SimpleComponent) Callback added in v0.0.9

func (d *SimpleComponent) Callback(isHead bool, callback func(ctx context.Context, cbIndx int, changeObj ChangeObject)) error

func (*SimpleComponent) DefaultSyncMessageHandler added in v0.1.0

func (d *SimpleComponent) DefaultSyncMessageHandler(context.Context, interface{}) error

func (*SimpleComponent) GetContainer

func (d *SimpleComponent) GetContainer() *Container

func (*SimpleComponent) GetEtag

func (d *SimpleComponent) GetEtag() string

func (*SimpleComponent) GetLogger added in v0.5.0

func (d *SimpleComponent) GetLogger() *zap.Logger

func (*SimpleComponent) GetName

func (d *SimpleComponent) GetName() string

func (*SimpleComponent) GetStage

func (d *SimpleComponent) GetStage() stage

func (*SimpleComponent) GetState

func (d *SimpleComponent) GetState() state

func (*SimpleComponent) GetURI added in v0.0.7

func (d *SimpleComponent) GetURI() string

func (*SimpleComponent) Hash

func (d *SimpleComponent) Hash() uint64

func (*SimpleComponent) Init

components which use SimpleComponent as embedded type could override this method to have custom implementation. Refer notes within Component interface for implementing the method.

func (*SimpleComponent) IsNonRestEntity added in v0.1.0

func (d *SimpleComponent) IsNonRestEntity() bool

func (*SimpleComponent) IsRestartableWithDelay

func (d *SimpleComponent) IsRestartableWithDelay() (bool, time.Duration)

components which use SimpleComponent as embedded type could override this method to have custom implementation. Refer notes within Component interface for implementing the method.

func (*SimpleComponent) PostInit added in v0.1.0

func (d *SimpleComponent) PostInit(context.Context) error

components which use Container as embedded type could override this method to have custom implementation. Refer notes within Component interface for implementing the method.

func (*SimpleComponent) PreStart added in v0.5.1

func (d *SimpleComponent) PreStart(context.Context) error

components that initiate other components could override this method to have custom implementation. Refer notes within Component interface for implementing the method.

func (*SimpleComponent) RemoveCallback added in v0.0.9

func (d *SimpleComponent) RemoveCallback(cbIndx int) error

func (*SimpleComponent) SendSyncMessage added in v0.1.0

func (d *SimpleComponent) SendSyncMessage(timeout time.Duration, msgType interface{}, msgsLookup map[interface{}]interface{}) error

func (*SimpleComponent) ServeHTTP

func (d *SimpleComponent) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*SimpleComponent) SetAsNonRestEntity added in v0.1.0

func (d *SimpleComponent) SetAsNonRestEntity(set bool)

func (*SimpleComponent) SetSyncMessageHandler added in v0.1.0

func (d *SimpleComponent) SetSyncMessageHandler(msgType interface{}, msgTypeHandler func(context.Context, interface{}) error)

func (*SimpleComponent) Start

components which use SimpleComponent as embedded type could override this method to have custom implementation. Refer notes within Component interface for implementing the method.

func (*SimpleComponent) Stop

components which use SimpleComponent as embedded type could override this method to have custom implementation. Refer notes within Component interface for implementing the method.

func (*SimpleComponent) String

func (d *SimpleComponent) String() string

func (*SimpleComponent) Subscribe

func (d *SimpleComponent) Subscribe(subscriber string, subscriberCh chan<- ChangeObject) error

func (*SimpleComponent) Unsubscribe added in v0.0.9

func (d *SimpleComponent) Unsubscribe(subscriber string) error

type StageChangeObject added in v1.0.0

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

func (StageChangeObject) GetCurrentObject added in v1.0.0

func (c StageChangeObject) GetCurrentObject() interface{}

func (StageChangeObject) GetPreviousObject added in v1.0.0

func (c StageChangeObject) GetPreviousObject() interface{}

func (StageChangeObject) String added in v1.0.0

func (c StageChangeObject) String() string

type StateChangeObject added in v1.0.0

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

func (StateChangeObject) GetCurrentObject added in v1.0.0

func (c StateChangeObject) GetCurrentObject() interface{}

func (StateChangeObject) GetPreviousObject added in v1.0.0

func (c StateChangeObject) GetPreviousObject() interface{}

func (StateChangeObject) String added in v1.0.0

func (c StateChangeObject) String() string

Jump to

Keyboard shortcuts

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