Documentation
¶
Overview ¶
lockfile package provides an interface for working with files that can be locked and unlocked.
Index ¶
- Constants
- Variables
- type Lockfile
- func (l *Lockfile) Close() error
- func (l *Lockfile) Locked() bool
- func (l *Lockfile) Path() string
- func (l *Lockfile) Read(p []byte) (int, error)
- func (l *Lockfile) Seek(offset int64, whence int) (int64, error)
- func (l *Lockfile) Truncate(size int64) error
- func (l *Lockfile) TryLock(ctx context.Context, retryInterval time.Duration) error
- func (l *Lockfile) Unlock() error
- func (l *Lockfile) Write(p []byte) (int, error)
- type RecordFile
- type RecordMarshaller
Constants ¶
const (
DefaultLockRetryInterval = 20 * time.Millisecond
)
Variables ¶
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 ¶
Creates a new Lockfile instance for given path. The actual file is not created or locked yet. The path must be an absolute path.
type RecordFile ¶
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.