module

package
v0.20.1 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: Apache-2.0 Imports: 20 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package module is how an application mounts its topic managers and lookup services in the Go overlay engine.

It replaces the TypeScript host's Module/ModuleHost loader. Go has no runtime module loading worth using, so a module is compiled in: its package calls Register from init, the binary imports it, and OVERLAY_MODULES names which registered modules this host mounts. Each module is a thin adapter over its application's Go core: it holds no admission or lookup logic of its own that the core does not already hold.

The interfaces a module satisfies are bcommon's engine-neutral contract (github.com/lightwebinc/bcommon/module: TopicManager, LookupService and their payloads, Host, Module). A module written to that contract imports no engine; this package holds the registry, the runtime Host it is built with, and the adapters that make a contract manager an engine.TopicManager and a contract service an engine.LookupService.

Index

Constants

This section is empty.

Variables

View Source
var ErrCannotEvict = errors.New("module: the storage cannot forget an applied transaction")

ErrCannotEvict is a storage with no way to forget an applied transaction.

Functions

func LookupService added in v0.2.0

func LookupService(svc bmodule.LookupService) engine.LookupService

LookupService makes an engine.LookupService of a contract service. The engine hands every admission as Atomic BEEF; the adapter reshapes it to the service's AdmissionMode, and a spend to its SpendNotificationMode, and leaves out what the service did not ask for.

Every notification inside a submission (admitted, spent, no longer retained, evicted) is timed into the submission's notify stage: the engine tells every mounted lookup service of every admitted and spent output, and a service's own indexing (a journal write, say) runs there.

func Output added in v0.2.0

func Output(o *engine.Output) (*bmodule.Output, error)

Output makes a contract row of an engine row. The script and satoshis come from the row's BEEF, which the engine's reads include when asked; a row read without it has neither, which a restore that needs them refuses through its own check.

func Register

func Register(name string, f Factory)

Register makes a module available under name. Call it from init. A name registered twice panics: two modules under one name is a build error.

func Registered

func Registered() []string

Registered lists registered module names, sorted.

func TopicManager added in v0.2.0

func TopicManager(tm bmodule.TopicManager) engine.TopicManager

TopicManager makes an engine.TopicManager of a contract manager. A manager that is not an InputIdentifier needs no inputs fetched.

Types

type Background added in v0.9.0

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

Background is the life of the modules' background work on one host.

func NewBackground added in v0.9.0

func NewBackground() *Background

NewBackground starts a background life.

func (*Background) Stop added in v0.9.0

func (b *Background) Stop(wait time.Duration) bool

Stop ends the background context and waits, at most wait, for every function Go started to return. It reports whether they all did.

type Change added in v0.20.0

type Change interface {
	// Module is the module under the new settings. The host merges it
	// before Apply, and Apply runs only if the merge holds.
	Module() Module
	// Topics are the topics the change adds, whose unspent rows Apply is
	// handed, read the way a restore reads them. Each must be managed by
	// Module and not by the module as it runs: the host builds the module
	// afresh otherwise, so it never hands a module rows it already holds.
	Topics() []string
	// Apply makes the change. It forgets what the module held for the
	// topics Module no longer manages (the host tells it nothing of them),
	// and takes in the rows of Topics.
	//
	// The host holds admissions from before it reads the rows until Apply
	// returns, so no admission reaches the module meanwhile. Everything else
	// may run concurrently with Apply, and the module must make its change
	// safe against it: lookups (the previous layer goes on answering them),
	// a topic manager's Precheck (the pipeline runs it before the hold's
	// gate), and the module's own background work, which is not stopped.
	//
	// A failed Apply may leave the module half changed, so the host then
	// builds it afresh.
	Apply(ctx context.Context, rows []bmodule.Output, seam bmodule.RestoreStorage) (int, error)
}

Change is a change of settings that a built module takes in place on a reload, without being built again (TakeChanges).

type Changer added in v0.20.0

type Changer func(getenv func(string) string) (Change, error)

Changer turns new settings into a Change. It returns nil when it cannot take them in place, and the host then builds the module afresh. It returns an error for settings a new build would refuse; the reload then fails and nothing changes. It must not change the module: only Apply does.

type Factory

type Factory func(h Host) (Module, error)

Factory builds a module for one host.

func Adapt added in v0.2.0

func Adapt(f bmodule.Factory) Factory

Adapt makes a Factory of a contract factory, for a module that needs nothing of the runtime Host beyond the contract.

type Host

type Host struct {
	// Topics are the topics this host carries (OVERLAY_TOPICS). A module may
	// claim only topics in this list.
	Topics []string
	// Storage is the engine's storage, for lookup services that answer from
	// it rather than from an index of their own.
	Storage engine.Storage
	// Registerer is the host's metrics registry; a module's families use its
	// own prefix (bstore_, bbox_ ...), never overlay_go_.
	Registerer prometheus.Registerer
	Log        *slog.Logger
	// Getenv reads the module's settings: the host's settings source, which
	// a reload reads afresh (OVERLAY_ENV_FILE). Nil reads the process
	// environment. A module reads its settings while it is built: a reload
	// keeps an instance whose build-time reads are unchanged (Reusable), so a
	// setting read later is neither compared nor refreshed.
	//
	// A module whose topics follow the host's list reads it through
	// CarriedTopics, never the Topics field, so a change of the list builds it
	// again.
	Getenv func(string) string

	// Background is the life of the module's background work (a terms
	// route, a retention tick): its context ends when the host closes, and
	// the host waits for what Go started before it closes its storage. Nil
	// in a test that builds a Host by hand; Go then runs fn under a context
	// that never ends.
	Background *Background
	// contains filtered or unexported fields
}

Host is what a module is handed when it is built: the runtime this engine offers beyond the contract. Contract gives the contract's view of it.

func (Host) CarriedTopics added in v0.19.0

func (h Host) CarriedTopics() []string

CarriedTopics is Topics, for a module whose topics follow the host's list (anytx): reading it through here marks the module to be built again when the list changes (Instance.Reusable).

func (Host) Contract added in v0.2.0

func (h Host) Contract() bmodule.Host

Contract is the bcommon module.Host this runtime host presents to a module written against the contract alone: its log and metrics, nothing of the engine.

func (Host) Env added in v0.19.0

func (h Host) Env() func(string) string

Env is the settings reader a module is built with: Getenv, else the process environment.

func (Host) Go added in v0.9.0

func (h Host) Go(fn func(ctx context.Context))

Go runs fn in a goroutine under the host's background context.

func (Host) TakeChanges added in v0.20.0

func (h Host) TakeChanges(keys []string, fn Changer)

TakeChanges says the module can take, in place, a change of the settings in keys and nothing else: a reload that changes only those calls fn instead of building the module again. The module keeps its background work and its in-memory index, so the time a change takes is the change's own, not a restore of everything the module holds. Any other change, and a module that reads the host's list (CarriedTopics) when the list changes, is built afresh.

type Instance added in v0.19.0

type Instance struct {
	Name   string
	Module Module
	BG     *Background
	// Reads are the settings the factory read, with the values read.
	Reads map[string]string
	// UsedTopics: the factory read the host's topic list (CarriedTopics).
	UsedTopics bool
	// Fresh: built for this host, so its lookups restore their indexes;
	// false for an instance kept from the previous host.
	Fresh bool
	// contains filtered or unexported fields
}

Instance is one built module: what its factory returned, the life of its background work, and what it read to be built. A reload keeps an instance whose settings did not change (Reusable), with its in-memory index and its background work running, and builds only the others.

func Build added in v0.19.0

func Build(name string, h Host) (*Instance, error)

Build builds one registered module with a background life of its own and a record of the settings it read.

func (*Instance) Applied added in v0.20.0

func (i *Instance) Applied() bool

Applied is whether a change was applied to the instance: the module's state is the new settings', whatever its Module says.

func (*Instance) Changed added in v0.20.0

func (i *Instance) Changed(get func(string) string, topicsChanged bool) (*Instance, error)

Changed is the instance as it is under the settings get reads, taken in place: nil when the change is not one the module takes (Reusable is false and a setting outside the module's TakeChanges keys changed, or the module returned nil), else a new Instance sharing this one's background work, with the change pending for ApplyChanges. An error is settings a new build would refuse. The instance itself is never changed.

func (*Instance) Reusable added in v0.19.0

func (i *Instance) Reusable(get func(string) string, topicsChanged bool) bool

Reusable is whether the instance would be built the same now: every setting it read reads the same through get, and, if it read the host's topic list (CarriedTopics), the list is the same.

OVERLAY_TOPICS, read by an app module, is compared only over the module's own family: the carried topics under a prefix of the topics it claims or under a longer word starting with it (tm_bstore_ also covers tm_bstoreblob_, so an unconfigured bucket's blob topic rebuilds bstore and is refused as a restart would), the topic itself for one with no further part (tm_finger), and any topic with the name of one it claims (a bucket's blob topic arriving beside its records topic). Errors are toward rebuilding. The app modules read the list only to check that every carried topic of their family is one they were configured for (bbox, bchat, blogs, borg, bsecret, bstore, bfinger at their pinned versions), so a topic of another family cannot change what they build. A module that claims nothing is compared over the whole list.

type Module

type Module = bmodule.Module

Module is what a factory returns: the contract's Module, managers keyed by tm_* topic name and lookup services keyed by ls_* name.

type Mounted

type Mounted struct {
	TopicManagers  map[string]engine.TopicManager
	LookupServices map[string]engine.LookupService
	// Owner says which module mounted each topic and lookup name.
	Owner map[string]string
	// Restorers are the mounted lookup services that rebuild an index at
	// startup, by name. Restore runs them.
	Restorers map[string]bmodule.Restorer
	// RestoreTopics are, by restorer name, the topics its module manages:
	// the rows Restore hands it. None (a module that manages no topic) is
	// every carried topic.
	RestoreTopics map[string][]string
	// Replayers are, by topic, the replayer of the module that manages the
	// topic: its lookup service that is a bcommon module.Replayer (the
	// first by name, if it has two). Catch-up asks it for the topics GASP
	// never carries (ruling R-o).
	Replayers map[string]bmodule.Replayer
	// Prechecks are, by topic, the managers that are Prechecker.
	Prechecks map[string]Prechecker
	// Instances are the modules mounted, in order.
	Instances []*Instance
}

Mounted is the result of Mount: what the engine is built with.

func Merge added in v0.19.0

func Merge(insts []*Instance, h Host, m *metrics.Metrics) (*Mounted, error)

Merge makes the instances, in order, one mount for the engine. It refuses rather than resolves every conflict, and it refuses a carried topic with no manager: a host must never admit a topic under a manager nobody chose.

  • a topic outside h.Topics, or claimed by two modules;
  • a lookup name claimed by two modules;
  • a carried topic no module claims.

func Mount

func Mount(names []string, h Host, m *metrics.Metrics) (*Mounted, error)

Mount builds the named modules in order and merges them.

func (*Mounted) ApplyChanges added in v0.20.0

func (mt *Mounted) ApplyChanges(ctx context.Context, h Host) (map[string]int, error)

ApplyChanges applies the changes pending on the mounted instances (Instance.Changed), each with its topics' unspent rows, and returns the count each indexed. It runs where Restore does: after the merge held and before the engine serves.

func (*Mounted) Restore added in v0.2.0

func (mt *Mounted) Restore(ctx context.Context, h Host) (map[string]int, error)

Restore hands every mounted Restorer the host's unspent rows on the topics its module manages (RestoreTopics), with the storage seam the contract allows, and returns the count each indexed. It runs before the engine serves, as the TypeScript host's restore does; a topic no restorer reads (anytx's load topics) is never read. Every module filters the rows it is handed by its own topics and reads anything else through the seam, so handing it only its own changes nothing but the time a start takes. A service that fails is the host failing to start.

type Prechecker added in v0.13.0

type Prechecker interface {
	Precheck(ctx context.Context) context.Context
}

Prechecker is a contract topic manager whose verdict reads nothing the topic holds but the previous coins (bstore's records manager): the host calls Precheck with the submission's context (bcommon module.WithSubmission) before it takes the topic's lock, and hands the context Precheck returns down to IdentifyAdmissibleOutputs under the lock, where the manager finishes the verdict it left there. Precheck must count, log and store nothing: the host may never show the manager the transaction (a duplicate, an earlier refusal). A manager implements it without importing this package; the method is the contract.

Jump to

Keyboard shortcuts

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