utils

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: MIT Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

utils

通用工具,包含任务池、本地持久化辅助函数和不可变文件只读视图, 父包可独立用于其他 Go 程序,仅依赖标准库。 独立子包 utils/raft 实现固定成员加权共识,使用 bbolt/XXH3, 通过 opaque command、Apply 与 Call 接口接入业务,不导入文件系统或网络协议包。

导入路径:github.com/lfhy/compactfs/utils。

进程内有界任务池,供文件读写调度使用,只依赖 Go 标准库。独立算法进程由 codec/worker 管理。

  • NewPool(ctx, workers, queue) 创建固定并发数、有限队列容量的任务池。
  • SubmitContext 支持取消入队等待,已接收任务的所有权仍由池管理。
  • Wait 停止接收新任务并等待完成;Close 先取消任务上下文再等待。
  • 首个任务错误会取消池上下文,后续已入队任务仍会执行以释放资源。任务需要 处理取消并完成自己持有的缓冲区或其他资源的清理。
import "github.com/lfhy/compactfs/utils"

pool, err := utils.NewPool(ctx, 8, 128)
if err != nil {
    return err
}
defer pool.Close()
if err := pool.SubmitContext(ctx, func(jobCtx context.Context) error {
    return persist(jobCtx)
}); err != nil {
    return err
}
return pool.Wait()

本地持久化函数:

  • MkdirAll(path, mode):创建缺失目录,并同步新目录及第一个已有父目录, 保证完整目录链的目录项得到持久化。
  • SyncDir(path):同步目录项,供创建文件或原子重命名后建立持久化边界。

backend、元数据日志和原始写回缓存共享这两项工具;函数保留原有目录同步语义。

LockState(path) 在 Unix 上为 path + ".lock" 取得非阻塞进程间独占锁, 目录和新锁文件分别使用 0700/0600。锁被其他进程持有时返回错误;成功后必须调用 返回的 release 函数释放 FD,可以重复或并发释放。释放保留锁文件,后续所有者继续 锁同一 inode。其他平台明确返回不支持;调用方仍负责业务路径及符号链接校验。

FileBytes(file, size) 从 offset 0 借用已验证大小的不可变文件内容,不改变当前 seek offset。 Linux 优先只读 mmap,失败或其他系统复制。成功后必须调用返回的 release 函数一次; 之后不可再访问切片。借用期间可以关闭 FD 或移除文件名,不能修改/截断底层 inode。 chunk 编解码与历史校验共用此工具;这只是文件视图优化,不代表整个文件系统零复制。

Documentation

Overview

Package utils provides reusable utilities independent of compactfs storage.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FileBytes added in v1.0.2

func FileBytes(source *os.File, size int) ([]byte, func(), error)

FileBytes borrows size read-only bytes from an immutable file at offset zero, preserving its seek offset. Linux uses mmap, with a copied fallback. On success the caller must release the view exactly once and stop accessing it. The file may be closed/unlinked, but its inode must not be modified/truncated while the view is borrowed. The caller must validate the file size first.

func LockState added in v1.0.3

func LockState(path string) (func(), error)

LockState acquires a nonblocking process-shared exclusive lock on path+.lock. It owns the lock file descriptor until the returned idempotent release runs. The lock file stays in place so later owners lock the same inode.

func MkdirAll

func MkdirAll(name string, mode os.FileMode) error

MkdirAll creates a directory tree and syncs every new directory plus the first existing ancestor. Syncing a data file alone cannot persist the links of newly created parent directories.

func SyncDir

func SyncDir(name string) error

SyncDir persists directory entries after creation or atomic rename.

Types

type Job

type Job func(context.Context) error

type Pool

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

func NewPool

func NewPool(parent context.Context, workers, queue int) (*Pool, error)

func (*Pool) Close

func (p *Pool) Close() error

func (*Pool) Submit

func (p *Pool) Submit(job Job) error

func (*Pool) SubmitContext

func (p *Pool) SubmitContext(ctx context.Context, job Job) error

SubmitContext cancels queue admission without canceling jobs already owned by the pool. Local writeback must be able to cancel capacity backpressure.

func (*Pool) Wait

func (p *Pool) Wait() error

Directories

Path Synopsis
Package raft implements fixed-membership Raft with weighted votes and priority-biased election delays.
Package raft implements fixed-membership Raft with weighted votes and priority-biased election delays.

Jump to

Keyboard shortcuts

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