Documentation
¶
Overview ¶
Package pglease — worker.LeaseStore 的 PostgreSQL 实现。 多副本对同一 key 竞争时由数据库行级约束保证唯一 winner。 走 database/sql 直执行——绕开 ORM 预编译缓存的参数计数问题。
Index ¶
- Constants
- type PGLeaseStore
- func (s *PGLeaseStore) Migrate(ctx context.Context) error
- func (s *PGLeaseStore) Release(ctx context.Context, key, holder string) error
- func (s *PGLeaseStore) TryAcquire(ctx context.Context, key, holder string, ttl time.Duration) (bool, error)
- func (s *PGLeaseStore) WithTable(name string) *PGLeaseStore
Constants ¶
View Source
const DefaultTable = "agentkit_lease"
DefaultTable 缺省租约表名。
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type PGLeaseStore ¶
type PGLeaseStore struct {
// contains filtered or unexported fields
}
PGLeaseStore 实现 worker.LeaseStore。
func NewPGLeaseStore ¶
func NewPGLeaseStore(db *sql.DB) *PGLeaseStore
NewPGLeaseStore 构造(表名用 DefaultTable;用 WithTable 覆盖)。
func (*PGLeaseStore) Migrate ¶
func (s *PGLeaseStore) Migrate(ctx context.Context) error
Migrate 建租约表。
func (*PGLeaseStore) Release ¶
func (s *PGLeaseStore) Release(ctx context.Context, key, holder string) error
Release 仅持有者本人可释放(优雅停机让位,加速故障转移)。
func (*PGLeaseStore) TryAcquire ¶
func (s *PGLeaseStore) TryAcquire(ctx context.Context, key, holder string, ttl time.Duration) (bool, error)
TryAcquire 原子获取或续约:空闲/已过期/本人持有 → true。 expires_at 用 DB 时钟(now() + make_interval)而非应用时钟——写入与过期判定 必须同钟:多实例应用时钟漂移会等量放大/缩短租约实际时长,削弱「失联后 ≤ttl 换主」的承诺(v0.10.29 审计修复)。
func (*PGLeaseStore) WithTable ¶
func (s *PGLeaseStore) WithTable(name string) *PGLeaseStore
WithTable 指定租约表名(兼容已有库的自定义表名)。 非空且不匹配标识符白名单时保持原表名(后续 SQL 仍用安全值)。
Click to show internal directories.
Click to hide internal directories.