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

67
worker/options.go Normal file
View File

@@ -0,0 +1,67 @@
package worker
import (
"context"
"log/slog"
"time"
"github.com/nats-io/nats.go"
)
type Handler func(context.Context, *nats.Msg) error
type Option func(*config)
type config struct {
streamName string
consumerName string
durableName string
handler Handler
concurrency int
ackWait time.Duration
maxDeliver int
filterSubject string
deliverPolicy nats.DeliverPolicy
replayPolicy nats.ReplayPolicy
log *slog.Logger
storage nats.StorageType
}
func WithStream(name string) Option {
return func(c *config) { c.streamName = name }
}
func WithConsumer(consumer, subject string) Option {
return func(c *config) {
c.consumerName = consumer
c.filterSubject = subject
}
}
func WithDurable(name string) Option {
return func(c *config) { c.durableName = name }
}
func WithHandler(h Handler) Option {
return func(c *config) { c.handler = h }
}
func WithConcurrency(n int) Option {
return func(c *config) { c.concurrency = n }
}
func WithAckWait(d time.Duration) Option {
return func(c *config) { c.ackWait = d }
}
func WithMaxDeliver(n int) Option {
return func(c *config) { c.maxDeliver = n }
}
func WithLogger(l *slog.Logger) Option {
return func(c *config) { c.log = l }
}
func WithStorage(storage nats.StorageType) Option {
return func(c *config) { c.storage = storage }
}