aboutsummaryrefslogtreecommitdiffstats
path: root/util
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-09-13 16:05:27 +0200
committerMichael Muré <batolettre@gmail.com>2018-09-13 16:05:27 +0200
commitbf11c08f5ad0894e494228c312235d674b58af6a (patch)
tree1f1b36341ed1e4d57a38d4f468aaac60001f45a4 /util
parentfb0f5530f100e8ae3b8561fe5dcfd19edc181b15 (diff)
downloadgit-bug-bf11c08f5ad0894e494228c312235d674b58af6a.tar.gz
lamport: better perf by ensuring that the folder is created only once
Diffstat (limited to 'util')
-rw-r--r--util/lamport/persisted_lamport.go17
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)
}