core/rawdb, node: use standalone flock dependency (#26633)

This commit is contained in:
Martin Holst Swende
2023-02-23 09:11:50 +02:00
committed by GitHub
parent a36c68f12c
commit 09a9ccdbce
4 changed files with 33 additions and 33 deletions
+14 -8
View File
@@ -30,7 +30,7 @@ import (
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"
"github.com/prometheus/tsdb/fileutil"
"github.com/gofrs/flock"
)
var (
@@ -75,7 +75,7 @@ type Freezer struct {
readonly bool
tables map[string]*freezerTable // Data tables for storing everything
instanceLock fileutil.Releaser // File-system lock to prevent double opens
instanceLock *flock.Flock // File-system lock to prevent double opens
closeOnce sync.Once
}
@@ -104,11 +104,17 @@ func NewFreezer(datadir string, namespace string, readonly bool, maxTableSize ui
return nil, errSymlinkDatadir
}
}
flockFile := filepath.Join(datadir, "FLOCK")
if err := os.MkdirAll(filepath.Dir(flockFile), 0755); err != nil {
return nil, err
}
// Leveldb uses LOCK as the filelock filename. To prevent the
// name collision, we use FLOCK as the lock name.
lock, _, err := fileutil.Flock(filepath.Join(datadir, "FLOCK"))
if err != nil {
lock := flock.New(flockFile)
if locked, err := lock.TryLock(); err != nil {
return nil, err
} else if !locked {
return nil, errors.New("locking failed")
}
// Open all the supported data tables
freezer := &Freezer{
@@ -124,12 +130,12 @@ func NewFreezer(datadir string, namespace string, readonly bool, maxTableSize ui
for _, table := range freezer.tables {
table.Close()
}
lock.Release()
lock.Unlock()
return nil, err
}
freezer.tables[name] = table
}
var err error
if freezer.readonly {
// In readonly mode only validate, don't truncate.
// validate also sets `freezer.frozen`.
@@ -142,7 +148,7 @@ func NewFreezer(datadir string, namespace string, readonly bool, maxTableSize ui
for _, table := range freezer.tables {
table.Close()
}
lock.Release()
lock.Unlock()
return nil, err
}
@@ -165,7 +171,7 @@ func (f *Freezer) Close() error {
errs = append(errs, err)
}
}
if err := f.instanceLock.Release(); err != nil {
if err := f.instanceLock.Unlock(); err != nil {
errs = append(errs, err)
}
})