feat: initial scaffold of unibus message bus (membership service + client lib + demo peers)
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
// Package embeddednats starts an in-process NATS server with JetStream enabled.
|
||||
//
|
||||
// This lets the whole unibus stack run with `go run` without installing or
|
||||
// managing a separate NATS deployment. In production, point clients at an
|
||||
// external NATS via the --nats-url flag instead of using this.
|
||||
package embeddednats
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
server "github.com/nats-io/nats-server/v2/server"
|
||||
)
|
||||
|
||||
// Start launches an embedded nats-server with JetStream enabled, listening on
|
||||
// the given port and persisting JetStream state under storeDir. It blocks until
|
||||
// the server is ready to accept connections (up to 5s) and returns the running
|
||||
// server. The caller is responsible for calling Shutdown on it.
|
||||
func Start(storeDir string, port int) (*server.Server, error) {
|
||||
opts := &server.Options{
|
||||
JetStream: true,
|
||||
StoreDir: storeDir,
|
||||
Port: port,
|
||||
DontListen: false,
|
||||
// Keep the embedded server quiet by default; the host app logs the URLs.
|
||||
NoLog: true,
|
||||
NoSigs: true,
|
||||
}
|
||||
|
||||
ns, err := server.NewServer(opts)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("embeddednats: new server: %w", err)
|
||||
}
|
||||
|
||||
go ns.Start()
|
||||
|
||||
if !ns.ReadyForConnections(5 * time.Second) {
|
||||
ns.Shutdown()
|
||||
return nil, fmt.Errorf("embeddednats: server not ready for connections within 5s")
|
||||
}
|
||||
|
||||
return ns, nil
|
||||
}
|
||||
|
||||
// ClientURL returns a NATS connection URL for the running embedded server.
|
||||
func ClientURL(ns *server.Server) string {
|
||||
return ns.ClientURL()
|
||||
}
|
||||
Reference in New Issue
Block a user