Skip to content

Commit

Permalink
Merge pull request #67 from philchia/feature/atomic_create_dump
Browse files Browse the repository at this point in the history
create file atomic
  • Loading branch information
philchia committed May 7, 2020
2 parents 9d9ae9f + 5684bd1 commit b202199
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,22 @@ func (n *namespaceCache) dump(name string) error {
dumps[namespace] = cache.dump()
}

f, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
tmp := name + "tmp"
f, err := os.OpenFile(tmp, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return err
}
defer f.Close()

return gob.NewEncoder(f).Encode(&dumps)
if err := gob.NewEncoder(f).Encode(&dumps); err != nil {
_ = f.Close()
return err
}

if err := f.Close(); err != nil {
return err
}

return os.Rename(tmp, name)
}

func (n *namespaceCache) load(name string) error {
Expand Down

0 comments on commit b202199

Please sign in to comment.