added nats + worker modules

This commit is contained in:
2025-12-19 09:59:00 -05:00
parent b457c74d49
commit ca758cc708
12 changed files with 559 additions and 31 deletions

5
nats/errors.go Normal file
View File

@@ -0,0 +1,5 @@
package nats
import "errors"
var ErrNotReady = errors.New("nats server not ready after ready check timeout")

68
nats/nats.go Normal file
View File

@@ -0,0 +1,68 @@
package nats
import (
"time"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
)
func New(opts ...Option) (*nats.Conn, func(), error) {
cfg := &config{
readyTimeout: 5 * time.Second,
}
for _, opt := range opts {
opt(cfg)
}
var nc *nats.Conn
var shutdown func()
if cfg.externalURL != "" {
var err error
nc, err = nats.Connect(cfg.externalURL, cfg.connectOpts...)
if err != nil {
return nil, nil, err
}
shutdown = func() { nc.Close() }
return nc, shutdown, nil
}
serverOpts := cfg.serverOpts
if serverOpts == nil {
serverOpts = &server.Options{
Host: "127.0.0.1",
Port: -1,
NoLog: true,
}
}
s, err := server.NewServer(serverOpts)
if err != nil {
return nil, nil, err
}
go s.Start()
if !s.ReadyForConnections(cfg.readyTimeout) {
s.Shutdown()
return nil, nil, ErrNotReady
}
nc, err = nats.Connect(
s.ClientURL(),
append(cfg.connectOpts, nats.InProcessServer(s), nats.Name("embedded-client"))...,
)
if err != nil {
s.Shutdown()
return nil, nil, err
}
shutdown = func() {
nc.Close()
s.Shutdown()
}
return nc, shutdown, nil
}

74
nats/options.go Normal file
View File

@@ -0,0 +1,74 @@
package nats
import (
"strings"
"time"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
)
type Option func(*config)
type config struct {
serverOpts *server.Options
connectOpts []nats.Option
externalURL string
readyTimeout time.Duration
shutdownTimeout time.Duration
}
func WithExternalURL(url string) Option {
return func(c *config) { c.externalURL = url }
}
func WithServerOpts(opts *server.Options) Option {
return func(c *config) { c.serverOpts = opts }
}
func WithConnectOpts(opts ...nats.Option) Option {
return func(c *config) { c.connectOpts = append(c.connectOpts, opts...) }
}
func WithReadyTimeout(d time.Duration) Option {
return func(c *config) { c.readyTimeout = d }
}
func WithCluster(name string, routes []string) Option {
return func(c *config) {
if c.serverOpts == nil {
c.serverOpts = &server.Options{}
}
c.serverOpts.Cluster = server.ClusterOpts{
Name: name,
}
c.serverOpts.Routes = server.RoutesFromStr(strings.Join(routes, ","))
}
}
func WithClusterListen() Option {
return func(c *config) {
if c.serverOpts == nil {
c.serverOpts = &server.Options{}
}
c.serverOpts.Cluster.ListenStr = "0.0.0.0:6222"
}
}
func WithJetStream(enabled bool, storeDir ...string) Option {
return func(c *config) {
if c.serverOpts == nil {
c.serverOpts = &server.Options{}
}
c.serverOpts.JetStream = enabled
if len(storeDir) > 0 {
c.serverOpts.StoreDir = storeDir[0]
}
}
}
func WithClientName(name string) Option {
return func(c *config) {
c.connectOpts = append(c.connectOpts, nats.Name(name))
}
}