Documentation
¶
Index ¶
- type Client
- type Config
- type Consumer
- type Cron
- type CronConfig
- type CronMiddleware
- type DelayMode
- type EnqueueOption
- type Event
- type EventBus
- type EventBusConfig
- type EventMiddleware
- type ExponentialBackoff
- type Filter
- type Handler
- type IdempotencyConfig
- type Job
- type JobConfig
- type JobHandler
- type JobMiddleware
- type Jobs
- type KV
- type Logger
- type LoggerConfig
- type MQ
- type MQConfig
- type MQProvider
- type Message
- type Middleware
- type Option
- type Producer
- type RabbitMQConfig
- type RedisConfig
- type RedisKV
- type RetryConfig
- type RetryPolicy
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Config ¶
type Config struct { MQ MQConfig Job JobConfig Cron CronConfig EventBus EventBusConfig Logger LoggerConfig // Idempotency 可选配置:若提供 KV/Redis,则默认启用 Jobs 与 EventBus 的幂等检查。 Idempotency IdempotencyConfig }
Config 为包总配置,应用通过 New 传入。
type Consumer ¶
type Consumer interface { // Consume 订阅 topic,group 为消费组;返回停止函数。 Consume(ctx context.Context, topic, group string, handler Handler, mws ...Middleware) (stop func(context.Context) error, err error) }
Consumer 统一消费接口。
type Cron ¶
type Cron interface { Add(spec string, name string, fn func(context.Context) error, mws ...CronMiddleware) (id string, err error) Remove(id string) error Start(ctx context.Context) error Stop(ctx context.Context) error }
Cron 提供基于 Cron 表达式的任务调度管理。
type CronConfig ¶
type CronMiddleware ¶
CronMiddleware 用于 Cron 任务。
type Event ¶
type Event struct { Topic string Type string Subject string Metadata map[string]string Payload []byte }
Event 领域事件。
type EventBus ¶
type EventBus interface { Publish(ctx context.Context, e Event) error Subscribe(topic, group string, filter Filter, handler func(context.Context, Event) error, mws ...EventMiddleware) (stop func(context.Context) error, err error) }
EventBus 提供事件发布与订阅。
type EventBusConfig ¶
type EventBusConfig struct {
SubscriberConcurrency int
}
type EventMiddleware ¶
type EventMiddleware func(next func(context.Context, Event) error) func(context.Context, Event) error
EventMiddleware 用于 EventBus。
type ExponentialBackoff ¶
ExponentialBackoff 简单指数回退策略。
func (ExponentialBackoff) NextBackoff ¶
func (e ExponentialBackoff) NextBackoff(attempt int) (time.Duration, bool)
type IdempotencyConfig ¶
type IdempotencyConfig struct { KV KV // 可选:键值存储(生产用 RedisKV),若为 nil 且提供 Redis* 或使用 MQ.Redis,则自动创建 // 可选 Redis 连接参数(若 KV 为空则使用这些参数自动启用) RedisAddr string RedisUsername string RedisPassword string RedisDB int Prefix string // key 前缀,如 "tq:idem" TTL time.Duration // 幂等键过期时间 KeyFunc func(ctx context.Context, m Message) (string, error) // 可选:自定义业务唯一键 }
IdempotencyConfig 配置幂等中间件。 Key 计算顺序:优先 Message.Key;若为空且提供 KeyFunc,则使用 KeyFunc。 最终存储 key 为 Prefix + ":" + sha1(keyRaw)。
type JobConfig ¶
type JobConfig struct { Retry RetryConfig DeadLetterTopic string }
type JobHandler ¶
JobHandler 用于中间件包装 Job 执行。
type JobMiddleware ¶
type JobMiddleware func(next JobHandler) JobHandler
JobMiddleware 用于 Job 执行。
func NewJobIdempotencyMiddleware ¶
func NewJobIdempotencyMiddleware(cfg IdempotencyConfig) JobMiddleware
NewJobIdempotencyMiddleware 生成 JobMiddleware。 Job 场景下,构造 Message 计算 key(jobName+payload 或自定义)。
type Jobs ¶
type Jobs interface { Register(job Job) Enqueue(ctx context.Context, jobName string, payload []byte, opts ...EnqueueOption) error StartWorkers(ctx context.Context, groups map[string]int, mws ...JobMiddleware) (stop func(context.Context) error, err error) }
Jobs 提供任务入队与 Worker 管理。
type KV ¶
type KV interface {
SetNX(ctx context.Context, key string, value string, ttl time.Duration) (bool, error)
}
KV 是幂等中间件依赖的最小键值接口,便于单元测试注入 mock。
type Logger ¶
type Logger interface { Info(ctx context.Context, msg string, kv ...interface{}) Error(ctx context.Context, msg string, kv ...interface{}) }
Logger 为最小日志接口,应用可注入自定义实现。
type LoggerConfig ¶
type LoggerConfig struct {
Level string
}
type MQConfig ¶
type MQConfig struct { Provider MQProvider RabbitMQ RabbitMQConfig Redis RedisConfig }
type MQProvider ¶
type MQProvider string
const ( MQProviderRabbitMQ MQProvider = "rabbitmq" MQProviderRedis MQProvider = "redis" )
type Middleware ¶
Middleware 用于 MQ Handler。
func NewIdempotencyMiddleware ¶
func NewIdempotencyMiddleware(cfg IdempotencyConfig) Middleware
NewIdempotencyMiddleware 生成通用 MQ Middleware。
type Producer ¶
type Producer interface { Publish(ctx context.Context, msg Message) error PublishDelay(ctx context.Context, msg Message, delay time.Duration) error }
Producer 统一发布接口。
type RabbitMQConfig ¶
type RedisConfig ¶
Click to show internal directories.
Click to hide internal directories.