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

Properly save sync.map version of Frontier #57

Merged
merged 1 commit into from
Oct 25, 2023
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
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
}
Loading