lockfile

package
v0.22.6 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

lockfile package provides an interface for working with files that can be locked and unlocked.

Index

Constants

View Source
const (
	DefaultLockRetryInterval = 20 * time.Millisecond
)

Variables

View Source
var (
	ErrUnlocked    = errors.New("Lockfile has not been locked, I/O operations are not allowed")
	ErrNeedAbsPath = errors.New("Lockfiles must be created using absolute path")
)

Functions

This section is empty.

Types

type Lockfile

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

Represents a file that can be locked and unlocked. I/O operations are not allowed on an unlocked Lockfile. Lockfile is NOT goroutine-safe.

func NewLockfile

func NewLockfile(path string) (*Lockfile, error)

Creates a new Lockfile instance for given path. The actual file is not created or locked yet. The path must be an absolute path.

func (*Lockfile) Close

func (l *Lockfile) Close() error

func (*Lockfile) Locked

func (l *Lockfile) Locked() bool

func (*Lockfile) Path

func (l *Lockfile) Path() string

func (*Lockfile) Read

func (l *Lockfile) Read(p []byte) (int, error)

func (*Lockfile) Seek

func (l *Lockfile) Seek(offset int64, whence int) (int64, error)

func (*Lockfile) Truncate

func (l *Lockfile) Truncate(size int64) error

func (*Lockfile) TryLock

func (l *Lockfile) TryLock(ctx context.Context, retryInterval time.Duration) error

func (*Lockfile) Unlock

func (l *Lockfile) Unlock() error

func (*Lockfile) Write

func (l *Lockfile) Write(p []byte) (int, error)

type RecordFile

type RecordFile[R any] struct {
	Lockfile
	// contains filtered or unexported fields
}

A RecordFile is a Lockfile that stores a sequence of records. Each record occupies a single line in the file.

func NewRecordFile

func NewRecordFile[R any](path string, rw RecordMarshaller[R]) (*RecordFile[R], error)

func (*RecordFile[R]) TryLockAndRead

func (rf *RecordFile[R]) TryLockAndRead(ctx context.Context) ([]R, error)

Reads all records from the file. The file is left locked if the operation is successful. If an error occurs, the file is truncated and unlocked.

func (*RecordFile[R]) WriteAndUnlock

func (rf *RecordFile[R]) WriteAndUnlock(ctx context.Context, records []R) error

Writes the given records to the file, truncating the file first. The file is unlocked no matter if the operation is successful or not.

type RecordMarshaller

type RecordMarshaller[R any] interface {
	Unmarshal(line []byte) (R, error)
	Marshal(record R) []byte
}

Jump to

Keyboard shortcuts

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