Skip to content

Multi-instance Modules

Some modules — particularly gRPC clients and database connections — are often needed in multiple configurations within a single service. Lakta supports this via NamedModule.

A NamedModule registers its DI providers under its name rather than the type alone. This lets you have, say, two gRPC client connections with different targets.

Every built-in module that supports multi-instance ships a WithName option:

lakta.NewRuntime(
grpcclient.NewModule(grpcclient.WithName("payments")),
grpcclient.NewModule(grpcclient.WithName("notifications")),
)

Config for each instance lives under its name:

modules:
grpc:
client:
payments:
target: "payments-svc:50051"
notifications:
target: "notifications-svc:50051"

Use do.MustInvokeNamed with the instance name:

paymentsConn := do.MustInvokeNamed[*grpc.ClientConn](injector, "payments")
notifsConn := do.MustInvokeNamed[*grpc.ClientConn](injector, "notifications")
type MyModule struct {
lakta.NamedBase
}
func NewMyModule(name string) *MyModule {
m := &MyModule{}
m.NamedBase = lakta.NewNamedBase(name)
return m
}

NamedBase satisfies the NamedModule interface and stores the name for use in ConfigPath() and DI registration.