Skip to content
Merged
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
46 changes: 20 additions & 26 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,13 @@ func PutParam(path string, data []byte) error {
if err != nil {
return errors.Wrap(err, "could not fsync temp param file")
}
err = file.Close()
file = nil
if err != nil {
return errors.Wrap(err, "could not close temp param file")
}

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 +150,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 +161,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 +171,22 @@ func PutParam(path string, data []byte) error {
if err != nil {
return errors.Wrap(err, "could not open params directory")
}

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

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 +198,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 +209,21 @@ 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")
}

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

return nil
}
Loading