Documentation
¶
Index ¶
- type Coordinator
- type CoordinatorConfig
- type CoordinatorConfigOpt
- func WithCoordinatorHooks(m CoordinatorHooks) CoordinatorConfigOpt
- func WithCoordinatorSessionTimeout(timeout time.Duration) CoordinatorConfigOpt
- func WithInitialRebalanceDelay(delay time.Duration) CoordinatorConfigOpt
- func WithKVCoordinatorBucket(bucket string) CoordinatorConfigOpt
- func WithKVKeyCoordinator(key string) CoordinatorConfigOpt
- func WithKVOffsetsBucket(bucket string) CoordinatorConfigOpt
- func WithLogger(logger *slog.Logger) CoordinatorConfigOpt
- func WithMaxConcurrentRequestsPerGroup(n int) CoordinatorConfigOpt
- func WithMaxConsumersPerGroup(n int) CoordinatorConfigOpt
- func WithSubjectPrefix(prefix string) CoordinatorConfigOpt
- type CoordinatorHooks
- type Message
- type PartitionResolver
- type Reader
- type ReaderConfig
- type ReaderHooks
- type ReaderOption
- func WithAssigner(a cg.Assigner) ReaderOption
- func WithBufferSize(size int) ReaderOption
- func WithFetchBatchTimeout(d time.Duration) ReaderOption
- func WithFetchWaitTime(d time.Duration) ReaderOption
- func WithHeartbeatInterval(d time.Duration) ReaderOption
- func WithPartitionResolver(resolver PartitionResolver) ReaderOption
- func WithReaderHooks(h ReaderHooks) ReaderOption
- func WithReaderLogger(logger *slog.Logger) ReaderOption
- func WithReaderSubjectPrefix(prefix string) ReaderOption
- func WithRebalanceTimeout(d time.Duration) ReaderOption
- func WithRequestTimeout(d time.Duration) ReaderOption
- func WithSessionTimeout(d time.Duration) ReaderOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Coordinator ¶
type Coordinator struct {
// contains filtered or unexported fields
}
Coordinator is the NATS-based coordinator that manages consumer groups. It uses JetStream KV for leader election and offset storage.
func NewCoordinator ¶
func NewCoordinator(ctx context.Context, nc *nats.Conn, js jetstream.JetStream, oo ...CoordinatorConfigOpt) (*Coordinator, error)
NewCoordinator creates a new coordinator instance.
func (*Coordinator) Run ¶
func (c *Coordinator) Run(ctx context.Context)
Run starts the coordinator's main loop. It attempts to acquire leadership and runs as leader until context is cancelled.
type CoordinatorConfig ¶
type CoordinatorConfig struct {
// KVReplicas is the number of replicas for JetStream KV buckets.
KVReplicas int
// KVBucketCoordinator is the KV bucket name used for leader election.
KVBucketCoordinator string
// KVBucketOffsets is the KV bucket name used for offset storage.
KVBucketOffsets string
// KVKeyCoordinator is the key name within the coordinator KV bucket
// used for leader election.
KVKeyCoordinator string
// CoordinatorSessionTimeout is the TTL for the coordinator leadership lease.
// The coordinator must renew its lease before this timeout expires.
CoordinatorSessionTimeout time.Duration
// InitialRebalanceDelay is the delay before triggering the first rebalance
// when a new group is created with no previous members.
InitialRebalanceDelay time.Duration
// SubjectPrefix is the NATS subject prefix for coordinator RPC communication.
SubjectPrefix string
// MaxConcurrentRequests is the worker pool size for handling concurrent
// requests from consumers.
MaxConcurrentRequests int
// MaxConsumersPerGroup limits the maximum number of consumers allowed per group.
// 0 means unlimited.
MaxConsumersPerGroup int
// Logger is used for logging coordinator events and errors.
Logger *slog.Logger
// Hooks holds optional callbacks for coordinator observability.
Hooks CoordinatorHooks
}
CoordinatorConfig holds configuration for the coordinator.
type CoordinatorConfigOpt ¶
type CoordinatorConfigOpt func(CoordinatorConfig) CoordinatorConfig
CoordinatorConfigOpt is a function that modifies a Config.
func WithCoordinatorHooks ¶
func WithCoordinatorHooks(m CoordinatorHooks) CoordinatorConfigOpt
WithCoordinatorHooks sets the coordinator hooks callbacks.
func WithCoordinatorSessionTimeout ¶
func WithCoordinatorSessionTimeout(timeout time.Duration) CoordinatorConfigOpt
WithCoordinatorSessionTimeout sets the session timeout for the coordinator.
func WithInitialRebalanceDelay ¶
func WithInitialRebalanceDelay(delay time.Duration) CoordinatorConfigOpt
WithInitialRebalanceDelay sets the delay before triggering a rebalance after a member joins.
func WithKVCoordinatorBucket ¶
func WithKVCoordinatorBucket(bucket string) CoordinatorConfigOpt
WithKVCoordinatorBucket sets the KV bucket name for coordinator state.
func WithKVKeyCoordinator ¶
func WithKVKeyCoordinator(key string) CoordinatorConfigOpt
WithKVKeyCoordinator sets the key name used within the coordinator KV bucket for leader election.
func WithKVOffsetsBucket ¶
func WithKVOffsetsBucket(bucket string) CoordinatorConfigOpt
WithKVOffsetsBucket sets the KV bucket name for offset storage.
func WithLogger ¶
func WithLogger(logger *slog.Logger) CoordinatorConfigOpt
WithLogger sets the logger for the coordinator.
func WithMaxConcurrentRequestsPerGroup ¶
func WithMaxConcurrentRequestsPerGroup(n int) CoordinatorConfigOpt
WithMaxConcurrentRequestsPerGroup sets the maximum number of concurrent requests per group.
func WithMaxConsumersPerGroup ¶
func WithMaxConsumersPerGroup(n int) CoordinatorConfigOpt
WithMaxConsumersPerGroup sets the maximum number of consumers allowed per group.
func WithSubjectPrefix ¶
func WithSubjectPrefix(prefix string) CoordinatorConfigOpt
WithSubjectPrefix sets the NATS subject prefix for coordinator communication.
type CoordinatorHooks ¶
type CoordinatorHooks struct {
// Elected is called when this coordinator becomes the leader.
Elected func()
// LostLeader is called when leadership is lost unexpectedly.
LostLeader func()
// SteppingDown is called when the coordinator voluntarily steps down.
SteppingDown func()
// RebalanceStarted is called when a group rebalance begins.
RebalanceStarted func(group string, generation uint64)
}
CoordinatorHooks holds optional hooks for coordinator observability.
type Message ¶
type Message struct {
// Stream is the JetStream stream this message came from.
Stream string
// Subject is the NATS subject this message was published to.
Subject string
// Data is the message payload.
Data []byte
// Partition is the partition number this message belongs to.
Partition int
// Sequence is the message sequence number within the partition.
Sequence uint64
// Headers contains optional message headers.
Headers map[string][]string
// Timestamp is when the message was published.
Timestamp time.Time
}
Message represents a message received from a partition.
type PartitionResolver ¶
PartitionResolver maps a partition index to a stream and subject.
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader provides methods to read messages from assigned partitions.
func NewReader ¶
func NewReader(nc *nats.Conn, js jetstream.JetStream, group string, partitions int, opts ...ReaderOption) (*Reader, error)
NewReader creates a new Reader for consuming messages from partitions. The reader handles partition assignment, rebalancing, and offset management.
type ReaderConfig ¶
type ReaderConfig struct {
// PartitionResolver maps partition index to stream and subject (required).
PartitionResolver PartitionResolver
// BufferSize is the size of the internal message buffer.
BufferSize int
// SessionTimeout is how long before the consumer is considered dead
// if no heartbeat is received.
SessionTimeout time.Duration
// RebalanceTimeout is how long to wait for consumers to rejoin during rebalance.
RebalanceTimeout time.Duration
// HeartbeatInterval is how often to send heartbeats to the coordinator.
HeartbeatInterval time.Duration
// RequestTimeout is the timeout for coordinator RPC requests.
RequestTimeout time.Duration
// FetchWaitTime is how long to wait for messages when fetching.
FetchWaitTime time.Duration
// FetchBatchTimeout is the timeout for collecting additional messages after
// the first message is received.
FetchBatchTimeout time.Duration
// SubjectPrefix is the prefix for coordinator RPC subjects.
SubjectPrefix string
// Assigner is the partition assignment strategy.
Assigner cg.Assigner
// Logger is used for logging errors.
Logger *slog.Logger
// Hooks holds optional callbacks for reader observability.
Hooks ReaderHooks
}
ReaderConfig holds configuration options for a Reader.
type ReaderHooks ¶
type ReaderHooks struct {
// PartitionsAssigned is called when partitions are assigned to this reader.
PartitionsAssigned func(partitions []int)
// PartitionsRevoked is called when partitions are revoked from this reader.
PartitionsRevoked func(partitions []int)
}
ReaderHooks holds optional callbacks for reader observability.
type ReaderOption ¶
type ReaderOption func(*ReaderConfig)
ReaderOption is a function that modifies ReaderConfig.
func WithAssigner ¶
func WithAssigner(a cg.Assigner) ReaderOption
WithAssigner sets a custom partition assigner.
func WithBufferSize ¶
func WithBufferSize(size int) ReaderOption
WithBufferSize sets the internal message buffer size.
func WithFetchBatchTimeout ¶
func WithFetchBatchTimeout(d time.Duration) ReaderOption
WithFetchBatchTimeout sets the timeout for collecting additional messages after the first message is received.
func WithFetchWaitTime ¶
func WithFetchWaitTime(d time.Duration) ReaderOption
WithFetchWaitTime sets the fetch wait time.
func WithHeartbeatInterval ¶
func WithHeartbeatInterval(d time.Duration) ReaderOption
WithHeartbeatInterval sets the heartbeat interval.
func WithPartitionResolver ¶
func WithPartitionResolver(resolver PartitionResolver) ReaderOption
WithPartitionResolver sets the partition resolver function.
func WithReaderHooks ¶
func WithReaderHooks(h ReaderHooks) ReaderOption
WithReaderHooks sets the hooks for the reader.
func WithReaderLogger ¶
func WithReaderLogger(logger *slog.Logger) ReaderOption
WithReaderLogger sets the logger for the reader.
func WithReaderSubjectPrefix ¶
func WithReaderSubjectPrefix(prefix string) ReaderOption
WithReaderSubjectPrefix sets the subject prefix for coordinator RPC.
func WithRebalanceTimeout ¶
func WithRebalanceTimeout(d time.Duration) ReaderOption
WithRebalanceTimeout sets the rebalance timeout.
func WithRequestTimeout ¶
func WithRequestTimeout(d time.Duration) ReaderOption
WithRequestTimeout sets the request timeout.
func WithSessionTimeout ¶
func WithSessionTimeout(d time.Duration) ReaderOption
WithSessionTimeout sets the session timeout.