provider

package
v0.0.2-alpha Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package provider defines the service provider contract and the ordered registry that drives application bootstrap.

A Service registers bindings during Register, then performs any startup work in Boot. Providers that need cleanup implement the optional Shutdowner interface; the Registry only calls Shutdown on providers that declare it. Boot order matches registration order; shutdown order is the reverse.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Application

type Application interface {
	Bind(name string, fn container.Factory)
	Singleton(name string, fn container.Factory)
	Make(name string) (any, error)

	On(event string, fn events.Subscriber) func()
	Emit(event string, data ...any) error

	// Environment reports the runtime environment the app is running
	// in. Providers can consult this to swap driver defaults (memory
	// backends in tests, real services in production) without waiting
	// on config layering.
	Environment() env.Environment
}

Application is the surface a provider sees during Register and Boot. It is intentionally narrow: providers can register bindings, resolve previously registered ones, and publish or subscribe to events. This lets the Registry drive lifecycle without exposing the full app kernel to providers.

type Base

type Base struct{}

Base is an embeddable no-op Service. Embed it in concrete providers to satisfy the Service interface while only implementing the methods that matter. Base does not implement Shutdowner — add a Shutdown method to your provider if it needs cleanup.

func (Base) Boot

Boot is a no-op default. Override in concrete providers.

func (Base) Register

func (Base) Register(Application) error

Register is a no-op default. Override in concrete providers.

type Registry

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

Registry manages an ordered list of service providers and drives their lifecycle. The zero value is not usable; call NewRegistry.

func NewRegistry

func NewRegistry() *Registry

NewRegistry returns an empty Registry.

func (*Registry) Add

func (r *Registry) Add(providers ...Service)

Add appends one or more providers to the registry. Order is preserved and determines the Register/Boot sequence.

func (*Registry) Boot

func (r *Registry) Boot(ctx context.Context, app Application) error

Boot invokes Boot on every provider in insertion order, recording each successfully booted provider so Shutdown can visit them in reverse. On the first Boot error the sequence stops and the error is returned; providers booted before the failure are still recorded and will be shut down.

func (*Registry) Len

func (r *Registry) Len() int

Len returns the number of registered providers.

func (*Registry) Register

func (r *Registry) Register(app Application) error

Register invokes Register on every provider in insertion order. It returns the first error encountered, aborting the sequence.

func (*Registry) Shutdown

func (r *Registry) Shutdown(ctx context.Context, app Application) error

Shutdown invokes Shutdown on booted providers in reverse order. Only providers that implement Shutdowner are visited. Shutdown errors are collected and returned as a joined error; every provider is given a chance to clean up even if earlier ones fail.

type Service

type Service interface {
	Register(app Application) error
	Boot(ctx context.Context, app Application) error
}

Service is a service provider. Register runs first for every provider in registration order (all Register calls complete before any Boot call). Boot runs after all providers are registered, again in registration order. Any error from either phase aborts bootstrap.

type Shutdowner

type Shutdowner interface {
	Shutdown(ctx context.Context, app Application) error
}

Shutdowner is the optional interface implemented by providers that need to release resources on application shutdown. Providers that do not need cleanup should not implement it — the Registry skips them entirely.

Jump to

Keyboard shortcuts

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