Documentation
¶
Index ¶
- Constants
- type FileSystemStorage
- func (x *FileSystemStorage) BuildCurrentLockFilePath(lockId string) string
- func (x *FileSystemStorage) BuildFlockFilePath(lockId string) string
- func (x *FileSystemStorage) BuildLockDirectory(lockId string) string
- func (x *FileSystemStorage) BuildLockVersionFilePath(lockId string, version storage.Version) string
- func (x *FileSystemStorage) Capabilities() []storage.StorageCapability
- func (x *FileSystemStorage) Close(ctx context.Context) error
- func (x *FileSystemStorage) CreateWithVersion(ctx context.Context, lockId string, version storage.Version, ...) error
- func (x *FileSystemStorage) DeleteWithVersion(ctx context.Context, lockId string, exceptedVersion storage.Version, ...) error
- func (x *FileSystemStorage) EnsureDirectoryExists(directory string) error
- func (x *FileSystemStorage) ExtractLockIdFromLockDirectoryName(lockDirectoryName string) string
- func (x *FileSystemStorage) Get(ctx context.Context, lockId string) (string, error)
- func (x *FileSystemStorage) GetName() string
- func (x *FileSystemStorage) GetTime(ctx context.Context) (time.Time, error)
- func (x *FileSystemStorage) Init(ctx context.Context) error
- func (x *FileSystemStorage) IsLockDirectory(path string) bool
- func (x *FileSystemStorage) List(ctx context.Context) (iterator.Iterator[*storage.LockInformation], error)
- func (x *FileSystemStorage) ReadCurrentLockInformation(lockId string) (*storage.LockInformation, error)
- func (x *FileSystemStorage) ReadLockInformationFromPath(lockVersionPath string) (*storage.LockInformation, error)
- func (x *FileSystemStorage) ReadLockLatestVersionInformation(lockId string) (*storage.LockInformation, error)
- func (x *FileSystemStorage) UpdateWithVersion(ctx context.Context, lockId string, ...) error
- func (x *FileSystemStorage) Watch(ctx context.Context, lockId string) (<-chan *storage.LockInformation, error)
Constants ¶
const ( // LockDirectoryPrefix 存放锁的目录名必须有这个前缀 LockDirectoryPrefix = "storage-lock-" // LockDirectorySuffix 存放锁的目录名必须有这个后缀 LockDirectorySuffix = ".lock" // CurrentLockFileName 当前生效的锁信息存储在这个文件中 CurrentLockFileName = "current.json" // FlockFileName 进程间互斥锁文件名。 // ⚠️ 关键:flock 锁的是【inode】而非路径。锁载体文件必须放在【永不被业务删除】的稳定位置 // (见 FlocksDirectoryName),否则 DeleteWithVersion 删除锁目录时会一并删除 flock.lock, // 此后另一进程新建同名文件得到新 inode,旧 flock 不再保护新文件,互斥被绕过(漏洞 H-1)。 FlockFileName = "flock.lock" // FlocksDirectoryName 存放所有 flock 锁载体文件的目录,位于 workspace 根下、锁目录之外。 // DeleteWithVersion 只删除锁目录(含 current.json),绝不触碰此目录,保证 flock 载体 // 文件的 inode 在锁的整个生命周期内稳定不变,flock 互斥始终有效。 FlocksDirectoryName = "flocks" )
const StorageName = "file-system-storage"
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FileSystemStorage ¶
type FileSystemStorage struct {
// contains filtered or unexported fields
}
FileSystemStorage 基于文件系统来存储锁,可以用作同一台机器上的不同进程之间互相协同工作, 优势是兼容性好,只要有文件系统能读写文件就能使用这种锁。
CAS 实现说明(漏洞 H 修复后的正确实现) ¶
此实现通过【flock 排他锁】把"读版本 + 校验 + 写入"包成一个【进程间互斥的临界区】, 从而在多进程下实现真正的线性一致 CAS——满足 go-storage 对 CapabilityCAS 的硬性要求。
每个锁目录下有一个固定的 flock.lock 文件作为互斥点:
- CreateWithVersion:flock → 若 current.json 已存在则 ErrLockAlreadyExists,否则原子写入 → unlock
- UpdateWithVersion:flock → 读 current.json 校验版本,不匹配 ErrVersionMiss,匹配则原子写入 → unlock
- DeleteWithVersion:flock → 读 current.json 校验版本,不匹配 ErrVersionMiss,匹配则删除目录 → unlock
关键:flock 是内核级进程互斥,同一 lockId 的并发 CAS 在文件系统层被串行化, 任意时刻只有一个进程能进入临界区,因此 Check-Then-Act 不再有竞态窗口,CAS 严格成立。 atomicWriteFile 的 rename 仍保留(防止读者读到半截内容),但它现在受 flock 保护,不再并发。
平台支持 ¶
- Linux / macOS:使用 syscall.Flock,多进程安全(同一台机器内)。
- 其它平台(如 Windows):flock 回退为 no-op,此时仅单进程内安全;Windows 多进程安全需用 LockFileEx 实现。
局限(不可逾越的物理边界) ¶
flock 只能保护【同一台机器、同一文件系统】上的进程互斥。跨机器/NFS 场景下 flock 行为依赖 NFS 服务端实现(NFSv4 通常支持,NFSv3 不可靠),不应直接用于跨机分布式协调——那是 关系型数据库/对象存储等真正分布式存储的领域。本实现定位是"单机多进程"分布式锁。
func NewFileSystemStorage ¶
func NewFileSystemStorage(workspace string) *FileSystemStorage
NewFileSystemStorage 基于文件系统存储锁的时候必须指定一个存储锁的工作目录
func (*FileSystemStorage) BuildCurrentLockFilePath ¶
func (x *FileSystemStorage) BuildCurrentLockFilePath(lockId string) string
BuildCurrentLockFilePath 当前锁信息文件的路径
func (*FileSystemStorage) BuildFlockFilePath ¶
func (x *FileSystemStorage) BuildFlockFilePath(lockId string) string
BuildFlockFilePath 进程间互斥锁文件的路径。 放在 workspace 根下的 flocks/ 目录中,【独立于锁目录】——这样 DeleteWithVersion 删除锁目录时不会删除 flock 载体文件,保证其 inode 稳定,flock 互斥始终有效(修复漏洞 H-1)。
func (*FileSystemStorage) BuildLockDirectory ¶
func (x *FileSystemStorage) BuildLockDirectory(lockId string) string
BuildLockDirectory 每个锁存放在一个独立的目录中
func (*FileSystemStorage) BuildLockVersionFilePath ¶
func (x *FileSystemStorage) BuildLockVersionFilePath(lockId string, version storage.Version) string
保留旧方法名以兼容可能的外部引用,内部不再使用版本文件链 BuildLockVersionFilePath 根据锁的ID和版本构建对应的锁文件地址(已废弃,保留兼容)
func (*FileSystemStorage) Capabilities ¶
func (x *FileSystemStorage) Capabilities() []storage.StorageCapability
Capabilities 声明文件系统存储支持的能力。
CapabilityCAS:成立——通过 flock 排他锁把"读版本+校验+写"串行化,多进程下线性一致
(Linux/macOS;其它平台见类型文档的平台说明)。
CapabilityReliableTime:成立——单机文件系统,本地时钟在单机内天然一致。 CapabilityAtomicDelete:成立——DeleteWithVersion 同样在 flock 临界区内"校验版本+删除"。
平台注意:CapabilityCAS/AtomicDelete 仅在支持 flock 的平台(Linux/macOS)声明。 非 Unix 平台 flock 不可用,多进程 CAS 不成立,不声明该能力——NewStorageLockWithOptions 的能力校验会拒绝将其用于分布式锁,避免静默不安全。如需 Windows 多进程安全,应实现 LockFileEx 版本的 flock 并把 flockSupported 改 true。
func (*FileSystemStorage) CreateWithVersion ¶
func (x *FileSystemStorage) CreateWithVersion(ctx context.Context, lockId string, version storage.Version, lockInformation *storage.LockInformation) error
CreateWithVersion 创建锁记录,仅在锁不存在时成功。 在 flock 排他锁临界区内检查 current.json 是否存在并创建,多进程下只有一个成功。
func (*FileSystemStorage) DeleteWithVersion ¶
func (x *FileSystemStorage) DeleteWithVersion(ctx context.Context, lockId string, exceptedVersion storage.Version, lockInformation *storage.LockInformation) error
DeleteWithVersion 如果锁的当前版本是期望的版本,则删除锁。 在 flock 排他锁临界区内完成"读版本+校验+删锁目录",多进程下线性一致。
⚠️ 关键(修复漏洞 H-1):只删除锁目录(含 current.json),【绝不删除 flock 载体文件】。 flock 载体文件位于锁目录之外的 flocks/ 目录,inode 在锁的整个生命周期内稳定不变。 若删除 flock 载体文件,另一进程会新建同名文件得到新 inode,旧 flock 不再保护新文件, 互斥被绕过——这正是删除路径上的致命竞态。保留 flock 载体文件使该竞态无法发生。 flock 载体文件的轻微磁盘占用可接受(每个 lockId 一个 0 字节文件)。
func (*FileSystemStorage) EnsureDirectoryExists ¶
func (x *FileSystemStorage) EnsureDirectoryExists(directory string) error
EnsureDirectoryExists 确保给定的路径存在并且是一个目录
func (*FileSystemStorage) ExtractLockIdFromLockDirectoryName ¶
func (x *FileSystemStorage) ExtractLockIdFromLockDirectoryName(lockDirectoryName string) string
ExtractLockIdFromLockDirectoryName 从存放锁的目录名中抽取锁的ID
func (*FileSystemStorage) GetName ¶
func (x *FileSystemStorage) GetName() string
func (*FileSystemStorage) IsLockDirectory ¶
func (x *FileSystemStorage) IsLockDirectory(path string) bool
IsLockDirectory 判断这个路径是否是一个存放锁的路径。 既接受锁目录的全路径,也接受目录名(此时回退到 workspace 下查找)。 注意:历史实现直接对传入路径做 HasPrefix 前缀判断,传入全路径时永远返回 false, 导致 List 永远为空;现改为对 filepath.Base 做格式判断,stat 用原路径, 原路径不存在时再回退到 workspace/Base 查找。
func (*FileSystemStorage) List ¶
func (x *FileSystemStorage) List(ctx context.Context) (iterator.Iterator[*storage.LockInformation], error)
func (*FileSystemStorage) ReadCurrentLockInformation ¶
func (x *FileSystemStorage) ReadCurrentLockInformation(lockId string) (*storage.LockInformation, error)
ReadCurrentLockInformation 读取当前锁的信息,如果锁不存在则返回 (nil, nil)
func (*FileSystemStorage) ReadLockInformationFromPath ¶
func (x *FileSystemStorage) ReadLockInformationFromPath(lockVersionPath string) (*storage.LockInformation, error)
ReadLockInformationFromPath 从文件中读取锁信息(保留兼容)
func (*FileSystemStorage) ReadLockLatestVersionInformation ¶
func (x *FileSystemStorage) ReadLockLatestVersionInformation(lockId string) (*storage.LockInformation, error)
ReadLockLatestVersionInformation 保留旧方法以兼容,内部统一改用 ReadCurrentLockInformation
func (*FileSystemStorage) UpdateWithVersion ¶
func (x *FileSystemStorage) UpdateWithVersion(ctx context.Context, lockId string, exceptedVersion, newVersion storage.Version, lockInformation *storage.LockInformation) error
UpdateWithVersion 如果锁的当前版本是期望的版本,则更新为新版本。 在 flock 排他锁临界区内完成"读版本+校验+写",多进程下线性一致。
func (*FileSystemStorage) Watch ¶
func (x *FileSystemStorage) Watch(ctx context.Context, lockId string) (<-chan *storage.LockInformation, error)