baseservice

package
v0.14.3 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2024 License: MPL-2.0 Imports: 4 Imported by: 3

Documentation

Overview

Package baseservice contains structs and initialization functions for "service-like" objects that provide commonly needed facilities so that they don't have to be redefined on every struct. The word "service" is used quite loosely here in that it may be applied to many long-lived object that aren't strictly services (e.g. adapters).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Init

func Init[TService withBaseService](archetype *Archetype, service TService) TService

Init initializes a base service from an archetype. It returns the same service that was passed into it for convenience.

Types

type Archetype

type Archetype struct {
	// Logger is a structured logger.
	Logger *slog.Logger

	// Time returns a time generator to get the current time in UTC. Normally
	// it's implemented as UnStubbableTimeGenerator which just calls through to
	// `time.Now().UTC()`, but is riverinternaltest.timeStub in tests to allow
	// the current time to be stubbed.  Services should try to use this function
	// instead of the vanilla ones from the `time` package for testing purposes.
	Time TimeGenerator
}

Archetype contains the set of base service properties that are immutable, or otherwise safe for services to copy from another service. The struct is also embedded in BaseService, so these properties are available on services directly.

func NewArchetype added in v0.11.3

func NewArchetype(logger *slog.Logger) *Archetype

NewArchetype returns a new archetype. This function is most suitable for non-test usage wherein nothing should be stubbed.

type BaseService

type BaseService struct {
	Archetype

	// Name is a name of the service. It should generally be used to prefix all
	// log lines the service emits.
	Name string
}

BaseService is a struct that's meant to be embedded on "service-like" objects (e.g. client, producer, queue maintainer) and which provides a number of convenient properties that are widely needed so that they don't have to be defined on every individual service and can easily be copied from each other.

An initial Archetype should be defined near the program's entrypoint (currently in Client), and then each service should invoke Init along with the archetype to initialize its own base service. This is often done in the service's constructor, but if it doesn't have one, it's the job of the caller which instantiates it to invoke Init.

func (*BaseService) GetBaseService

func (s *BaseService) GetBaseService() *BaseService

type TimeGenerator

type TimeGenerator interface {
	// NowUTC returns the current time. This may be a stubbed time if the time
	// has been actively stubbed in a test.
	NowUTC() time.Time

	// NowUTCOrNil returns if the currently stubbed time _if_ the current time
	// is stubbed, and returns nil otherwise. This is generally useful in cases
	// where a component may want to use a stubbed time if the time is stubbed,
	// but to fall back to a database time default otherwise.
	NowUTCOrNil() *time.Time

	// StubNowUTC stubs the current time. It will panic if invoked outside of
	// tests. Returns the same time passed as parameter for convenience.
	StubNowUTC(nowUTC time.Time) time.Time
}

TimeGenerator generates a current time in UTC. In test environments it's implemented by riverinternaltest.timeStub which lets the current time be stubbed. Otherwise, it's implemented as UnStubbableTimeGenerator which doesn't allow stubbing.

type UnStubbableTimeGenerator

type UnStubbableTimeGenerator struct{}

UnStubbableTimeGenerator is a TimeGenerator implementation that can't be stubbed. It's always the generator used outside of tests.

func (*UnStubbableTimeGenerator) NowUTC

func (g *UnStubbableTimeGenerator) NowUTC() time.Time

func (*UnStubbableTimeGenerator) NowUTCOrNil

func (g *UnStubbableTimeGenerator) NowUTCOrNil() *time.Time

func (*UnStubbableTimeGenerator) StubNowUTC

func (g *UnStubbableTimeGenerator) StubNowUTC(nowUTC time.Time) time.Time

Jump to

Keyboard shortcuts

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