Documentation
¶
Index ¶
- Constants
- Variables
- func FromStream[T any](stream goflow.Stream[Message[T]], pub Publisher[T], subject string) error
- func IsNonRetryable(err error) bool
- func MessageID(ctx context.Context) string
- func NonRetryable(err error) error
- func ToChan[T any](ctx context.Context, sub Subscriber[T], subject string, bufSize int) <-chan Message[T]
- func ToStream[T any](ctx context.Context, sub Subscriber[T], subject string, bufSize int) goflow.Stream[Message[T]]
- func WithHeader(ctx context.Context, h Header) context.Context
- func WithMessageID(ctx context.Context, id string) context.Context
- type Acker
- type BackoffFunc
- type BoundPublisher
- type BoundSubscriber
- type DeadLetterFunc
- type DelayedNaker
- type Filter
- type Handler
- type Header
- type MapFunc
- type Message
- type Middleware
- type PipeOption
- type ProcessOption
- type Publisher
- type PublisherMiddleware
- type RequestHandler
- type Requester
- type Responder
- type Subscriber
- type Telemetry
- func (t *Telemetry) ExtractContext(ctx context.Context, carrier propagation.TextMapCarrier) context.Context
- func (t *Telemetry) ExtractSpanContext(ctx context.Context, carrier propagation.TextMapCarrier) trace.SpanContext
- func (t *Telemetry) InjectContext(ctx context.Context, carrier propagation.TextMapCarrier)
- func (t *Telemetry) RecordAckOutcome(ctx context.Context, action, subject string, err error)
- func (t *Telemetry) RecordFetch(ctx context.Context, subject string, system semconvmsg.SystemAttr, count int, ...) error
- func (t *Telemetry) RecordProcess(ctx context.Context, subject string, system semconvmsg.SystemAttr, ...) error
- func (t *Telemetry) RecordPublish(ctx context.Context, subject string, system semconvmsg.SystemAttr, ...) error
- func (t *Telemetry) RecordRequest(ctx context.Context, subject string, system semconvmsg.SystemAttr, ...) error
- func (t *Telemetry) RegisterLag(subject string, lagFn func() int64) (metric.Int64ObservableGauge, error)
- type TelemetryOption
- type Terminator
- type Topic
Examples ¶
Constants ¶
const MessageIDHeader = "X-Message-ID"
MessageIDHeader is the HTTP header name used to propagate a message ID across the HTTP transport.
Variables ¶
var ( // ErrPublish indicates a failure in the publish path. ErrPublish = errors.New("publish") // ErrSubscribe indicates a failure in the subscribe path. ErrSubscribe = errors.New("subscribe") // ErrEncode indicates a serialization failure. ErrEncode = errors.New("encode") // ErrDecode indicates a deserialization failure. ErrDecode = errors.New("decode") // ErrTransport indicates a transport-level failure (network, protocol). ErrTransport = errors.New("transport") )
Sentinel errors for classifying failures. Transports join these with the causal error via errors.Join, so callers can inspect with errors.Is:
if errors.Is(err, goflux.ErrEncode) { /* codec problem */ }
if errors.Is(err, goflux.ErrPublish) { /* publish-path failure */ }
var ErrNonRetryable = errors.New("non-retryable")
ErrNonRetryable is a sentinel error that marks a handler failure as permanent. When a [RetryPolicy] encounters this error (via errors.Is), it returns [RetryTerm] so that the message is terminated rather than redelivered.
Functions ¶
func FromStream ¶ added in v0.2.0
FromStream consumes a goflow.Stream of messages and publishes each one via the provided Publisher. When subject is non-empty every message is published to that subject; when empty the original message subject is preserved. It blocks until the stream is exhausted or the context is cancelled.
func IsNonRetryable ¶ added in v0.2.0
IsNonRetryable reports whether err (or any error in its chain) is marked as non-retryable.
func NonRetryable ¶ added in v0.2.0
NonRetryable wraps err so that errors.Is(err, ErrNonRetryable) returns true. Use this to signal that a handler error is permanent and the message should not be retried.
Example ¶
ExampleNonRetryable demonstrates wrapping and detecting non-retryable errors.
package main
import (
"errors"
"fmt"
"github.com/foomo/goflux"
)
func main() {
orig := errors.New("invalid payload")
wrapped := goflux.NonRetryable(orig)
fmt.Println("is non-retryable:", goflux.IsNonRetryable(wrapped))
fmt.Println("is original:", errors.Is(wrapped, orig))
fmt.Println("plain error:", goflux.IsNonRetryable(errors.New("transient")))
}
Output: is non-retryable: true is original: true plain error: false
func ToChan ¶
func ToChan[T any](ctx context.Context, sub Subscriber[T], subject string, bufSize int) <-chan Message[T]
ToChan bridges a Subscriber into a plain channel. It launches Subscribe in a goroutine and forwards each message (including acker) into a buffered channel. The returned channel closes when ctx is cancelled.
bufSize controls backpressure: a full buffer blocks the subscriber's handler until the consumer catches up.
Example ¶
ExampleToChan demonstrates bridging a Subscriber into a plain channel. Messages can be consumed with a range loop instead of a callback handler.
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
bus := channel.NewBus[Event]()
pub := channel.NewPublisher(bus)
sub, err := channel.NewSubscriber(bus, 1)
if err != nil {
panic(err)
}
ch := goflux.ToChan[Event](ctx, sub, "events", 4)
// Allow subscriber to register.
time.Sleep(10 * time.Millisecond)
_ = pub.Publish(ctx, "events", Event{ID: "1", Name: "alpha"})
_ = pub.Publish(ctx, "events", Event{ID: "2", Name: "bravo"})
// Read two messages from the channel.
fmt.Println((<-ch).Payload.Name)
fmt.Println((<-ch).Payload.Name)
Output: alpha bravo
func ToStream ¶ added in v0.2.0
func ToStream[T any](ctx context.Context, sub Subscriber[T], subject string, bufSize int) goflow.Stream[Message[T]]
ToStream bridges a Subscriber into a goflow.Stream. It launches Subscribe in a goroutine via ToChan and wraps the resulting channel as a Stream.
bufSize controls backpressure: a full buffer blocks the subscriber's handler until the consumer catches up.
func WithHeader ¶
WithHeader returns a copy of ctx with the given header attached. Transports read this header during Publish and merge it into the outgoing transport headers.
func WithMessageID ¶
WithMessageID returns a copy of ctx with the given message ID attached. The ID is purely opt-in: if set, transports propagate it via headers and RecordPublish / RecordProcess attach it as the goflux.message.id span attribute.
Types ¶
type Acker ¶
Acker is the minimal acknowledgment interface. Transports that support at-least-once delivery implement this on their message wrapper.
type BackoffFunc ¶ added in v0.2.0
BackoffFunc returns the delay before the next retry attempt. attempt starts at 0 for the first retry (i.e. the second overall call).
type BoundPublisher ¶
type BoundPublisher[T any] struct { // contains filtered or unexported fields }
BoundPublisher wraps a Publisher with a fixed subject.
func Bind ¶
func Bind[T any](pub Publisher[T], subject string) *BoundPublisher[T]
Bind returns a BoundPublisher that always publishes to the given subject.
Example ¶
ExampleBind demonstrates creating a BoundPublisher that fixes the subject. Callers only need to provide the payload — the subject is always "orders".
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
bus := channel.NewBus[Event]()
pub := channel.NewPublisher(bus)
sub, err := channel.NewSubscriber(bus, 1)
if err != nil {
panic(err)
}
// Bind fixes the subject to "orders".
bound := goflux.Bind[Event](pub, "orders")
gofuncy.StartWithReady(ctx, func(ctx context.Context, ready gofuncy.ReadyFunc) error {
ready()
return sub.Subscribe(ctx, "orders", func(_ context.Context, msg goflux.Message[Event]) error {
fmt.Println(msg.Subject, msg.Payload.Name)
cancel()
return nil
})
}, gofuncy.WithName("subscriber"))
// Allow subscriber to register.
time.Sleep(10 * time.Millisecond)
// The subject argument is ignored — "orders" is always used.
if err := bound.Publish(ctx, "ignored", Event{ID: "1", Name: "widget"}); err != nil {
panic(err)
}
<-ctx.Done()
Output: orders widget
func (*BoundPublisher[T]) Close ¶
func (b *BoundPublisher[T]) Close() error
Close delegates to the underlying Publisher.
type BoundSubscriber ¶ added in v0.3.0
type BoundSubscriber[T any] struct { // contains filtered or unexported fields }
BoundSubscriber wraps a Subscriber with a fixed subject.
func BindSubscriber ¶ added in v0.3.0
func BindSubscriber[T any](sub Subscriber[T], subject string) *BoundSubscriber[T]
BindSubscriber returns a BoundSubscriber that always subscribes to the given subject.
Example ¶
ExampleBindSubscriber demonstrates creating a BoundSubscriber that fixes the subject. Callers only need to provide the handler — the subject is always "orders".
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
bus := channel.NewBus[Event]()
pub := channel.NewPublisher(bus)
sub, err := channel.NewSubscriber(bus, 1)
if err != nil {
panic(err)
}
// BindSubscriber fixes the subject to "orders".
bound := goflux.BindSubscriber[Event](sub, "orders")
gofuncy.StartWithReady(ctx, func(ctx context.Context, ready gofuncy.ReadyFunc) error {
ready()
// The subject argument is ignored — "orders" is always used.
return bound.Subscribe(ctx, "", func(_ context.Context, msg goflux.Message[Event]) error {
fmt.Println(msg.Subject, msg.Payload.Name)
cancel()
return nil
})
}, gofuncy.WithName("subscriber"))
// Allow subscriber to register.
time.Sleep(10 * time.Millisecond)
if err := pub.Publish(ctx, "orders", Event{ID: "1", Name: "widget"}); err != nil {
panic(err)
}
<-ctx.Done()
Output: orders widget
func (*BoundSubscriber[T]) Close ¶ added in v0.3.0
func (b *BoundSubscriber[T]) Close() error
Close delegates to the underlying Subscriber.
func (*BoundSubscriber[T]) Subscribe ¶ added in v0.3.0
Subscribe registers handler for the bound subject. The subject parameter is ignored — the subject provided to BindSubscriber is always used.
type DeadLetterFunc ¶
DeadLetterFunc receives messages that could not be mapped or published after all retries are exhausted. Use it to log, alert, or forward to a DLQ.
type DelayedNaker ¶
DelayedNaker extends Acker with delayed negative acknowledgment, causing the message to be redelivered after the given delay.
type Filter ¶
Filter decides whether a message should be forwarded. Returning false or a non-nil error drops the message and logs the reason.
type Handler ¶
Handler is the callback signature used by Subscriber.Subscribe. Returning a non-nil error signals the subscriber to nack / requeue the message (behavior is implementation-specific).
func Pipe ¶
func Pipe[T any](pub Publisher[T], opts ...PipeOption[T]) Handler[T]
Pipe returns a Handler[T] that forwards every accepted message to pub. Filters run first; a dropped message never reaches pub. A publish error is returned to the caller as-is. Acknowledgment is not performed — use [AutoAck] or [Processor] to manage ack/nak.
Example ¶
ExamplePipe demonstrates wiring a source subscriber to a destination publisher using goflux.Pipe. Every message received on "events" is forwarded to the destination bus on the same subject.
package main
import (
"context"
"fmt"
"github.com/foomo/goflux"
"github.com/foomo/goflux/transport/channel"
"github.com/foomo/gofuncy"
)
type Event struct {
ID string `json:"id"`
Name string `json:"name"`
}
func main() {
// Source transport.
srcBus := channel.NewBus[Event]()
srcPub := channel.NewPublisher(srcBus)
srcSub, err := channel.NewSubscriber(srcBus, 1)
if err != nil {
panic(err)
}
// Destination transport.
dstBus := channel.NewBus[Event]()
dstPub := channel.NewPublisher(dstBus)
dstSub, err := channel.NewSubscriber(dstBus, 1)
if err != nil {
panic(err)
}
ctx, cancel := context.WithCancel(context.Background())
// Listen on destination — print when a message arrives.
// Pipe preserves the original subject, so we subscribe to "events".
gofuncy.StartWithReady(ctx, func(ctx context.Context, ready gofuncy.ReadyFunc) error {
ready()
return dstSub.Subscribe(ctx, "events", func(_ context.Context, msg goflux.Message[Event]) error {
fmt.Println(msg.Subject, msg.Payload)
cancel()
return nil
})
}, gofuncy.WithName("dst-subscriber"))
// Pipe: source subscriber forwards to destination publisher.
gofuncy.StartWithReady(ctx, func(ctx context.Context, ready gofuncy.ReadyFunc) error {
ready()
return srcSub.Subscribe(ctx, "events", goflux.Pipe[Event](dstPub))
}, gofuncy.WithName("pipe"))
if err := srcPub.Publish(ctx, "events", Event{ID: "1", Name: "hello"}); err != nil {
panic(err)
}
<-ctx.Done()
}
Output: events {1 hello}
Example (WithDeadLetter) ¶
ExamplePipe_withDeadLetter demonstrates the dead-letter handler on a Pipe. When the downstream publisher returns an error, the dead-letter function receives the original message and the error.
ctx := context.Background()
// Source transport.
srcBus := channel.NewBus[Event]()
srcPub := channel.NewPublisher(srcBus)
srcSub, err := channel.NewSubscriber(srcBus, 1)
if err != nil {
panic(err)
}
// A publisher that always fails.
pubErr := errors.New("publish failed")
badPub := &failPublisher[Event]{err: pubErr}
received := make(chan struct{})
// Dead-letter handler prints the original message and error.
deadLetter := func(_ context.Context, msg goflux.Message[Event], err error) {
fmt.Println("dead-letter:", msg.Subject, msg.Payload, err)
close(received)
}
// Pipe with dead-letter: publish errors are routed to the handler.
gofuncy.StartWithReady(ctx, func(ctx context.Context, ready gofuncy.ReadyFunc) error {
ready()
return srcSub.Subscribe(ctx, "events", goflux.Pipe[Event](badPub, goflux.WithDeadLetter[Event](deadLetter)))
}, gofuncy.WithName("pipe"))
if err := srcPub.Publish(ctx, "events", Event{ID: "1", Name: "hello"}); err != nil {
panic(err)
}
<-received
Output: dead-letter: events {1 hello} publish failed
Example (WithFilter) ¶
ExamplePipe_withFilter demonstrates filtering messages before they are forwarded. Only events whose ID is not "skip" reach the destination.
package main
import (
"context"
"fmt"
"time"
"github.com/foomo/goflux"
"github.com/foomo/goflux/transport/channel"
"github.com/foomo/gofuncy"
)
type Event struct {
ID string `json:"id"`
Name string `json:"name"`
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Source transport.
srcBus := channel.NewBus[Event]()
srcPub := channel.NewPublisher(srcBus)
srcSub, err := channel.NewSubscriber(srcBus, 1)
if err != nil {
panic(err)
}
// Destination transport.
dstBus := channel.NewBus[Event]()
dstPub := channel.NewPublisher(dstBus)
dstSub, err := channel.NewSubscriber(dstBus, 1)
if err != nil {
panic(err)
}
// Filter: drop events with ID "skip".
filter := func(_ context.Context, msg goflux.Message[Event]) (bool, error) {
return msg.Payload.ID != "skip", nil
}
// Listen on destination — expect exactly one message.
gofuncy.StartWithReady(ctx, func(ctx context.Context, ready gofuncy.ReadyFunc) error {
ready()
return dstSub.Subscribe(ctx, "events", func(_ context.Context, msg goflux.Message[Event]) error {
fmt.Println(msg.Subject, msg.Payload)
cancel()
return nil
})
}, gofuncy.WithName("dst-subscriber"))
// Pipe with filter.
gofuncy.StartWithReady(ctx, func(ctx context.Context, ready gofuncy.ReadyFunc) error {
ready()
return srcSub.Subscribe(ctx, "events", goflux.Pipe[Event](dstPub, goflux.WithFilter[Event](filter)))
}, gofuncy.WithName("pipe"))
// Allow goroutines to register on their buses before publishing.
time.Sleep(10 * time.Millisecond)
// First message is filtered out.
if err := srcPub.Publish(ctx, "events", Event{ID: "skip", Name: "ignored"}); err != nil {
panic(err)
}
// Second message passes the filter.
if err := srcPub.Publish(ctx, "events", Event{ID: "keep", Name: "accepted"}); err != nil {
panic(err)
}
<-ctx.Done()
}
Output: events {keep accepted}
func PipeMap ¶
func PipeMap[T, U any](pub Publisher[U], mapFn MapFunc[T, U], opts ...PipeOption[T]) Handler[T]
PipeMap returns a Handler[T] that maps each message from T to U before publishing. Filters run on T before the map. A map error routes the original Message[T] to the dead-letter handler (if set) and drops the message. A publish error after a successful map is also dead-lettered with the original T message.
Example ¶
ExamplePipeMap demonstrates transforming messages from one type to another. Events are mapped to Summary values and published to the destination bus.
package main
import (
"context"
"fmt"
"github.com/foomo/goflux"
"github.com/foomo/goflux/transport/channel"
"github.com/foomo/gofuncy"
)
type Event struct {
ID string `json:"id"`
Name string `json:"name"`
}
type Summary struct {
Label string `json:"label"`
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
// Source transport (Event).
srcBus := channel.NewBus[Event]()
srcPub := channel.NewPublisher(srcBus)
srcSub, err := channel.NewSubscriber(srcBus, 1)
if err != nil {
panic(err)
}
// Destination transport (Summary).
dstBus := channel.NewBus[Summary]()
dstPub := channel.NewPublisher(dstBus)
dstSub, err := channel.NewSubscriber(dstBus, 1)
if err != nil {
panic(err)
}
// Map function: Event → Summary, keeping the same subject.
mapFn := func(_ context.Context, msg goflux.Message[Event]) (goflux.Message[Summary], error) {
return goflux.NewMessage(msg.Subject, Summary{Label: msg.Payload.Name}), nil
}
// Listen on destination — PipeMap publishes with the subject from the MapFunc result.
gofuncy.StartWithReady(ctx, func(ctx context.Context, ready gofuncy.ReadyFunc) error {
ready()
return dstSub.Subscribe(ctx, "events", func(_ context.Context, msg goflux.Message[Summary]) error {
fmt.Println(msg.Subject, msg.Payload)
cancel()
return nil
})
}, gofuncy.WithName("dst-subscriber"))
// PipeMap: source subscriber maps Event→Summary and forwards.
gofuncy.StartWithReady(ctx, func(ctx context.Context, ready gofuncy.ReadyFunc) error {
ready()
return srcSub.Subscribe(ctx, "events", goflux.PipeMap[Event, Summary](dstPub, mapFn))
}, gofuncy.WithName("pipe-map"))
if err := srcPub.Publish(ctx, "events", Event{ID: "1", Name: "hello"}); err != nil {
panic(err)
}
<-ctx.Done()
}
Output: events {hello}
Example (WithDeadLetter) ¶
ExamplePipeMap_withDeadLetter demonstrates the dead-letter handler on a PipeMap. When the MapFunc returns an error, the dead-letter function receives the original source message and the map error.
package main
import (
"context"
"errors"
"fmt"
"github.com/foomo/goflux"
"github.com/foomo/goflux/transport/channel"
"github.com/foomo/gofuncy"
)
type Event struct {
ID string `json:"id"`
Name string `json:"name"`
}
type Summary struct {
Label string `json:"label"`
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
// Source transport (Event).
srcBus := channel.NewBus[Event]()
srcPub := channel.NewPublisher(srcBus)
srcSub, err := channel.NewSubscriber(srcBus, 1)
if err != nil {
panic(err)
}
// Destination transport (Summary).
dstBus := channel.NewBus[Summary]()
dstPub := channel.NewPublisher(dstBus)
dstSub, err := channel.NewSubscriber(dstBus, 1)
if err != nil {
panic(err)
}
// Map function that fails for events with ID "bad".
mapErr := errors.New("bad event")
mapFn := func(_ context.Context, msg goflux.Message[Event]) (goflux.Message[Summary], error) {
if msg.Payload.ID == "bad" {
return goflux.Message[Summary]{}, mapErr
}
return goflux.NewMessage(msg.Subject, Summary{Label: msg.Payload.Name}), nil
}
// Dead-letter handler prints the original message and error.
deadLetter := func(_ context.Context, msg goflux.Message[Event], err error) {
fmt.Println("dead-letter:", msg.Subject, msg.Payload, err)
}
// Listen on destination for the valid message.
gofuncy.StartWithReady(ctx, func(ctx context.Context, ready gofuncy.ReadyFunc) error {
ready()
return dstSub.Subscribe(ctx, "events", func(_ context.Context, msg goflux.Message[Summary]) error {
fmt.Println(msg.Subject, msg.Payload)
cancel()
return nil
})
}, gofuncy.WithName("dst-subscriber"))
// PipeMap with dead-letter.
gofuncy.StartWithReady(ctx, func(ctx context.Context, ready gofuncy.ReadyFunc) error {
ready()
return srcSub.Subscribe(ctx, "events", goflux.PipeMap[Event, Summary](dstPub, mapFn, goflux.WithDeadLetter[Event](deadLetter)))
}, gofuncy.WithName("pipe-map"))
// First message fails the map — routed to dead-letter.
if err := srcPub.Publish(ctx, "events", Event{ID: "bad", Name: "broken"}); err != nil {
panic(err)
}
// Second message succeeds — arrives at destination.
if err := srcPub.Publish(ctx, "events", Event{ID: "1", Name: "hello"}); err != nil {
panic(err)
}
<-ctx.Done()
}
Output: dead-letter: events {bad broken} bad event events {hello}
Example (WithFilter) ¶
ExamplePipeMap_withFilter demonstrates filtering messages before they are mapped and forwarded. The filter runs on the source type (Event); only passing events are transformed into Summary values.
package main
import (
"context"
"fmt"
"time"
"github.com/foomo/goflux"
"github.com/foomo/goflux/transport/channel"
"github.com/foomo/gofuncy"
)
type Event struct {
ID string `json:"id"`
Name string `json:"name"`
}
type Summary struct {
Label string `json:"label"`
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Source transport (Event).
srcBus := channel.NewBus[Event]()
srcPub := channel.NewPublisher(srcBus)
srcSub, err := channel.NewSubscriber(srcBus, 1)
if err != nil {
panic(err)
}
// Destination transport (Summary).
dstBus := channel.NewBus[Summary]()
dstPub := channel.NewPublisher(dstBus)
dstSub, err := channel.NewSubscriber(dstBus, 1)
if err != nil {
panic(err)
}
// Map function: Event → Summary.
mapFn := func(_ context.Context, msg goflux.Message[Event]) (goflux.Message[Summary], error) {
return goflux.NewMessage(msg.Subject, Summary{Label: msg.Payload.Name}), nil
}
// Filter: drop events with ID "skip".
filter := func(_ context.Context, msg goflux.Message[Event]) (bool, error) {
return msg.Payload.ID != "skip", nil
}
// Listen on destination — expect exactly one message.
gofuncy.StartWithReady(ctx, func(ctx context.Context, ready gofuncy.ReadyFunc) error {
ready()
return dstSub.Subscribe(ctx, "events", func(_ context.Context, msg goflux.Message[Summary]) error {
fmt.Println(msg.Subject, msg.Payload)
cancel()
return nil
})
}, gofuncy.WithName("dst-subscriber"))
// PipeMap with filter.
gofuncy.StartWithReady(ctx, func(ctx context.Context, ready gofuncy.ReadyFunc) error {
ready()
return srcSub.Subscribe(ctx, "events", goflux.PipeMap[Event, Summary](dstPub, mapFn, goflux.WithFilter[Event](filter)))
}, gofuncy.WithName("pipe-map"))
// Allow goroutines to register on their buses before publishing.
time.Sleep(10 * time.Millisecond)
// First message is filtered out before the map runs.
if err := srcPub.Publish(ctx, "events", Event{ID: "skip", Name: "ignored"}); err != nil {
panic(err)
}
// Second message passes the filter, gets mapped, and arrives.
if err := srcPub.Publish(ctx, "events", Event{ID: "keep", Name: "accepted"}); err != nil {
panic(err)
}
<-ctx.Done()
}
Output: events {accepted}
type Header ¶
Header carries message metadata (trace context, message ID, custom KV). Keys are case-sensitive, values are slices — matching http.Header semantics.
func HeaderFromContext ¶
HeaderFromContext returns the header stored in ctx, or nil if none is set.
type MapFunc ¶
MapFunc transforms a Message[T] into a Message[U]. A non-nil error drops the message and routes it to the DeadLetterFunc if set.
type Message ¶
type Message[T any] struct { Subject string `json:"subject"` Payload T `json:"payload"` Header Header `json:"header,omitempty"` // contains filtered or unexported fields }
Message is the unit passed to every Handler. Subject carries the routing key (e.g. a NATS subject or HTTP path); Payload holds the decoded value; Header carries optional metadata; acker provides acknowledgment controls.
func NewMessage ¶
NewMessage creates a new Message.
func NewMessageWithHeader ¶
NewMessageWithHeader creates a new Message with the given header.
func (Message[T]) Ack ¶
Ack acknowledges successful processing. No-op if the transport does not support acknowledgments.
func (Message[T]) Nak ¶
Nak signals processing failure; the message should be redelivered. No-op if the transport does not support acknowledgments.
func (Message[T]) NakWithDelay ¶
NakWithDelay signals processing failure with a redelivery delay hint. Falls back to Nak if the transport does not support delayed redelivery.
type Middleware ¶
Middleware wraps a Handler[T] to add cross-cutting behaviour such as logging, rate-limiting, or circuit-breaking.
func Chain ¶
func Chain[T any](mws ...Middleware[T]) Middleware[T]
Chain composes middlewares left-to-right: the first middleware in the list is the outermost wrapper. Chain(a, b)(h) is equivalent to a(b(h)).
Example ¶
ExampleChain demonstrates composing handler middlewares. Middlewares execute left-to-right (outermost first).
var trace []string
mwA := func(next goflux.Handler[Event]) goflux.Handler[Event] {
return func(ctx context.Context, msg goflux.Message[Event]) error {
trace = append(trace, "A-before")
err := next(ctx, msg)
trace = append(trace, "A-after")
return err
}
}
mwB := func(next goflux.Handler[Event]) goflux.Handler[Event] {
return func(ctx context.Context, msg goflux.Message[Event]) error {
trace = append(trace, "B-before")
err := next(ctx, msg)
trace = append(trace, "B-after")
return err
}
}
base := func(_ context.Context, msg goflux.Message[Event]) error {
trace = append(trace, "handler:"+msg.Payload.Name)
return nil
}
handler := goflux.Chain[Event](mwA, mwB)(base)
msg := goflux.NewMessage("events", Event{ID: "1", Name: "hello"})
_ = handler(context.Background(), msg)
for _, s := range trace {
fmt.Println(s)
}
Output: A-before B-before handler:hello B-after A-after
type PipeOption ¶
type PipeOption[T any] func(*pipeConfig[T])
PipeOption configures a Pipe or PipeMap call.
func WithDeadLetter ¶
func WithDeadLetter[T any](fn DeadLetterFunc[T]) PipeOption[T]
WithDeadLetter registers a dead-letter handler called when MapFunc returns an error or when the publisher fails after all retries are exhausted. The original Message[T] and the terminal error are passed to the handler.
func WithFilter ¶
func WithFilter[T any](f Filter[T]) PipeOption[T]
WithFilter registers a filter that runs before publish (or before map in PipeMap). Messages for which the filter returns false are silently dropped and logged. Filter errors are treated as false.
type ProcessOption ¶
type ProcessOption func(*processConfig)
ProcessOption configures Telemetry.RecordProcess.
func WithRemoteSpanContext ¶
func WithRemoteSpanContext(sc trace.SpanContext) ProcessOption
WithRemoteSpanContext attaches the given span context as a span link instead of using it as the parent. Use this for async transports (e.g. NATS) where the producer and consumer are temporally decoupled — the consumer span becomes a root span linked to the producer, rather than a child of it.
type Publisher ¶
type Publisher[T any] interface { // Publish serializes v via the bound Codec and delivers it to the subject. Publish(ctx context.Context, subject string, v T) error // Close releases any underlying connections. Close() error }
Publisher sends encoded messages to a subject/topic.
func RetryPublisher ¶ added in v0.2.0
func RetryPublisher[T any](pub Publisher[T], maxAttempts int, backoff BackoffFunc) Publisher[T]
RetryPublisher wraps a Publisher with retry logic. On publish failure, it retries up to maxAttempts times with delays determined by backoff. Context cancellation aborts the retry loop immediately.
If all attempts fail, the last error is returned.
Example ¶
ExampleRetryPublisher demonstrates wrapping a publisher with retry logic. Transient errors are retried with the given backoff until success.
package main
import (
"context"
"errors"
"fmt"
"time"
"github.com/foomo/goflux"
)
// ExampleRetryPublisher demonstrates wrapping a publisher with retry logic.
// Transient errors are retried with the given backoff until success.
func main() {
// A publisher that fails twice then succeeds.
inner := &countingPublisher{failUntil: 2}
pub := goflux.RetryPublisher[string](inner, 3, func(_ int) time.Duration {
return time.Millisecond // fast backoff for example
})
err := pub.Publish(context.Background(), "events", "hello")
fmt.Println("error:", err)
fmt.Println("attempts:", inner.attempts)
}
// countingPublisher fails the first failUntil attempts, then succeeds.
type countingPublisher struct {
failUntil int
attempts int
}
func (p *countingPublisher) Publish(_ context.Context, _ string, _ string) error {
p.attempts++
if p.attempts <= p.failUntil {
return errors.New("transient error")
}
return nil
}
func (p *countingPublisher) Close() error { return nil }
Output: error: <nil> attempts: 3
type PublisherMiddleware ¶ added in v0.2.0
PublisherMiddleware wraps a Publisher[T] to add cross-cutting behaviour such as retry, rate-limiting, or circuit-breaking on the publish path.
type RequestHandler ¶
RequestHandler processes a request and returns a response.
type Requester ¶
type Requester[Req, Resp any] interface { // Request sends req to the given subject and returns the response. Request(ctx context.Context, subject string, req Req) (Resp, error) // Close releases any underlying resources. Close() error }
Requester sends a typed request and waits for a typed response.
type Responder ¶
type Responder[Req, Resp any] interface { // Serve registers the handler for the given subject. The call blocks // until ctx is cancelled or a fatal error occurs. Serve(ctx context.Context, subject string, handler RequestHandler[Req, Resp]) error // Close releases any underlying resources. Close() error }
Responder handles incoming requests and produces typed responses.
type Subscriber ¶
type Subscriber[T any] interface { // Subscribe registers handler for the subject. The call blocks until ctx is // canceled or the implementation encounters a fatal error. Subscribe(ctx context.Context, subject string, handler Handler[T]) error // Close unsubscribes and releases resources. Close() error }
Subscriber listens on one or more subjects and dispatches decoded messages to a Handler.
type Telemetry ¶
type Telemetry struct {
// contains filtered or unexported fields
}
Telemetry holds OTel instruments (tracer, metrics, propagator) for a single transport instance. Construct with NewTelemetry.
func DefaultTelemetry ¶ added in v0.2.0
DefaultTelemetry returns tel if non-nil, otherwise creates a new Telemetry from OTel globals. If that fails, it falls back to a noop implementation. This is the standard fallback logic used by all transports.
func NewNoopTelemetry ¶ added in v0.2.0
func NewNoopTelemetry() *Telemetry
NewNoopTelemetry returns a Telemetry backed by OTel's noop implementations. All Record* calls are safe but produce no spans or metrics.
func NewTelemetry ¶
func NewTelemetry(opts ...TelemetryOption) (*Telemetry, error)
NewTelemetry creates a Telemetry instance. Without options it reads from the current OTel globals, so callers that have already called otel.SetTracerProvider / otel.SetMeterProvider need not pass anything.
func (*Telemetry) ExtractContext ¶
func (t *Telemetry) ExtractContext(ctx context.Context, carrier propagation.TextMapCarrier) context.Context
ExtractContext extracts span context from carrier and returns an enriched context with the remote span as parent. Use this for synchronous transports (e.g. HTTP) where parent-child relationship is appropriate.
func (*Telemetry) ExtractSpanContext ¶
func (t *Telemetry) ExtractSpanContext(ctx context.Context, carrier propagation.TextMapCarrier) trace.SpanContext
ExtractSpanContext extracts the remote span context from carrier without injecting it as parent into ctx. Use this with WithRemoteSpanContext for async transports where the consumer span should link to (not be a child of) the producer span.
func (*Telemetry) InjectContext ¶
func (t *Telemetry) InjectContext(ctx context.Context, carrier propagation.TextMapCarrier)
InjectContext injects the span context from ctx into the carrier. Transports call this on the publish side to propagate trace context across wire boundaries (e.g. NATS headers, HTTP headers).
func (*Telemetry) RecordAckOutcome ¶ added in v0.2.0
RecordAckOutcome records an acknowledgment outcome (ack, nak, nak_with_delay, term) with an optional error label when the ack operation itself fails.
func (*Telemetry) RecordFetch ¶
func (t *Telemetry) RecordFetch(ctx context.Context, subject string, system semconvmsg.SystemAttr, count int, fn func(context.Context) error) error
RecordFetch opens a consumer span for a pull-based fetch operation.
func (*Telemetry) RecordProcess ¶
func (t *Telemetry) RecordProcess(ctx context.Context, subject string, system semconvmsg.SystemAttr, fn func(context.Context) error, opts ...ProcessOption) error
RecordProcess opens a consumer span, calls fn, records duration and counter. Pass WithRemoteSpanContext to attach the producer span as a link rather than a parent (recommended for async transports like NATS).
func (*Telemetry) RecordPublish ¶
func (t *Telemetry) RecordPublish(ctx context.Context, subject string, system semconvmsg.SystemAttr, fn func(context.Context) error) error
RecordPublish opens a producer span, calls fn, records duration and counter.
func (*Telemetry) RecordRequest ¶
func (t *Telemetry) RecordRequest(ctx context.Context, subject string, system semconvmsg.SystemAttr, fn func(context.Context) error) error
RecordRequest opens a client span for a request-reply call.
func (*Telemetry) RegisterLag ¶
func (t *Telemetry) RegisterLag(subject string, lagFn func() int64) (metric.Int64ObservableGauge, error)
RegisterLag registers the goflux.consumer.lag observable gauge. Uses the meter provider that was passed to NewTelemetry.
type TelemetryOption ¶
type TelemetryOption func(*telemetryConfig)
TelemetryOption configures a Telemetry instance.
func WithMeterProvider ¶
func WithMeterProvider(mp metric.MeterProvider) TelemetryOption
WithMeterProvider sets the meter provider. Defaults to otel.GetMeterProvider.
func WithPropagator ¶
func WithPropagator(p propagation.TextMapPropagator) TelemetryOption
WithPropagator sets the text-map propagator. Defaults to otel.GetTextMapPropagator.
func WithTracerProvider ¶
func WithTracerProvider(tp trace.TracerProvider) TelemetryOption
WithTracerProvider sets the tracer provider. Defaults to otel.GetTracerProvider.
type Terminator ¶
Terminator extends Acker with terminal rejection — the message will not be redelivered. Use for dead-letter patterns where handler errors are non-retryable.
type Topic ¶
type Topic[T any] struct { Publisher[T] Subscriber[T] }
Topic bundles a Publisher and Subscriber sharing the same Codec. Use it when a service needs to both produce and consume the same message type.
Example ¶
ExampleTopic demonstrates bundling a Publisher and Subscriber into a single Topic value. This is useful when a service needs to both produce and consume the same message type.
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
bus := channel.NewBus[Event]()
sub, err := channel.NewSubscriber(bus, 1)
if err != nil {
panic(err)
}
topic := goflux.Topic[Event]{
Publisher: channel.NewPublisher(bus),
Subscriber: sub,
}
gofuncy.StartWithReady(ctx, func(ctx context.Context, ready gofuncy.ReadyFunc) error {
ready()
return topic.Subscribe(ctx, "events", func(_ context.Context, msg goflux.Message[Event]) error {
fmt.Println(msg.Payload.Name)
cancel()
return nil
})
}, gofuncy.WithName("subscriber"))
time.Sleep(10 * time.Millisecond)
if err := topic.Publish(ctx, "events", Event{ID: "1", Name: "bundled"}); err != nil {
panic(err)
}
<-ctx.Done()
Output: bundled