chassis

package module
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2024 License: Apache-2.0 Imports: 39 Imported by: 28

Documentation

Overview

Draft is a light weight micro-services chassis used to sow a few runtime dependances together

Index

Constants

View Source
const (
	SYNC_INTERVAL     = 5 * time.Second
	INTITIALIZE_LIMIT = 5
)
View Source
const (
	ErrProcessRegistrationFailed = "failed to connect to blueprint"
)

///////////////// System Functions /////////////////

View Source
const (
	FuseAddressBlueprintKey = "fuse_address"
)

Variables

This section is empty.

Functions

func Unwrap

func Unwrap(err error) error

Unwrap recursively unwraps the error (if Error type) until it reaches the root error

Types

type Broker

type Broker interface {
	Open(ctx context.Context, config Config) error
	Publish(ctx context.Context, options PublishOptions) error
	Subscribe(ctx context.Context, options SubscribeOptions) error
	Unsubscribe(ctx context.Context, options UnsubscribeOptions) error
	Close(force bool) error
}

type CloseChan

type CloseChan = chan os.Signal

CloseChan is used to signal to the runtime that all servers, and connections need to be closed down.

type Config

type Config interface {
	Name() string
	Domain() string
	NodeID() string
	Title() string
	Env() string
	Reader
	Entrypoint() string
}

func GetConfig

func GetConfig() Config

func LoadConfig

func LoadConfig() Config

TODO -> Read config from the key/value store and not from a local static file.

type ConsensusKind

type ConsensusKind int
const (
	NullConsensusKind ConsensusKind = iota
	Raft
)

func (ConsensusKind) String

func (ck ConsensusKind) String() string

String - get the human readable value for `ConsensusKind`

type ConsensusRegistrar

type ConsensusRegistrar interface {
	RegisterConsensus(interface{}) error
	// LeadershipChange is a callback for the consensus system to notify when a leadership change is made. The
	// leader parameter is true when the node becomes the leader, and false when it becomes a follower. The address
	// parameter is the URL of this node and should be persisted to raft for other nodes to forward write requests to.
	LeadershipChange(log Logger, leader bool, address string)
	raft.FSM
}

type Consumer

type Consumer interface {
	Consume(ctx context.Context, event *events.CloudEvent) error
}

type Default

type Default interface {
	RPCRegistrar
	ConsensusRegistrar
}

type Error

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

Error is a custom error type that wraps a standard Go error with additional contextual information

func Wrap

func Wrap(e error, f Fields) Error

Wrap creates a customError from a given standard Go error and logger fields. It pulls out the caller function name, file name, and line number from the runtime.

func (Error) Error

func (r Error) Error() string

Error returns the error message as a string in the form of:

"[main.FunctionA]->[module/package1.FunctionB]->[module/package2.FunctionC]->[original error message]"

func (Error) Fields

func (r Error) Fields() Fields

Fields returns the logger fields from the context of the root error (the lowest `logger.WrapError()` call on the call stack). This preserves the logger context which will have logger fields added throughout the call stack down to where the error was created.

func (Error) Unwrap

func (r Error) Unwrap() error

Unwrap returns the underlying error. If wrapping has occurred it will take the shape of:

"[main.FunctionA]->[module/package1.FunctionB]->[module/package2.FunctionC]->[original error message]"

type Fields

type Fields map[string]any

Fields is an alias primarily used for Logger methods

type LogLevel

type LogLevel uint32

LogLevel type

const (
	// PanicLevel level, highest level of severity. Logs and then calls panic with the
	// message passed to Debug, Info, ...
	PanicLevel LogLevel = iota
	// FatalLevel level. Logs and then calls `os.Exit(1)`. It will exit even if the
	// logging level is set to Panic.
	FatalLevel
	// ErrorLevel level. Logs. Used for errors that should definitely be noted.
	// Commonly used for hooks to send errors to an error tracking service.
	ErrorLevel
	// WarnLevel level. Non-critical entries that deserve eyes.
	WarnLevel
	// InfoLevel level. General operational entries about what's going on inside the
	// application.
	InfoLevel
	// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
	DebugLevel
	// TraceLevel level. Designates finer-grained informational events than the Debug.
	TraceLevel
)

func ParseLogLevel

func ParseLogLevel(level string) LogLevel

ParseLogLevel takes a string level and returns the log level constant.

func (LogLevel) String

func (l LogLevel) String() string

type Logger

type Logger interface {
	// Start configures the logger for service startup
	Start(config Config)
	// SetLevel sets the logging level for the logger
	SetLevel(level LogLevel)
	// GetLevel gets the current logging level for the logger
	GetLevel() LogLevel
	// Wrap wraps an error with the additional context of current
	// logger fields and call stack information.
	Wrap(error) error
	// WithError - Add an error as single field (using the key defined in ErrorKey) to the logger.
	WithError(err error) Logger
	// WithContext - Add a context to the logger.
	WithContext(ctx context.Context) Logger
	// WithField - Add a single field to the logger.
	WithField(key string, value any) Logger
	// WithFields - Add a map of fields to the logger.
	WithFields(fields Fields) Logger
	// Trace - Definition:
	// "Seriously, WTF is going on here?!?!
	// I need to log every single statement I execute to find this @#$@ing memory corruption bug before I go insane"
	Trace(msg string)
	// Debug - Definition:
	// Off by default, able to be turned on for debugging specific unexpected problems.
	// This is where you might log detailed information about key method parameters or
	// other information that is useful for finding likely problems in specific 'problematic' areas of the code.
	Debug(msg string)
	// Info - Definition:
	// Normal logging that's part of the normal operation of the app;
	// diagnostic stuff so you can go back and say 'how often did this broad-level operation happen?',
	// or 'how did the user's data get into this state?'
	Info(msg string)
	// Warn - Definition:
	// something that's concerning but not causing the operation to abort;
	// # of connections in the DB pool getting low, an unusual-but-expected timeout in an operation, etc.
	// Think of 'WARN' as something that's useful in aggregate; e.g. grep, group,
	// and count them to get a picture of what's affecting the system health
	Warn(msg string)
	// Error - Definition:
	// something that the app's doing that it shouldn't.
	// This isn't a user error ('invalid search query');
	// it's an assertion failure, network problem, etc etc.,
	// probably one that is going to abort the current operation
	Error(msg string)
	// WrappedError - Definition:
	// this is a convenience method that calls Error() but makes sure to wrap the error a final time
	// so that all current call context is included in the error. This has the same output as:
	//   logger.WithFields(logger.WrapError(err).Fields()).WithError(logger.WrapError(err)).Error("failed to process request")
	// but instead has a much simpler oneliner of:
	//   logger.WrappedError(err, "failed to process request")
	WrappedError(error, string)
	// Fatal - Definition:
	// the app (or at the very least a thread) is about to die horribly.
	// This is where the info explaining why that's happening goes.
	Fatal(msg string)
	// Panic - Definition:
	// Be careful about calling this vs Fatal:
	// - For Fatal level, the log message goes to the configured log output, while panic is only going to write to stderr.
	// - Panic will print a stack trace, which may not be relevant to the error at all.
	// - Defers will be executed when a program panics, but calling os.Exit exits immediately, and deferred functions can't be run.
	// In general, only use panic for programming errors, where the stack trace is important to the context of the error.
	// If the message isn't targeted at the programmer, you're simply hiding the message in superfluous data.
	Panic(msg string)
	// Envoy control plane logger. The `envoyproxy` had defined it's own `log.Logger` interface we are embedding here so the same abstraction
	// can be used for logging in the control plane as well.
	envoy.Logger
}

type PublishOptions

type PublishOptions struct {
	Event *events.CloudEvent
	// Tags are an optional list of strings that can be used to contextualize the event. For example, a tag could be used in
	// a blue/green deployment scenario to indicate which version of the application published the event. Keep in mind that tags
	// are only *contextual* data and though they do affect the routing of the event, they do not affect the content of the event.
	// For a consumer to receive an event, it must have a matching event type and all tags that were used when the event was published.
	// Tags will be sorted alphabetically before being used to route the event so that the order of the tags does not matter.
	Tags []string
}

type RPCHandlerKind

type RPCHandlerKind int
const (
	NullRPCHandlerKind RPCHandlerKind = iota
	Grpc
)

type RPCRegistrar

type RPCRegistrar interface {
	RegisterRPC(server Rpcer)
}

type RaftController

type RaftController interface {
	Join(ctx context.Context, nodeID, raftAddress string) error
	Stats(ctx context.Context) map[string]string
}

func NewRaftController

func NewRaftController(r *raft.Raft) RaftController

type RaftRPCHandler

type RaftRPCHandler interface {
	RPCRegistrar
	rfConnect.RaftServiceHandler
}

func NewRaftRPCHandler

func NewRaftRPCHandler(raftController RaftController) RaftRPCHandler

type Reader

type Reader interface {
	Get(key string) interface{}
	GetString(key string) string
	GetBool(key string) bool
	GetInt(key string) int
	GetInt32(key string) int32
	GetInt64(key string) int64
	GetUint(key string) uint
	GetUint16(key string) uint16
	GetUint32(key string) uint32
	GetUint64(key string) uint64
	GetFloat64(key string) float64
	GetTime(key string) time.Time
	GetDuration(key string) time.Duration
	GetIntSlice(key string) []int
	GetStringSlice(key string) []string
	GetStringMap(key string) map[string]interface{}
	GetStringMapString(key string) map[string]string
	GetStringMapStringSlice(key string) map[string][]string
	GetSizeInBytes(key string) uint
	Unmarshal(rawVal interface{}, opts ...viper.DecoderConfigOption) error
	UnmarshalKey(key string, rawVal interface{}, opts ...viper.DecoderConfigOption) error
	SetDefault(key string, value any)
}

type RegistrationOptions

type RegistrationOptions struct {
	Namespace string
}

type Repository

type Repository interface {
	Open(ctx context.Context, config Config) (err error)
	Ping(ctx context.Context) (err error)
	Close(ctx context.Context) (err error)
}

type Rpcer

type Rpcer interface {
	AddHandler(pattern string, handler http.Handler, enableReflection bool)
	GetGrpcServer() *grpc.Server
}

type Runtime

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

func New

func New(logger Logger) *Runtime

func (*Runtime) DisableMux added in v0.3.2

func (c *Runtime) DisableMux() *Runtime

func (*Runtime) Register

func (c *Runtime) Register(options RegistrationOptions) *Runtime

func (*Runtime) Start

func (c *Runtime) Start()

Start the runtime of the service. This will do things like run the grpc server and consumers and put them on a background goroutine

func (*Runtime) WithBroker

func (c *Runtime) WithBroker(plugin Broker) *Runtime

func (*Runtime) WithClientApplication

func (c *Runtime) WithClientApplication(files embed.FS) *Runtime

func (*Runtime) WithConsensus

func (c *Runtime) WithConsensus(kind ConsensusKind, plugin ConsensusRegistrar) *Runtime

func (*Runtime) WithRPCHandler

func (c *Runtime) WithRPCHandler(plugin RPCRegistrar) *Runtime

func (*Runtime) WithRepository

func (c *Runtime) WithRepository(plugin Repository) *Runtime

func (*Runtime) WithRoute added in v0.3.0

func (c *Runtime) WithRoute(route *ntv1.Route) *Runtime

func (*Runtime) WithRunner added in v0.2.2

func (c *Runtime) WithRunner(f func()) *Runtime

WithRunner adds a function to be called in a goroutine when Runtime.Start is called

func (*Runtime) WithSecretStore

func (c *Runtime) WithSecretStore(plugin SecretStore) *Runtime

type SecretStore

type SecretStore interface {
	// Open will initialize the client with the given configuration
	Open(ctx context.Context, config Config) (err error)
	// Get retrieves a secret from the vault for the given key
	Get(ctx context.Context, key string) (value string, err error)
}

type SubscribeOptions

type SubscribeOptions struct {
	Event *events.CloudEvent
	// Tags are an optional list of strings that can be used to filter the events that are received. For example, a tag could be used
	// in a blue/green deployment scenario to filter out events that were published by the other version of the application. If no tags
	// are provided, all events that match the provided Event will be received.
	Tags     []string
	Consumer Consumer
	// Group is the name of the group the consumer belongs to. If not empty, messages delivered to the group
	// will be load-balanced across all consumers within the group. If empty, the consumer will receive all
	// messages that match the provided Event and Tags.
	//
	// WARNING: Make sure that this value is globally unique across all other groups that are consuming the same event. Otherwise
	// messages could be delivered to consumers unintentionally.
	Group string
	// IgnoreType states whether the subscribing consumer should ignore the event type when receiving messages. If true, the consumer
	// will receive all events that match Event.Source and any provided Tags. If false, the consumer will only receive events that match
	// Event.Source, Event.Type, and any provided Tags.
	IgnoreType bool
}

type UnsubscribeOptions

type UnsubscribeOptions struct {
	Event      *events.CloudEvent
	Tags       []string
	IgnoreType bool
}

Jump to

Keyboard shortcuts

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