-
Notifications
You must be signed in to change notification settings - Fork 212
/
persist.go
106 lines (98 loc) · 2.29 KB
/
persist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package p2p
import (
"context"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"hash/crc64"
"io"
"os"
"path/filepath"
"time"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/natefinch/atomic"
"github.com/spacemeshos/go-spacemesh/log"
)
const connectedFile = "connected.txt"
func persist(ctx context.Context, logger log.Log, h host.Host, dir string, period time.Duration) {
ticker := time.NewTicker(period)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
if err := writePeers(h, dir); err != nil {
logger.With().Warning("failed to write connected peers to file",
log.String("directory", dir),
log.Err(err),
)
}
}
}
}
func writePeers(h host.Host, dir string) error {
peers := h.Network().Peers()
if len(peers) == 0 {
return nil
}
checksum := crc64.New(crc64.MakeTable(crc64.ISO))
tmp, err := os.CreateTemp(dir, "connected.tmp")
if err != nil {
return err
}
crc := make([]byte, crc64.Size)
if _, err := tmp.Write(crc); err != nil {
tmp.Close()
return err
}
codec := json.NewEncoder(io.MultiWriter(tmp, checksum))
for _, pid := range peers {
info := h.Peerstore().PeerInfo(pid)
if err := codec.Encode(info); err != nil {
tmp.Close()
return err
}
}
binary.BigEndian.PutUint64(crc, checksum.Sum64())
if _, err = tmp.WriteAt(crc, 0); err != nil {
tmp.Close()
return err
}
tmp.Close()
return atomic.ReplaceFile(tmp.Name(), filepath.Join(dir, connectedFile))
}
func loadPeers(dir string) ([]peer.AddrInfo, error) {
f, err := os.Open(filepath.Join(dir, connectedFile))
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return nil, nil
}
return nil, err
}
defer f.Close()
crc := make([]byte, crc64.Size)
if _, err := f.Read(crc); err != nil {
return nil, err
}
checksum := crc64.New(crc64.MakeTable(crc64.ISO))
codec := json.NewDecoder(io.TeeReader(f, checksum))
rst := []peer.AddrInfo{}
for {
var info peer.AddrInfo
if err := codec.Decode(&info); err != nil {
if errors.Is(err, io.EOF) {
if saved := binary.BigEndian.Uint64(crc); saved != checksum.Sum64() {
return nil, fmt.Errorf(
"invalid checksum %d != %d", saved, checksum.Sum64())
}
return rst, nil
} else {
return nil, err
}
}
rst = append(rst, info)
}
}