channel

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 13, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package channel provides a generic, observable channel with optional OpenTelemetry metrics and tracing for send operations.

Index

Examples

Constants

This section is empty.

Variables

View Source
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

func New[T any](opts ...Option[T]) *Channel[T]

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]) Cap

func (c *Channel[T]) Cap() int

Cap returns the channel buffer capacity.

func (*Channel[T]) Close

func (c *Channel[T]) Close()

Close closes the channel. It is safe to call multiple times.

func (*Channel[T]) Len

func (c *Channel[T]) Len() int

Len returns the number of elements currently in the channel buffer.

func (*Channel[T]) Name

func (c *Channel[T]) Name() string

Name returns the channel name.

func (*Channel[T]) Receive

func (c *Channel[T]) Receive() <-chan T

Receive returns the underlying receive-only channel.

func (*Channel[T]) Send

func (c *Channel[T]) Send(ctx context.Context, values ...T) error

Send sends one or more values into the channel. Returns ErrClosed if the channel has been closed, or the context error if the context is cancelled.

type Option

type Option[T any] func(*Channel[T])

Option configures a Channel during construction.

func WithBuffer

func WithBuffer[T any](size int) Option[T]

WithBuffer sets the channel buffer size.

func WithDurationHistogram

func WithDurationHistogram[T any]() Option[T]

WithDurationHistogram enables the message send duration histogram.

func WithLogger

func WithLogger[T any](l *slog.Logger) Option[T]

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

func WithName[T any](name string) Option[T]

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL