state

package
v0.0.0-...-44c758a Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package state implements atomic file-based state management with support for advisory locking.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoState = errors.New("no state exists")

ErrNoState is returned when attempting to read a nonexistent state.

Functions

func Marshal

func Marshal(prefix string, v interface{}) error

Marshal opens a State, marshals v into it, and then closes it.

func Unmarshal

func Unmarshal(prefix string, v interface{}) error

Unmarshal opens a State, unmarshals it into v, and then closes it.

Types

type File

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

File implements file-based state management with support for advisory locking. It is also safe to use concurrently within a process.

func Open

func Open(prefix string) (*File, error)

New creates and returns a new State at the given prefix. The following files are stored:

  • {prefix}.json: the current state
  • {prefix}.lock: the POSIX lock file
  • {prefix}.bak: the previous state

func (*File) Close

func (f *File) Close() error

Close releases resources associated with this State instance.

func (*File) Lock

func (f *File) Lock() error

Lock locks the state, both inside of the process and outside. Lock relies on POSIX flock, which may not be available on all filesystems, notably NFS and SMB.

func (*File) LockLocal

func (f *File) LockLocal()

LockLocal locks local access to state.

func (*File) Marshal

func (f *File) Marshal(v interface{}) error

Marshal atomically stores the JSON-encoded representation of v to the current state. It is only stored when Marshal returns a nil error.

func (*File) Unlock

func (f *File) Unlock() error

Unlock unlocks the state.

func (*File) UnlockLocal

func (f *File) UnlockLocal()

UnlockLocal unlocks local access to state.

func (*File) Unmarshal

func (f *File) Unmarshal(v interface{}) error

Unmarshal decodes the current state into v. Unmarshal returns ErrNoState if no state is stored.

Jump to

Keyboard shortcuts

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