Documentation
¶
Overview ¶
Package channel provides a generic, observable channel with optional OpenTelemetry metrics and tracing for send operations.
Index ¶
- Variables
- type Channel
- type Option
- func WithBuffer[T any](size int) Option[T]
- func WithDurationHistogram[T any]() Option[T]
- func WithLogger[T any](l *slog.Logger) Option[T]
- func WithMeterProvider[T any](mp metric.MeterProvider) Option[T]
- func WithName[T any](name string) Option[T]
- func WithTracerProvider[T any](tp trace.TracerProvider) Option[T]
- func WithTracing[T any]() Option[T]
- func WithoutChansCounter[T any]() Option[T]
- func WithoutMessagesSentCounter[T any]() Option[T]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrClosed = errors.New("channel is closed")
ErrClosed is returned when sending on a closed channel.
Functions ¶
This section is empty.
Types ¶
type Channel ¶
type Channel[T any] struct { // contains filtered or unexported fields }
Channel is a generic, observable channel with optional telemetry.
func New ¶
New creates a new Channel with the given options. Use WithName to set a custom metric/tracing label; defaults to "gofuncy.channel".
Example ¶
package main
import (
"context"
"fmt"
"github.com/foomo/gofuncy/channel"
)
func main() {
ch := channel.New[string](channel.WithBuffer[string](3))
defer ch.Close()
_ = ch.Send(context.Background(), "hello", "world")
fmt.Println(<-ch.Receive())
fmt.Println(<-ch.Receive())
}
Output: hello world
func (*Channel[T]) Close ¶
func (c *Channel[T]) Close()
Close closes the channel. It is safe to call multiple times.
type Option ¶
Option configures a Channel during construction.
func WithBuffer ¶
WithBuffer sets the channel buffer size.
func WithDurationHistogram ¶
func WithDurationHistogram[T any]() Option[T]
WithDurationHistogram enables the message send duration histogram.
func WithLogger ¶
WithLogger sets the logger for telemetry errors.
func WithMeterProvider ¶
func WithMeterProvider[T any](mp metric.MeterProvider) Option[T]
WithMeterProvider sets a custom OTel meter provider.
func WithName ¶ added in v0.2.0
WithName sets the channel name used for metrics and tracing. Defaults to "gofuncy.channel" when omitted.
func WithTracerProvider ¶
func WithTracerProvider[T any](tp trace.TracerProvider) Option[T]
WithTracerProvider sets a custom OTel tracer provider.
func WithTracing ¶
func WithTracing[T any]() Option[T]
WithTracing enables OpenTelemetry tracing for send operations.
func WithoutChansCounter ¶
func WithoutChansCounter[T any]() Option[T]
WithoutChansCounter disables the open channels counter metric.
func WithoutMessagesSentCounter ¶
func WithoutMessagesSentCounter[T any]() Option[T]
WithoutMessagesSentCounter disables the messages sent counter metric.