Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 29 additions & 24 deletions params/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ func PutParam(path string, data []byte) error {
if err != nil {
return errors.Wrap(err, "could not create temp param file")
}
defer func() {
if file != nil {
file.Close()
}
}()
tmpName := file.Name()
defer os.Remove(tmpName)

Expand All @@ -127,8 +132,12 @@ func PutParam(path string, data []byte) error {
if err != nil {
return errors.Wrap(err, "could not fsync temp param file")
}
if err = file.Close(); err != nil {
return errors.Wrap(err, "could not close temp param file")
}
file = nil

fileLock := flock.New(filepath.Join(lock_dir, ".lock"))
fileLock := flock.New(filepath.Join(lock_dir, ".lock"), flock.SetPermissions(0o775))

retries := 0
for {
Expand All @@ -140,12 +149,6 @@ func PutParam(path string, data []byte) error {
break
}
retries += 1
if retries > 30 {
// try to force the lock to be removed
if err := os.Remove(filepath.Join(lock_dir, ".lock")); err != nil {
slog.Debug("failed to force delete params lock", "error", err)
}
}
if retries > 50 {
return errors.New("could not obtain lock")
}
Expand All @@ -157,11 +160,6 @@ func PutParam(path string, data []byte) error {
slog.Error("could not unlock params directory", "error", err)
}
}()
defer func() {
if err := os.Remove(filepath.Join(lock_dir, ".lock")); err != nil {
slog.Error("could not remove params lock file", "error", err)
}
}()

err = os.Rename(tmpName, path)
if err != nil {
Expand All @@ -172,19 +170,28 @@ func PutParam(path string, data []byte) error {
if err != nil {
return errors.Wrap(err, "could not open params directory")
}
defer func() {
if directory != nil {
directory.Close()
}
}()

err = directory.Sync()
if err != nil {
return errors.Wrap(err, "could not fsync params directory")
}
if err = directory.Close(); err != nil {
return errors.Wrap(err, "could not close params directory")
}
directory = nil

return nil
}

func RemoveParam(path string) error {
dir := filepath.Dir(path)
lock_dir := filepath.Dir(dir)
fileLock := flock.New(filepath.Join(lock_dir, ".lock"))
fileLock := flock.New(filepath.Join(lock_dir, ".lock"), flock.SetPermissions(0o775))

retries := 0
for {
Expand All @@ -196,12 +203,6 @@ func RemoveParam(path string) error {
break
}
retries += 1
if retries > 30 {
// try to force the lock to be removed
if err := os.Remove(filepath.Join(lock_dir, ".lock")); err != nil {
slog.Debug("failed to force delete params lock", "error", err)
}
}
if retries > 50 {
return errors.New("could not obtain lock")
}
Expand All @@ -213,23 +214,27 @@ func RemoveParam(path string) error {
slog.Error("could not unlock params directory", "error", err)
}
}()
defer func() {
if err := os.Remove(filepath.Join(lock_dir, ".lock")); err != nil {
slog.Error("could not remove params lock file", "error", err)
}
}()

os.Remove(path)

directory, err := os.Open(dir)
if err != nil {
return errors.Wrap(err, "could not open params directory")
}
defer func() {
if directory != nil {
directory.Close()
}
}()

err = directory.Sync()
if err != nil {
return errors.Wrap(err, "could not fsync params directory")
}
if err = directory.Close(); err != nil {
return errors.Wrap(err, "could not close params directory")
}
directory = nil

return nil
}
Loading