Documentation
¶
Index ¶
- type BatchHandlerFunc
- type BatchTxHandlerFunc
- type Client
- func (c *Client) HandleFetches(ctx context.Context) error
- func (c *Client) Produce(ctx context.Context, record *kgo.Record, promise PromiseFunc)
- func (c *Client) ProduceSync(ctx context.Context, records ...*kgo.Record) error
- func (c *Client) RunInTx(ctx context.Context, fn TxFunc) (err error)
- func (c *Client) Shutdown(ctx context.Context) error
- func (c *Client) TryProduce(ctx context.Context, record *kgo.Record, promise PromiseFunc)
- type Config
- type ConsumerOpt
- func WithConsumeResetOffset(offset kgo.Offset) ConsumerOpt
- func WithConsumerBatchHandler(handler BatchHandlerFunc) ConsumerOpt
- func WithFetchIsolationLevel(level kgo.IsolationLevel) ConsumerOpt
- func WithGroupTransactSessionBatchHandler(handler BatchTxHandlerFunc) ConsumerOpt
- func WithShareGroupBatchHandler(handler BatchHandlerFunc) ConsumerOpt
- type GroupOpt
- type GroupTransactSession
- func (g *GroupTransactSession) HandleFetches(ctx context.Context) error
- func (g *GroupTransactSession) Produce(ctx context.Context, record *kgo.Record, promise PromiseFunc)
- func (g *GroupTransactSession) ProduceSync(ctx context.Context, records ...*kgo.Record) error
- func (g *GroupTransactSession) Shutdown(_ context.Context) error
- func (g *GroupTransactSession) TryProduce(ctx context.Context, record *kgo.Record, promise PromiseFunc)
- type Opt
- func WithClientID(s string) Opt
- func WithConfig(config *Config) Opt
- func WithLogger(logger *slog.Logger) Opt
- func WithMeterProvider(provider metric.MeterProvider) Opt
- func WithMetricLabel(key, value string) Opt
- func WithMetricsNamespace(namespace string) Opt
- func WithTLS(tlsConfig *tls.Config) Opt
- func WithTracerPropagator(propagator propagation.TextMapPropagator) Opt
- func WithTracerProvider(provider trace.TracerProvider) Opt
- type ProducerOpt
- func WithAllowIdempotentProduceCancellation() ProducerOpt
- func WithProducePromise(promiseFunc PromiseFunc) ProducerOpt
- func WithProducerBatchCompression(preference ...kgo.CompressionCodec) ProducerOpt
- func WithProducerBatchMaxBytesFn(fn func(topic string) int32) ProducerOpt
- func WithRecordPartitioner(partitioner kgo.Partitioner) ProducerOpt
- func WithRequiredAcks(acks kgo.Acks) ProducerOpt
- type PromiseFunc
- type Tx
- type TxFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BatchHandlerFunc ¶
BatchHandlerFunc handles a batch of consumed Kafka records.
type BatchTxHandlerFunc ¶ added in v0.3.1
BatchTxHandlerFunc handles a batch of consumed Kafka records inside a group transaction.
type Client ¶ added in v0.3.1
type Client struct {
// contains filtered or unexported fields
}
Client is a high-level Kafka client for producing records and consuming records through handlers.
func NewClient ¶ added in v0.3.1
NewClient creates a Kafka client with the provided options and verifies broker connectivity.
func (*Client) HandleFetches ¶ added in v0.3.1
func (*Client) ProduceSync ¶ added in v0.3.1
func (*Client) RunInTx ¶ added in v0.3.1
RunInTx executes fn inside a Kafka transaction.
The transaction is committed when fn returns nil. It is aborted when fn returns an error. Panics are recovered only long enough to abort the transaction, then re-thrown.
func (*Client) Shutdown ¶ added in v0.3.1
Shutdown stops polling, flushes pending records and acks, and closes the underlying client.
func (*Client) TryProduce ¶ added in v0.3.1
type Config ¶ added in v0.3.1
type Config struct {
// Brokers sets the seed brokers for the client to use.
//
// Any seed without a port uses the default Kafka port 9092.
Brokers string `env:"KAFKA_BROKERS"`
// SASLMechanism is the SASL mechanism used for authentication.
// Supported values: PLAIN, SCRAM-SHA-256, SCRAM-SHA-512.
SASLMechanism string `env:"KAFKA_SASL_MECHANISM"`
// User is the SASL username.
User string `env:"KAFKA_USER"`
// Password is the SASL password.
Password string `env:"KAFKA_PASSWORD"`
// RequestTimeoutOverhead adds extra time while setting request deadlines.
RequestTimeoutOverhead time.Duration `env:"KAFKA_REQUEST_TIMEOUT_OVERHEAD"`
// ConnIdleTimeout controls how long idle broker connections are kept open.
ConnIdleTimeout time.Duration `env:"KAFKA_CONN_IDLE_TIMEOUT"`
// DialTimeout sets the timeout for opening broker connections.
DialTimeout time.Duration `env:"KAFKA_DIAL_TIMEOUT"`
// RequestRetries sets how many times retryable requests may be retried.
RequestRetries *int `env:"KAFKA_REQUEST_RETRIES"`
// RetryTimeout limits the total time allowed for retrying a request.
RetryTimeout time.Duration `env:"KAFKA_RETRY_TIMEOUT"`
// BrokerMaxWriteBytes limits how many bytes may be written to a broker in one write.
BrokerMaxWriteBytes *int32 `env:"KAFKA_MAX_WRITE_BYTES"`
// BrokerMaxReadBytes limits the maximum response size read from a broker.
BrokerMaxReadBytes *int32 `env:"KAFKA_MAX_READ_BYTES"`
// MetadataMaxAge sets the maximum age of cached Kafka metadata.
MetadataMaxAge time.Duration `env:"KAFKA_METADATA_MAX_AGE"`
// MetadataMinAge sets the minimum time between metadata refreshes.
MetadataMinAge time.Duration `env:"KAFKA_METADATA_MIN_AGE"`
// AlwaysRetryEOF retries EOF errors instead of treating them as terminal connection failures.
AlwaysRetryEOF bool `env:"KAFKA_ALWAYS_RETRY_EOF"`
// DefaultProduceTopic sets the default topic for records without an explicit topic.
DefaultProduceTopic string `env:"KAFKA_DEFAULT_PRODUCE_TOPIC"`
// ProducerBatchMaxBytes limits the maximum size of a produced record batch.
ProducerBatchMaxBytes int32 `env:"KAFKA_PRODUCER_BATCH_MAX_BYTES"`
// MaxBufferedRecords limits how many records the producer may buffer.
MaxBufferedRecords int `env:"KAFKA_MAX_BUFFERED_RECORDS"`
// MaxBufferedBytes limits how many bytes the producer may buffer.
MaxBufferedBytes int `env:"KAFKA_MAX_BUFFERED_BYTES"`
// ProduceRequestTimeout limits how long brokers may take to respond to produce requests.
ProduceRequestTimeout time.Duration `env:"KAFKA_PRODUCE_REQUEST_TIMEOUT"`
// RecordRetries sets how many times producing records may be retried.
RecordRetries int `env:"KAFKA_RECORD_RETRIES"`
// ProducerLinger sets how long records may wait for batching before a produce request is built.
ProducerLinger *time.Duration `env:"KAFKA_PRODUCER_LINGER"`
// RecordDeliveryTimeout limits how long a record may remain buffered before timing out.
RecordDeliveryTimeout time.Duration `env:"KAFKA_RECORD_DELIVERY_TIMEOUT"`
// TransactionalID identifies the transactional producer.
//
// It is used by producer transactions and group transact sessions.
TransactionalID string `env:"KAFKA_TRANSACTIONAL_ID"`
// TransactionTimeout limits how long a Kafka transaction may remain open.
TransactionTimeout time.Duration `env:"KAFKA_TRANSACTION_TIMEOUT"`
// ConsumeRegex treats configured topics as regular expressions.
ConsumeRegex bool `env:"KAFKA_CONSUME_REGEX"`
// Topics contains Kafka topics consumed by the consumer or group transact session.
Topics string `env:"KAFKA_TOPICS"`
// MaxPollRecords limits how many records are handled in one poll iteration.
MaxPollRecords int `env:"KAFKA_MAX_POLL_RECORDS"`
// FetchMaxWait limits how long a broker may wait before returning a fetch response.
FetchMaxWait time.Duration `env:"KAFKA_FETCH_MAX_WAIT"`
// FetchMaxBytes limits the maximum bytes returned in one fetch response.
FetchMaxBytes int32 `env:"KAFKA_FETCH_MAX_BYTES"`
// FetchMinBytes sets the minimum bytes a broker tries to return in one fetch response.
FetchMinBytes int32 `env:"KAFKA_FETCH_MIN_BYTES"`
// FetchMaxPartitionBytes limits how many bytes may be fetched from one partition.
FetchMaxPartitionBytes int32 `env:"KAFKA_FETCH_MAX_PARTITION_BYTES"`
// DisableFetchSessions disables Kafka fetch sessions.
DisableFetchSessions bool `env:"KAFKA_DISABLE_FETCH_SESSIONS"`
// Rack identifies the client rack for rack-aware fetching and consumer assignment.
Rack string `env:"KAFKA_RACK"`
// MaxConcurrentFetches limits how many fetch requests may be in flight or buffered at once.
//
// It can be used together with FetchMaxBytes to bound consumer memory usage.
MaxConcurrentFetches *int `env:"KAFKA_MAX_CONCURRENT_FETCHES"`
// Group identifies the Kafka consumer group.
Group string `env:"KAFKA_GROUP"`
// InstanceID sets a static consumer group member ID.
InstanceID string `env:"KAFKA_INSTANCE_ID"`
// SessionTimeout controls how long a group member may go without heartbeating.
SessionTimeout time.Duration `env:"KAFKA_SESSION_TIMEOUT"`
// RebalanceTimeout controls how long group members may take during a rebalance.
RebalanceTimeout time.Duration `env:"KAFKA_REBALANCE_TIMEOUT"`
// HeartbeatInterval controls how often a group member sends heartbeats.
HeartbeatInterval time.Duration `env:"KAFKA_HEARTBEAT_INTERVAL"`
//
// If set, share group consuming is used instead of a regular consumer group.
ShareGroup string `env:"KAFKA_SHARE_GROUP"`
//
// If zero, the franz-go default is used.
ShareMaxRecords int32 `env:"KAFKA_SHARE_MAX_RECORDS"`
ShareMaxRecordsStrict bool `env:"KAFKA_SHARE_MAX_RECORDS_STRICT"`
//
// If zero, failed records are released for redelivery.
ShareRejectAfterDeliveries int32 `env:"KAFKA_SHARE_REJECT_AFTER_DELIVERIES"`
//
// If zero, failed records are released immediately.
ShareReleaseTimeout time.Duration `env:"KAFKA_SHARE_RELEASE_TIMEOUT"`
// Enabled enables this Kafka component.
Enabled bool `env:"KAFKA_ENABLED"`
// SkipFatalErrors allows the consumer to continue after non-retriable fetch errors.
SkipFatalErrors bool `env:"KAFKA_SKIP_FATAL_ERRORS"`
// PollInterval sets the interval between consumer poll iterations.
PollInterval time.Duration `env:"KAFKA_POLL_INTERVAL"`
// SuspendProcessingTimeout sets the wait time after handler errors.
SuspendProcessingTimeout time.Duration `env:"KAFKA_SUSPEND_PROCESSING_TIMEOUT"`
// SuspendCommittingTimeout sets the wait time after offset commit or ack errors.
SuspendCommittingTimeout time.Duration `env:"KAFKA_SUSPEND_COMMITTING_TIMEOUT"`
}
type ConsumerOpt ¶ added in v0.3.1
type ConsumerOpt interface {
Opt
// contains filtered or unexported methods
}
ConsumerOpt is a consumer-specific option to configure a client.
This is simply a namespaced Opt.
func WithConsumeResetOffset ¶
func WithConsumeResetOffset(offset kgo.Offset) ConsumerOpt
func WithConsumerBatchHandler ¶
func WithConsumerBatchHandler(handler BatchHandlerFunc) ConsumerOpt
func WithFetchIsolationLevel ¶
func WithFetchIsolationLevel(level kgo.IsolationLevel) ConsumerOpt
func WithGroupTransactSessionBatchHandler ¶ added in v0.3.1
func WithGroupTransactSessionBatchHandler(handler BatchTxHandlerFunc) ConsumerOpt
func WithShareGroupBatchHandler ¶ added in v0.4.2
func WithShareGroupBatchHandler(handler BatchHandlerFunc) ConsumerOpt
type GroupOpt ¶ added in v0.3.1
type GroupOpt interface {
Opt
// contains filtered or unexported methods
}
GroupOpt is a consumer group-specific option to configure a client.
This is simply a namespaced Opt.
func WithBalancers ¶ added in v0.3.1
func WithBalancers(balancers ...kgo.GroupBalancer) GroupOpt
func WithShareAckCallback ¶ added in v0.3.1
func WithShareAckCallback(fn func(*kgo.Client, kgo.ShareAckResults)) GroupOpt
type GroupTransactSession ¶ added in v0.3.1
type GroupTransactSession struct {
// contains filtered or unexported fields
}
GroupTransactSession consumes records and produces records in the same Kafka transaction.
It is intended for Kafka-to-Kafka consume-process-produce flows where consumed offsets and produced records must be committed atomically.
func NewGroupTransactSession ¶ added in v0.3.1
func NewGroupTransactSession(opts ...Opt) (*GroupTransactSession, error)
NewGroupTransactSession creates a Kafka group transaction session and verifies broker connectivity.
func (*GroupTransactSession) HandleFetches ¶ added in v0.3.1
func (g *GroupTransactSession) HandleFetches(ctx context.Context) error
func (*GroupTransactSession) Produce ¶ added in v0.3.1
func (g *GroupTransactSession) Produce(ctx context.Context, record *kgo.Record, promise PromiseFunc)
func (*GroupTransactSession) ProduceSync ¶ added in v0.3.1
func (*GroupTransactSession) Shutdown ¶ added in v0.3.1
func (g *GroupTransactSession) Shutdown(_ context.Context) error
Shutdown stops polling and closes the group transaction session.
func (*GroupTransactSession) TryProduce ¶ added in v0.3.1
func (g *GroupTransactSession) TryProduce(ctx context.Context, record *kgo.Record, promise PromiseFunc)
type Opt ¶ added in v0.3.1
type Opt interface {
// contains filtered or unexported methods
}
func WithClientID ¶ added in v0.3.1
func WithConfig ¶ added in v0.3.1
func WithLogger ¶ added in v0.3.1
func WithMeterProvider ¶ added in v0.3.1
func WithMeterProvider(provider metric.MeterProvider) Opt
func WithMetricLabel ¶ added in v0.3.1
func WithMetricsNamespace ¶ added in v0.3.1
func WithTracerPropagator ¶ added in v0.3.1
func WithTracerPropagator(propagator propagation.TextMapPropagator) Opt
func WithTracerProvider ¶ added in v0.3.1
func WithTracerProvider(provider trace.TracerProvider) Opt
type ProducerOpt ¶ added in v0.3.1
type ProducerOpt interface {
Opt
// contains filtered or unexported methods
}
ProducerOpt is a producer-specific option to configure a client.
This is simply a namespaced Opt.
func WithAllowIdempotentProduceCancellation ¶ added in v0.3.1
func WithAllowIdempotentProduceCancellation() ProducerOpt
func WithProducePromise ¶ added in v0.3.1
func WithProducePromise(promiseFunc PromiseFunc) ProducerOpt
func WithProducerBatchCompression ¶
func WithProducerBatchCompression(preference ...kgo.CompressionCodec) ProducerOpt
func WithProducerBatchMaxBytesFn ¶ added in v0.3.1
func WithProducerBatchMaxBytesFn(fn func(topic string) int32) ProducerOpt
func WithRecordPartitioner ¶ added in v0.3.1
func WithRecordPartitioner(partitioner kgo.Partitioner) ProducerOpt
func WithRequiredAcks ¶ added in v0.3.1
func WithRequiredAcks(acks kgo.Acks) ProducerOpt
type PromiseFunc ¶ added in v0.4.2
PromiseFunc is called when an asynchronous produce request completes.
type Tx ¶ added in v0.4.0
type Tx struct {
// contains filtered or unexported fields
}
Tx exposes producing operations inside a Kafka transaction.
func (Tx) Produce ¶ added in v0.4.0
Produce asynchronously produces a record inside the current Kafka transaction.
func (Tx) ProduceSync ¶ added in v0.4.0
ProduceSync synchronously produces records inside the current Kafka transaction.
func (Tx) TryProduce ¶ added in v0.4.0
TryProduce attempts to enqueue a record inside the current Kafka transaction without blocking on producer backpressure.