db

package
v1.24.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const LockRetryAttempts = 3

LockRetryAttempts bounds the retry budget for transient lock conflicts. Three: enough for the pathological interleaving to have passed, few enough that a genuine hot spot surfaces as an error rather than as latency.

Variables

This section is empty.

Functions

func IsRetryableLockErr added in v1.19.0

func IsRetryableLockErr(err error) bool

IsRetryableLockErr reports whether err is a TRANSIENT InnoDB lock conflict — 1213 (deadlock) or 1205 (lock wait timeout) — and nothing else.

Everything else, including 1062 and every service sentinel, is NOT retryable: callers rely on errors.Is against their own sentinels, and re-running a transaction that failed for a permanent reason only moves the failure later.

func Migration

func Migration(sqlDir string, session *dbr.Session) error

func NewMySQL

func NewMySQL(addr string, sqlDir string, migration bool) *dbr.Session

NewMySQL 创建一个MySQL db,[path]db存储路径 [sqlDir]sql脚本目录

func NewRedis

func NewRedis(addr string, password string) *redis.Conn

NewRedis 兼容旧调用方,不会启用 TLS。 新代码应优先用 NewRedisFromConfig 以自动应用 TLS 等公共配置。

func NewRedisFromConfig added in v1.4.0

func NewRedisFromConfig(cfg *config.Config) *redis.Conn

NewRedisFromConfig 从 cfg 构造 Redis 连接,自动应用 RedisTLS / CA 等设置。

func NewSqlite

func NewSqlite(filepath string, sqlDir string) *dbr.Session

NewSqlite 创建一个sqlite db,[path]db存储路径 [sqlDir]sql脚本目录

func ReconcileThreadSchemaRecords

func ReconcileThreadSchemaRecords(ctx context.Context, db *sql.DB) error

ReconcileThreadSchemaRecords pre-seeds gorp_migrations with the thread module's migration IDs when the thread tables are already present from a prior snapshot-built install but the migration rows are missing. Without this, the next sql-migrate.Exec would try to apply CREATE TABLE `thread` (no IF NOT EXISTS) and panic with Error 1050.

Why a separate step from RewriteLegacyMigrationIDs: the legacy-ID rewrite is a 1:1 in-place rename, while this is a "the schema is here, please record that" reconciliation. Splitting them keeps each function's invariants narrow.

Idempotent on three axes:

  • fresh install (no thread tables): no-op.
  • already-reconciled install (rows present): no-op.
  • already-applied install (sql-migrate ran the migrations itself): no-op.

Call after RewriteLegacyMigrationIDs and before module.Setup.

func RetryOnLockConflict added in v1.19.0

func RetryOnLockConflict(fn func() error) error

RetryOnLockConflict re-runs fn while it fails with a transient lock conflict.

This is the canonical copy. modules/project had the first one (its unexported retryOnLockConflict now delegates here) and modules/common still carries a third, unexported, for its own callers; converging that one is a separate change because it would touch paths this transaction work does not.

Why a shared helper rather than per-module reasoning about lock order: three consecutive review rounds on the project module each found a lock-order cycle that careful reasoning had missed — first a table-order cycle against modules/space, then a row-order cycle within space_member, then a join-order inversion the OPTIMIZER chose (a single UPDATE ... JOIN whose driving table flips with cardinality, so the lock order is not even a property of the SQL text). "We reasoned about the order" is demonstrably not sufficient on its own, and the cost of being wrong is not a retry — it is a 500 on a security-relevant write, or a rolled-back member removal.

fn must own its whole transaction — meaning it BEGINs, and it rolls back on every exit it does not commit on. That requirement, not the engine, is what makes a retry from BEGIN sound.

Stating it that way because the reason given here before was wrong for half the retryable set. It said the retry is safe "because InnoDB has already rolled the failed attempt back", which is true for 1213 — a deadlock victim is rolled back whole — and FALSE for 1205: with innodb_rollback_on_timeout at its default OFF, a lock wait timeout rolls back only the failing STATEMENT and leaves the transaction open. Every fn today is a *Once function holding `defer tx.RollbackUnlessCommitted()`, so the transaction does get discarded and the helper is sound — but by the caller's discipline, not by the engine's. The wrong reason would have licensed an fn that leaves its rollback to InnoDB, and that fn would re-run from BEGIN with the previous attempt's statements still holding locks.

Anything fn wrote — including outbox rows — is gone with the rollback, so a retry cannot duplicate a side effect that lives in the same transaction.

Known, deliberately not changed here: the 1205 latency budget

Retries are immediate and identical for both codes. On 1205 the caller has already waited innodb_lock_wait_timeout — measured at 50s on the 8.0.46 engine used for this branch — so three attempts is up to 150s of one request holding a pooled connection. A backoff would make that worse rather than better, and the right shape is probably to retry 1213 with jitter and surface 1205 immediately. That is a behaviour change across eight call sites in two modules, so it belongs in its own change with its own load test, not in a branch about membership epochs.

func RewriteLegacyMigrationIDs

func RewriteLegacyMigrationIDs(ctx context.Context, db *sql.DB) error

RewriteLegacyMigrationIDs maps any legacy entries in `gorp_migrations.id` to their new timestamp-prefixed equivalents.

Why this exists: sql-migrate (rubenv/sql-migrate@v1.5.2 migrate.go:135-146) falls back to lexicographic `m.Id < other.Id` when filenames don't start with digits, so the historical `<module>-<YYYYMMDD>-<NN>.sql` scheme ordered migrations by module name first — which caused cross-module dependencies like `botfather-20260417-01.sql` (ALTERs `robot`) to run before `robot-20210926-01.sql` (CREATEs the table). The fix is to rename every file to a 14-digit timestamp prefix; the cost is that any already-applied database has the old IDs in `gorp_migrations` and would otherwise hit sql-migrate's "unknown migration in database" safety check.

This function is idempotent: it only rewrites rows whose old ID is present AND whose new ID is absent, and it leaves no trace on a fresh install (the table is empty, so the loop is a no-op).

Call this once at startup, before any call to migrate.Exec / module.Setup.

Types

type BaseModel

type BaseModel struct {
	Id        int64
	CreatedAt Time
	UpdatedAt Time
}

type FileDirMigrationSource

type FileDirMigrationSource struct {
	Dir string
}

FileDirMigrationSource 文件目录源 遇到目录进行递归获取

func (FileDirMigrationSource) FindMigrations

func (f FileDirMigrationSource) FindMigrations() ([]*migrate.Migration, error)

FindMigrations FindMigrations

type Time

type Time time.Time

func (Time) MarshalJSON

func (t Time) MarshalJSON() ([]byte, error)

func (Time) String

func (t Time) String() string

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(data []byte) (err error)

Jump to

Keyboard shortcuts

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