Skip to content

Commit

Permalink
feat: properly save new syncmap version of frontier.
Browse files Browse the repository at this point in the history
I have briefly tested on my machine and it works.
  • Loading branch information
NGTmeaty committed Oct 17, 2023
1 parent f14ac37 commit 24611a4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 17 deletions.
42 changes: 25 additions & 17 deletions internal/pkg/frontier/save.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package frontier

import (
"encoding/gob"
"os"
"path"
"sync"

"github.com/sirupsen/logrus"
)
Expand All @@ -27,17 +25,15 @@ func (f *Frontier) Load() {
}
defer decodeFile.Close()

// Create a decoder
decoder := gob.NewDecoder(decodeFile)

// We create the structure to load the file's content
var dump = new(sync.Map)

// Decode the content of the file in the structure
decoder.Decode(&dump)

// Copy the loaded data to our actual frontier
f.HostPool = dump
if err := SyncMapDecode(f.HostPool, decodeFile); err != nil {
f.LoggingChan <- &FrontierLogMessage{
Fields: logrus.Fields{
"err": err.Error(),
},
Message: "unable to decode Frontier stats and host pool",
Level: logrus.WarnLevel,
}
}

f.LoggingChan <- &FrontierLogMessage{
Fields: logrus.Fields{
Expand All @@ -53,13 +49,25 @@ func (f *Frontier) Save() {
// Create a file for IO
encodeFile, err := os.OpenFile(path.Join(f.JobPath, "frontier.gob"), os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
logrus.Warning(err)
f.LoggingChan <- &FrontierLogMessage{
Fields: logrus.Fields{
"err": err.Error(),
},
Message: "unable to open Frontier file",
Level: logrus.WarnLevel,
}
}
defer encodeFile.Close()

// Write to the file
var encoder = gob.NewEncoder(encodeFile)
if err := encoder.Encode(f.HostPool); err != nil {
logrus.Warning(err)

if err := SyncMapEncode(f.HostPool, encodeFile); err != nil {
f.LoggingChan <- &FrontierLogMessage{
Fields: logrus.Fields{
"err": err.Error(),
},
Message: "unable to save Frontier stats and host pool",
Level: logrus.WarnLevel,
}
}
}
39 changes: 39 additions & 0 deletions internal/pkg/frontier/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package frontier

import (
"bufio"
"encoding/gob"
"errors"
"fmt"
"net/url"
"os"
"sync"

"github.com/gosuri/uilive"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -67,3 +69,40 @@ func IsSeedList(path string) (seeds []Item, err error) {

return seeds, nil
}

type Pair struct {
Key, Value interface{}
}

func SyncMapEncode(m *sync.Map, file *os.File) error {
var pairs []Pair

m.Range(func(key, value interface{}) bool {
pairs = append(pairs, Pair{key, value})
return true
})

gob.Register(PoolItem{})

enc := gob.NewEncoder(file)
err := enc.Encode(pairs)

return err
}

func SyncMapDecode(m *sync.Map, file *os.File) error {
var pairs []Pair
gob.Register(PoolItem{})
dec := gob.NewDecoder(file)
err := dec.Decode(&pairs)

if err != nil {
return err
}

for _, p := range pairs {
m.Store(p.Key, p.Value.(PoolItem))
}

return nil
}

0 comments on commit 24611a4

Please sign in to comment.