mysqlbroker

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package mysqlbroker 是 taskgate 的 MySQL 后端:database/sql + go-sql-driver/mysql(纯 Go 免 cgo)。 薄壳——把 MySQL 方言注入共享核心 internal/sqlbroker,认领互斥靠 FOR UPDATE SKIP LOCKED (要求 MySQL 8.0+),语义与 sqlite 后端完全对齐,过 brokertest 18 条契约(env 门控)。

MySQL 独有限制(见已知限制):ID/type/queue 最长 255 字符(Enqueue 入口校验,超限清晰报错); payload/result 受服务器 max_allowed_packet 限制(默认 64M);表用 utf8mb4_bin 排序规则(DDL 内置)。 同 pgbroker:跨进程感知延迟 = 轮询间隔(默认 200ms);不提供分布式限流;无 LISTEN/NOTIFY。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Broker

type Broker struct {
	*sqlbroker.Broker
}

Broker 包一层共享核心,只为在 Enqueue 入口加 MySQL 独有的长度校验;其余方法全部透传。

func Open

func Open(dsn string, opts ...Option) (*Broker, error)

Open 打开一个连到 dsn 的 MySQL 后端(如 "user:pass@tcp(localhost:3306)/db")。 会话级 innodb_lock_wait_timeout 调低到 5s(配合核心对 1205 的有限重试),连接排序规则设 utf8mb4_bin。 返回的 Broker 用之前必须先 Init(由 taskgate.New(cfg) 统一调用),Init 时自动建表。

func (*Broker) Enqueue

func (b *Broker) Enqueue(ctx context.Context, t *taskgate.Task) error

Enqueue 在透传给核心前先做 MySQL 独有的长度校验:id/type/queue 超过 255 字符(VARCHAR(255)) 直接返回清晰错误,不落库——否则驱动会在协议层报脏错(参照 redisbroker validateID 的先例)。

type Option

type Option func(*Options)

Option 函数式选项。

func WithMaxOpenConns

func WithMaxOpenConns(n int) Option

WithMaxOpenConns 设置连接池上限(防阻塞轮询打爆 max_connections)。

func WithPollInterval

func WithPollInterval(d time.Duration) Option

WithPollInterval 设置 Dequeue 兜底轮询间隔(跨进程新任务感知延迟)。

func WithTablePrefix

func WithTablePrefix(prefix string) Option

WithTablePrefix 设置表名/索引名前缀(多应用共享同一 MySQL 库时隔离)。

type Options

type Options struct {
	TablePrefix  string        `yaml:"table_prefix" json:"table_prefix"`     // 默认 "taskgate_"
	MaxOpenConns int           `yaml:"max_open_conns" json:"max_open_conns"` // 默认 10
	PollInterval time.Duration `yaml:"poll_interval" json:"poll_interval"`   // 默认 200ms
}

Options mysqlbroker 的可选配置,字段带 tag 供应用自己 unmarshal(库内不读环境变量,宪法 I)。

Jump to

Keyboard shortcuts

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