base: github.com/grailbio/base/state Index | Files

package state

import "github.com/grailbio/base/state"

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

Index

Package Files

file.go

Variables

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

ErrNoState is returned when attempting to read a nonexistent state.

func Marshal Uses

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

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

func Unmarshal Uses

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

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

type File Uses

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 Uses

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 Uses

func (f *File) Close() error

Close releases resources associated with this State instance.

func (*File) Lock Uses

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 Uses

func (f *File) LockLocal()

LockLocal locks local access to state.

func (*File) Marshal Uses

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 Uses

func (f *File) Unlock() error

Unlock unlocks the state.

func (*File) UnlockLocal Uses

func (f *File) UnlockLocal()

UnlockLocal unlocks local access to state.

func (*File) Unmarshal Uses

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

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

Package state imports 7 packages (graph) and is imported by 5 packages. Updated 2018-08-13. Refresh now. Tools for package owners.