state: rename simple store implementation
Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
This commit is contained in:
parent
5d25bee786
commit
f796622c35
@ -11,8 +11,8 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
// file-based locking
|
// fine-grained locking and access
|
||||||
type simpleStore struct {
|
type multiStore struct {
|
||||||
path []string
|
path []string
|
||||||
|
|
||||||
// created/opened by prepare
|
// created/opened by prepare
|
||||||
@ -25,7 +25,7 @@ type simpleStore struct {
|
|||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *simpleStore) Do(f func(b Backend)) (bool, error) {
|
func (s *multiStore) Do(f func(b Backend)) (bool, error) {
|
||||||
s.init.Do(s.prepare)
|
s.init.Do(s.prepare)
|
||||||
if s.initErr != nil {
|
if s.initErr != nil {
|
||||||
return false, s.initErr
|
return false, s.initErr
|
||||||
@ -40,7 +40,7 @@ func (s *simpleStore) Do(f func(b Backend)) (bool, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// initialise new backend for caller
|
// initialise new backend for caller
|
||||||
b := new(simpleBackend)
|
b := new(multiBackend)
|
||||||
b.path = path.Join(s.path...)
|
b.path = path.Join(s.path...)
|
||||||
f(b)
|
f(b)
|
||||||
// disable backend
|
// disable backend
|
||||||
@ -50,7 +50,7 @@ func (s *simpleStore) Do(f func(b Backend)) (bool, error) {
|
|||||||
return true, s.unlockFile()
|
return true, s.unlockFile()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *simpleStore) lockFileAct(lt int) (err error) {
|
func (s *multiStore) lockFileAct(lt int) (err error) {
|
||||||
op := "LockAct"
|
op := "LockAct"
|
||||||
switch lt {
|
switch lt {
|
||||||
case syscall.LOCK_EX:
|
case syscall.LOCK_EX:
|
||||||
@ -75,15 +75,15 @@ func (s *simpleStore) lockFileAct(lt int) (err error) {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *simpleStore) lockFile() error {
|
func (s *multiStore) lockFile() error {
|
||||||
return s.lockFileAct(syscall.LOCK_EX)
|
return s.lockFileAct(syscall.LOCK_EX)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *simpleStore) unlockFile() error {
|
func (s *multiStore) unlockFile() error {
|
||||||
return s.lockFileAct(syscall.LOCK_UN)
|
return s.lockFileAct(syscall.LOCK_UN)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *simpleStore) prepare() {
|
func (s *multiStore) prepare() {
|
||||||
s.initErr = func() error {
|
s.initErr = func() error {
|
||||||
prefix := path.Join(s.path...)
|
prefix := path.Join(s.path...)
|
||||||
// ensure directory
|
// ensure directory
|
||||||
@ -102,7 +102,7 @@ func (s *simpleStore) prepare() {
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *simpleStore) Close() error {
|
func (s *multiStore) Close() error {
|
||||||
s.lock.Lock()
|
s.lock.Lock()
|
||||||
defer s.lock.Unlock()
|
defer s.lock.Unlock()
|
||||||
|
|
||||||
@ -113,19 +113,19 @@ func (s *simpleStore) Close() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
type simpleBackend struct {
|
type multiBackend struct {
|
||||||
path string
|
path string
|
||||||
|
|
||||||
lock sync.RWMutex
|
lock sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *simpleBackend) filename(pid int) string {
|
func (b *multiBackend) filename(pid int) string {
|
||||||
return path.Join(b.path, strconv.Itoa(pid))
|
return path.Join(b.path, strconv.Itoa(pid))
|
||||||
}
|
}
|
||||||
|
|
||||||
// reads all launchers in simpleBackend
|
// reads all launchers in simpleBackend
|
||||||
// file contents are ignored if decode is false
|
// file contents are ignored if decode is false
|
||||||
func (b *simpleBackend) load(decode bool) ([]*State, error) {
|
func (b *multiBackend) load(decode bool) ([]*State, error) {
|
||||||
b.lock.RLock()
|
b.lock.RLock()
|
||||||
defer b.lock.RUnlock()
|
defer b.lock.RUnlock()
|
||||||
|
|
||||||
@ -172,7 +172,7 @@ func (b *simpleBackend) load(decode bool) ([]*State, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Save writes process state to filesystem
|
// Save writes process state to filesystem
|
||||||
func (b *simpleBackend) Save(state *State) error {
|
func (b *multiBackend) Save(state *State) error {
|
||||||
b.lock.Lock()
|
b.lock.Lock()
|
||||||
defer b.lock.Unlock()
|
defer b.lock.Unlock()
|
||||||
|
|
||||||
@ -197,18 +197,18 @@ func (b *simpleBackend) Save(state *State) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *simpleBackend) Destroy(pid int) error {
|
func (b *multiBackend) Destroy(pid int) error {
|
||||||
b.lock.Lock()
|
b.lock.Lock()
|
||||||
defer b.lock.Unlock()
|
defer b.lock.Unlock()
|
||||||
|
|
||||||
return os.Remove(b.filename(pid))
|
return os.Remove(b.filename(pid))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *simpleBackend) Load() ([]*State, error) {
|
func (b *multiBackend) Load() ([]*State, error) {
|
||||||
return b.load(true)
|
return b.load(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *simpleBackend) Len() (int, error) {
|
func (b *multiBackend) Len() (int, error) {
|
||||||
// rn consists of only nil entries but has the correct length
|
// rn consists of only nil entries but has the correct length
|
||||||
rn, err := b.load(false)
|
rn, err := b.load(false)
|
||||||
return len(rn), err
|
return len(rn), err
|
||||||
@ -216,7 +216,7 @@ func (b *simpleBackend) Len() (int, error) {
|
|||||||
|
|
||||||
// NewSimple returns an instance of a file-based store.
|
// NewSimple returns an instance of a file-based store.
|
||||||
func NewSimple(runDir string, prefix ...string) Store {
|
func NewSimple(runDir string, prefix ...string) Store {
|
||||||
b := new(simpleStore)
|
b := new(multiStore)
|
||||||
b.path = append([]string{runDir, "state"}, prefix...)
|
b.path = append([]string{runDir, "state"}, prefix...)
|
||||||
return b
|
return b
|
||||||
}
|
}
|
@ -37,7 +37,7 @@ func MustPrintLauncherStateSimpleGlobal(w **tabwriter.Writer, runDir string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// obtain temporary store
|
// obtain temporary store
|
||||||
s := NewSimple(runDir, e.Name()).(*simpleStore)
|
s := NewSimple(runDir, e.Name()).(*multiStore)
|
||||||
|
|
||||||
// print states belonging to this store
|
// print states belonging to this store
|
||||||
s.mustPrintLauncherState(w, now)
|
s.mustPrintLauncherState(w, now)
|
||||||
@ -50,7 +50,7 @@ func MustPrintLauncherStateSimpleGlobal(w **tabwriter.Writer, runDir string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *simpleStore) mustPrintLauncherState(w **tabwriter.Writer, now time.Time) {
|
func (s *multiStore) mustPrintLauncherState(w **tabwriter.Writer, now time.Time) {
|
||||||
var innerErr error
|
var innerErr error
|
||||||
|
|
||||||
if ok, err := s.Do(func(b Backend) {
|
if ok, err := s.Do(func(b Backend) {
|
||||||
|
Loading…
Reference in New Issue
Block a user