You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
regolancer/cache.go

78 lines
1.5 KiB
Go

2 years ago
package main
import (
"encoding/gob"
"fmt"
2 years ago
"log"
"os"
"path/filepath"
"time"
"github.com/gofrs/flock"
"github.com/lightningnetwork/lnd/lnrpc"
2 years ago
)
func lock() *flock.Flock {
return flock.New(filepath.Join(os.TempDir(), "regolancer.lock"))
}
func (r *regolancer) loadNodeCache(filename string, exp int, doLock bool) error {
2 years ago
if filename == "" {
return nil
}
if doLock {
log.Printf("Loading node cache from %s", filename)
l := lock()
l.RLock()
defer l.Unlock()
2 years ago
}
f, err := os.Open(filename)
if err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("error opening node cache file: %s", err)
2 years ago
}
return nil
2 years ago
}
defer f.Close()
gob.NewDecoder(f).Decode(&r.nodeCache)
for k, v := range r.nodeCache {
if time.Since(time.Unix(int64(v.Node.LastUpdate), 0)) >
time.Minute*time.Duration(exp) {
delete(r.nodeCache, k)
}
}
return nil
2 years ago
}
func (r *regolancer) saveNodeCache(filename string, exp int) error {
2 years ago
if filename == "" {
return nil
2 years ago
}
log.Printf("Saving node cache to %s", filename)
2 years ago
l := lock()
l.Lock()
defer l.Unlock()
old := regolancer{nodeCache: map[string]*lnrpc.NodeInfo{}}
err := old.loadNodeCache(filename, exp, false)
if err != nil {
logErrorF("Error merging cache, saving anew: %s", err)
}
for k, v := range old.nodeCache {
if n, ok := r.nodeCache[k]; !ok ||
n.Node.LastUpdate < v.Node.LastUpdate {
r.nodeCache[k] = v
}
}
2 years ago
f, err := os.Create(filename)
if err != nil {
return fmt.Errorf("error creating node cache file: %s", err)
2 years ago
}
defer f.Close()
gob.NewEncoder(f).Encode(r.nodeCache)
return nil
2 years ago
}