Documentation
¶
Overview ¶
Package app provides a high-level API for building cluster applications that combine actors with sharded message routing.
The App type orchestrates a cluster cluster.Node and cluster.Client with actor-based message handling, providing a simple way to build horizontally scalable services.
Basic Usage ¶
app, err := app.Run(app.Config{
Node: app.NodeConfig{
ID: "node-1",
NumShards: 64,
Transport: natsTransport,
},
},
actor.HandleMsg[CreateUserCmd](handleCreateUser),
actor.HandleRequest[GetUserQuery, *User](handleGetUser),
)
if err != nil {
log.Fatal(err)
}
// Send messages via the client
user, err := cluster.NewRequest[GetUserQuery, User](
app.Client().Key("user:123"),
).Request(ctx, GetUserQuery{ID: "123"})
// Graceful shutdown
app.Shutdown(ctx)
Multi-Node Clusters ¶
For multi-node deployments, configure all nodes with the same NumShards and ShardSeed, and provide the full list of NodeIDs:
app.NodeConfig{
ID: myNodeID,
NumShards: 256,
NodeIDs: []string{"node-1", "node-2", "node-3"},
ShardSeed: "production",
Transport: natsTransport,
}
Shards are distributed across nodes using consistent hashing, so adding or removing nodes only redistributes a minimal number of shards.
Custom Actor Factory ¶
For advanced use cases, provide a custom CreateActor function:
app.Config{
CreateActor: func(key string) (actor.Actor, error) {
// Create actor with custom configuration based on key
return actor.New(opts, handler), nil
},
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ActorOptions ¶
type ActorOptions struct {
// MailboxSize is the actor mailbox capacity (default: 256).
MailboxSize int
// MaxConcurrentTasks caps scheduled tasks via HandlerCtx.Schedule.
// If 0 or negative, scheduling is unlimited.
MaxConcurrentTasks int
// Metrics for actor instrumentation. Optional.
Metrics actor.ActorMetrics
}
ActorOptions configures actor creation.
type App ¶
type App struct {
// contains filtered or unexported fields
}
App combines a cluster Node and Client with actor-based message handling.
func New ¶
func New(config Config, handlers ...actor.HandlerRegistration) (app *App, err error)
New creates a new App without starting it. Call Run() to start.
func Run ¶
func Run(config Config, handlers ...actor.HandlerRegistration) (*App, error)
Run creates and starts an App. Convenience function combining New() and Run().
func (*App) Done ¶
func (a *App) Done() <-chan struct{}
Done returns a channel that is closed when the app has fully stopped.
func (*App) Run ¶
Run starts the cluster node. Blocks until the context is cancelled. Can only be called once.
type Config ¶
type Config struct {
// Context is the parent context for cancellation. Defaults to context.Background().
Context context.Context
// Log is the logger instance. Defaults to slog.Default().
Log *slog.Logger
// Node configures the cluster node.
Node NodeConfig
// Actor configures actor creation options.
Actor ActorOptions
// CreateActor overrides the default actor factory. If set, Actor options are ignored.
CreateActor func(key string) (actor.Actor, error)
}
Config is the application configuration.
type NodeConfig ¶
type NodeConfig struct {
// ID is the node identifier. Auto-generated if empty.
ID string
// NumShards is the total number of shards (default: 256).
NumShards uint32
// NodeIDs lists all node IDs in the cluster. Defaults to [ID] for single-node.
NodeIDs []string
// ShardSeed is used for deterministic shard distribution (default: "default").
ShardSeed string
// Transport is the communication layer. Defaults to in-memory for testing.
Transport cluster.Transport
// Metrics for cluster instrumentation. Optional.
Metrics cluster.ClusterMetrics
}
NodeConfig configures the cluster node.