idempotent

package
v1.63.0 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package idempotent 提供基于 Redis 的业务幂等防护: 首次请求执行业务并占用幂等标记,重复请求(回调重试/用户连点/消息重投)直接拒绝。 与分布式锁(cache.Lock)的区别:锁是执行期间互斥(用完释放); 幂等标记是结果性占用(执行成功后仍保留,TTL 内重复请求一律拒绝)。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Guard

type Guard struct {
	// contains filtered or unexported fields
}

Guard 幂等守卫:基于 Redis SETNX 的标记占用。

func NewGuard

func NewGuard(rdb *redis.Client, prefix string) *Guard

NewGuard 创建幂等守卫。prefix 为 key 统一前缀(如 "idem:gift"),可为空。

func (*Guard) Check

func (g *Guard) Check(ctx context.Context, bizKey string, ttl time.Duration) (bool, error)

Check 检查业务键是否首次执行: 首次执行返回 true 并占用标记;重复请求返回 false(业务应直接拒绝或返回缓存结果)。 ttl 控制标记存活期(支付回调 24h、防重复提交 10min 等),非正数默认 10 分钟。

func (*Guard) Release

func (g *Guard) Release(ctx context.Context, bizKey string) error

Release 手动释放幂等标记(业务执行失败时回滚,允许重试)。 业务成功后不应调用(标记保留用于拒绝重复请求)。

func (*Guard) Status

func (g *Guard) Status(ctx context.Context, bizKey string) (bool, error)

Status 查询幂等标记是否存在(存在 = 已执行过)。

Jump to

Keyboard shortcuts

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