pomerium/pkg/fanout/publish.go
Joe Kralicky fe31799eb5
Fix many instances of contexts and loggers not being propagated (#5340)
This also replaces instances where we manually write "return ctx.Err()"
with "return context.Cause(ctx)" which is functionally identical, but
will also correctly propagate cause errors if present.
2024-10-25 14:50:56 -04:00

19 lines
437 B
Go

package fanout
import "context"
// Publish publishes a message to all currently registered subscribers
// if the fanout is closed, ErrStopped is returned
func (f *FanOut[T]) Publish(ctx context.Context, msg T) error {
ctx, cancel := context.WithTimeout(ctx, f.cfg.publishTimeout)
defer cancel()
select {
case <-ctx.Done():
return context.Cause(ctx)
case <-f.done:
return ErrStopped
case f.messages <- msg:
return nil
}
}