Files

46 lines
1.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
## 项目概述
基于 Redis 的分布式锁实现(Go)。使用 Redis SETNX + Lua 脚本保证原子性,支持锁获取、释放、续期和重试。
## 常用命令
```bash
# 构建
go build ./...
# 运行所有测试(需要 Redis 连接,配置在 .env)
go test ./... -v -count=1
# 运行单个测试
go test ./lock/ -v -run TestAcquireAndRelease
# 整理依赖
go mod tidy
```
## 架构
- `lock/lock.go` — 核心分布式锁实现,所有关键操作(Release、Refresh)使用 Lua 脚本保证原子性
- `lock/lock_test.go` — 集成测试,依赖真实 Redis 实例,通过 .env 的 REDIS_URL 连接
- `main.go` — 示例入口,演示锁的基本用法
## 关键设计
- **锁安全释放**:Release 使用 Lua 脚本先 GET 校验 owner 再 DEL,防止误释放别人的锁
- **锁续期**:Refresh 同样用 Lua 脚本保证只有持有者能续期
- **TTL 防死锁**:所有锁都有过期时间,即使进程崩溃也不会永久死锁
- **Owner 标识**:每次持锁需要唯一 owner(如 UUID),保证分布式环境下的安全性
## 提交规范
使用 Conventional Commits:
- `feat:` 新功能
- `fix:` 修复
- `test:` 测试相关
- `docs:` 文档
- `refactor:` 重构