diff options
author | Michael Muré <batolettre@gmail.com> | 2018-09-13 16:05:27 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2018-09-13 16:05:27 +0200 |
commit | bf11c08f5ad0894e494228c312235d674b58af6a (patch) | |
tree | 1f1b36341ed1e4d57a38d4f468aaac60001f45a4 /util/lamport | |
parent | fb0f5530f100e8ae3b8561fe5dcfd19edc181b15 (diff) | |
download | git-bug-bf11c08f5ad0894e494228c312235d674b58af6a.tar.gz |
lamport: better perf by ensuring that the folder is created only once
Diffstat (limited to 'util/lamport')
-rw-r--r-- | util/lamport/persisted_lamport.go | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/util/lamport/persisted_lamport.go b/util/lamport/persisted_lamport.go index 4f12dd1b..22b23dcb 100644 --- a/util/lamport/persisted_lamport.go +++ b/util/lamport/persisted_lamport.go @@ -12,12 +12,19 @@ type Persisted struct { filePath string } -func NewPersisted(filePath string) *Persisted { +func NewPersisted(filePath string) (*Persisted, error) { clock := &Persisted{ Clock: NewClock(), filePath: filePath, } - return clock + + dir := filepath.Dir(filePath) + err := os.MkdirAll(dir, 0777) + if err != nil { + return nil, err + } + + return clock, nil } func LoadPersisted(filePath string) (*Persisted, error) { @@ -67,12 +74,6 @@ func (c *Persisted) read() error { } func (c *Persisted) Write() error { - dir := filepath.Dir(c.filePath) - err := os.MkdirAll(dir, 0777) - if err != nil { - return err - } - data := []byte(fmt.Sprintf("%d", c.counter)) return ioutil.WriteFile(c.filePath, data, 0644) } |