Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add exclusive lock when writing to Statfile #42

Merged
merged 2 commits into from
Dec 17, 2022
Merged
Show file tree
Hide file tree
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
14 changes: 11 additions & 3 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ func (r *regolancer) loadNodeCache(filename string, exp int, doLock bool) error
if doLock {
log.Printf("Loading node cache from %s", filename)
l := lock()
l.RLock()
err := l.RLock()
defer l.Unlock()

if err != nil {
return fmt.Errorf("error taking shared lock on file %s: %s", filename, err)
}
}
f, err := os.Open(filename)
if err != nil {
Expand Down Expand Up @@ -61,11 +65,15 @@ func (r *regolancer) saveNodeCache(filename string, exp int) error {
log.Printf("Saving node cache to %s", filename)

l := lock()
l.Lock()
err := l.Lock()
defer l.Unlock()

if err != nil {
return fmt.Errorf("error taking exclusive lock on file %s: %s", filename, err)
}

old := regolancer{nodeCache: map[string]cachedNodeInfo{}}
err := old.loadNodeCache(filename, exp, false)
err = old.loadNodeCache(filename, exp, false)

if err != nil {
logErrorF("Error merging cache, saving anew: %s", err)
Expand Down
11 changes: 10 additions & 1 deletion payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,16 @@ func (r *regolancer) pay(ctx context.Context, amount int64, minAmount int64,
log.Printf("Success! Paid %s in fees, %s ppm",
formatFee(result.Route.TotalFeesMsat), formatFeePPM(result.Route.TotalAmtMsat, result.Route.TotalFeesMsat))
if r.statFilename != "" {
_, err := os.Stat(r.statFilename)

l := lock()
err := l.Lock()
defer l.Unlock()

if err != nil {
return fmt.Errorf("error taking exclusive lock on file %s: %s", r.statFilename, err)
}

_, err = os.Stat(r.statFilename)
f, ferr := os.OpenFile(r.statFilename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if ferr != nil {
logErrorF("Error saving rebalance stats to %s: %s", r.statFilename, ferr)
Expand Down