Documentation
¶
Overview ¶
Package dim is a slim, generics-based dependency-injection toolkit: lazy providers and managed resources built from plain functions, with structured logging of initialization and cleanup.
A Provider[T] is just a function that returns a value, evaluated lazily and (via Once / OnceWithName / NewResource) memoized so the resource is built exactly once on first use. NewResource additionally pairs the provider with a CleanupFunc, giving each resource an automatically registered teardown that the application can run in reverse (LIFO) order at shutdown, so applications can adopt a single, consistent wiring style.
Usage ¶
Declare each dependency as a managed resource; keep the returned cleanup to register with a shutdown coordinator (see the closer package):
store, cleanup := dim.NewResource("Store",
func(ctx context.Context) (Store, dim.CleanupFunc, error) {
st, err := postgres.New(ctx, dsn)
if err != nil {
return nil, nil, err
}
return st, func() error { return st.Close() }, nil
})
closer.Add(cleanup)
db := store(ctx) // built (and logged) on first call, cached thereafter
For a value with no cleanup, wrap a factory in a Provider directly. A factory error is fatal and panics, so initialization failures surface at startup:
cfg := dim.OnceWithName("Config", func(ctx context.Context) (Config, error) {
return loadConfig(ctx)
})
API ¶
- Provider[T]: lazy resource accessor func(ctx) T.
- FactoryFunc[T]: func(ctx) (T, CleanupFunc, error) used by NewResource.
- CleanupFunc: a teardown func() error, run LIFO at shutdown.
- Once / OnceWithName: memoize a factory into a Provider (panic on error).
- NewResource: a memoized Provider plus an auto-registered CleanupFunc.
- NamedCleanup: wrap a teardown with named, timed logging.
Example ¶
NewResource builds the value on the first call and caches it; its cleanup runs the teardown the factory returned. A resource nobody asked for is never built, and its cleanup does nothing.
package main
import (
"context"
"fmt"
"github.com/assanoff/skit/dim"
)
type pool struct{ dsn string }
func main() {
ctx := context.Background()
db, closeDB := dim.NewResource("DB",
func(context.Context) (*pool, dim.CleanupFunc, error) {
fmt.Println("connect")
p := &pool{dsn: "postgres://localhost/app"}
return p, func() error {
fmt.Println("disconnect")
return nil
}, nil
})
_, closeCache := dim.NewResource("Cache",
func(context.Context) (*pool, dim.CleanupFunc, error) {
fmt.Println("connect cache")
return &pool{}, nil, nil
})
first, second := db(ctx), db(ctx)
fmt.Println(first.dsn, first == second)
fmt.Println(closeCache(), closeDB())
}
Output: connect postgres://localhost/app true disconnect <nil> <nil>
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewResource ¶
func NewResource[T any](name string, factory func(context.Context) (T, CleanupFunc, error)) (Provider[T], CleanupFunc)
NewResource creates a new managed resource and returns Provider and CleanupFunc. factory must return: (resource, cleanup_function, error).
Usage:
c.Store, cleanup := dim.NewResource("Store", func(ctx context.Context) (store.Store, dim.CleanupFunc, error) {
st := postgres.New(c.Logger)
if err := st.Connect(config); err != nil {
return nil, nil, err
}
return st, func() error { return st.Close() }, nil
})
return cleanup, nil
Note: Go cannot infer T from the named type FactoryFunc[T], so the signature takes an anonymous function literal.
Types ¶
type CleanupFunc ¶
type CleanupFunc func() error
CleanupFunc - function to close/clean up a resource. Functions are added in initialization order and are called in reverse order (LIFO).
func NamedCleanup ¶
func NamedCleanup(name string, fn func() error) CleanupFunc
NamedCleanup creates a function with closure on name that logs the resource name and execution time.
type FactoryFunc ¶
type FactoryFunc[T any] func(ctx context.Context) (T, CleanupFunc, error)
FactoryFunc is a function that creates a resource and returns it along with an optional cleanup function.
type Provider ¶
Provider is a function that returns a resource value.
func Once ¶
Once wraps factory in a Provider that runs it a single time, caching the result. A factory error is fatal and panics — initialization failures should surface at startup, not be silently swallowed at call sites.
Example ¶
Once memoizes a factory without a teardown; the factory runs on the first call, not when the provider is declared.
package main
import (
"context"
"fmt"
"github.com/assanoff/skit/dim"
)
func main() {
ctx := context.Background()
limit := dim.Once(func(context.Context) (int, error) {
fmt.Println("load limit")
return 100, nil
})
fmt.Println("declared")
fmt.Println(limit(ctx), limit(ctx))
}
Output: declared load limit 100 100