Back to

Package state

Latest Go to latest
Published: Jun 24, 2020 | License: Apache-2.0 | Module:


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


Package Files


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

ErrNoState is returned when attempting to read a nonexistent state.

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.

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.

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier