mirror of
https://github.com/jeremyd/ergo.git
synced 2026-04-26 15:40:00 -07:00
optionally protect against multiple starts with flock (#1873)
* optionally protect against multiple starts with flock Fixes #1823 * use traditional .lock extension * move config key to top level
This commit is contained in:
committed by
GitHub
parent
e112a78b9b
commit
ed75533cb1
24
irc/flock/flock.go
Normal file
24
irc/flock/flock.go
Normal file
@@ -0,0 +1,24 @@
|
||||
//go:build !plan9
|
||||
|
||||
package flock
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/gofrs/flock"
|
||||
)
|
||||
|
||||
var (
|
||||
CouldntAcquire = errors.New("Couldn't acquire flock (is another Ergo running?)")
|
||||
)
|
||||
|
||||
func TryAcquireFlock(path string) (fl Flocker, err error) {
|
||||
f := flock.New(path)
|
||||
success, err := f.TryLock()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !success {
|
||||
return nil, CouldntAcquire
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
14
irc/flock/flock_iface.go
Normal file
14
irc/flock/flock_iface.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package flock
|
||||
|
||||
// documentation for github.com/gofrs/flock incorrectly claims that
|
||||
// Flock implements sync.Locker; it does not because the Unlock method
|
||||
// has a return type (err).
|
||||
type Flocker interface {
|
||||
Unlock() error
|
||||
}
|
||||
|
||||
type noopFlocker struct{}
|
||||
|
||||
func (n *noopFlocker) Unlock() error {
|
||||
return nil
|
||||
}
|
||||
7
irc/flock/flock_plan9.go
Normal file
7
irc/flock/flock_plan9.go
Normal file
@@ -0,0 +1,7 @@
|
||||
//go:build plan9
|
||||
|
||||
package flock
|
||||
|
||||
func TryAcquireFlock(path string) (fl Flocker, err error) {
|
||||
return &noopFlocker{}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user