-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpeer_cache.go
More file actions
150 lines (130 loc) · 2.73 KB
/
peer_cache.go
File metadata and controls
150 lines (130 loc) · 2.73 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package linker
import (
"context"
"fmt"
"sync"
"github.com/glvd/go-linker/config"
core "github.com/ipfs/go-ipfs/core"
"github.com/libp2p/go-libp2p-core/peer"
)
const peerName = "address"
type PeerCache struct {
lock *sync.RWMutex
addresses map[peer.ID]peer.AddrInfo
cache Cacher
}
func peerCache() *PeerCache {
return &PeerCache{
lock: &sync.RWMutex{},
addresses: make(map[peer.ID]peer.AddrInfo),
}
}
func NewAddress(cfg *config.Config, node *core.IpfsNode) *PeerCache {
cache := peerCache()
log.Info("peer cache initialized")
cache.cache = NewCache(cfg.Address, cfg.Repo, peerName)
return cache
}
func (c *PeerCache) CheckPeerAddress(id peer.ID) (b bool) {
c.lock.RLock()
_, b = c.addresses[id]
c.lock.RUnlock()
return
}
func (c *PeerCache) AddPeerAddress(addr peer.AddrInfo) (b bool) {
c.lock.RLock()
_, b = c.addresses[addr.ID]
c.lock.RUnlock()
if b {
return !b
}
c.lock.Lock()
_, b = c.addresses[addr.ID]
if !b {
c.addresses[addr.ID] = addr
}
c.lock.Unlock()
return !b
}
func (c *PeerCache) GetAddress(id peer.ID) (ai peer.AddrInfo, b bool) {
c.lock.RLock()
ai, b = c.addresses[id]
c.lock.RUnlock()
return ai, b
}
func (c *PeerCache) Peers() (ids []peer.ID) {
c.lock.RLock()
for id := range c.addresses {
ids = append(ids, id)
}
c.lock.RUnlock()
return
}
func (c *PeerCache) UpdatePeerAddress(new peer.AddrInfo) bool {
address, b := c.GetAddress(new.ID)
if !b {
return c.AddPeerAddress(new)
}
updated := false
newAddr := address
mark := make(map[fmt.Stringer]bool)
for _, addr := range address.Addrs {
mark[addr] = true
}
for _, addr := range new.Addrs {
if !mark[addr] {
updated = true
newAddr.Addrs = append(newAddr.Addrs, addr)
}
}
if !updated {
return updated
}
c.lock.Lock()
c.addresses[new.ID] = newAddr
c.lock.Unlock()
return updated
}
func (c *PeerCache) LoadAddress(ctx context.Context) (<-chan peer.AddrInfo, error) {
ai := make(chan peer.AddrInfo)
go func() {
defer close(ai)
c.cache.Range(func(hash string, value string) bool {
log.Infow("per cache ranging", "hash", hash, "value", value)
var info peer.AddrInfo
err := info.UnmarshalJSON([]byte(value))
if err != nil {
log.Errorw("unmarshal addr info failed in range", "err", err)
return false
}
select {
case <-ctx.Done():
return false
case ai <- info:
return true
}
})
}()
return ai, nil
}
// SaveNode ...
func (c *PeerCache) SaveAddress(ctx context.Context) (err error) {
go func() {
for _, id := range c.Peers() {
select {
case <-ctx.Done():
return
default:
address, b := c.GetAddress(id)
if !b {
continue
}
err := c.cache.Store(id.Pretty(), address)
if err != nil {
return
}
}
}
}()
return nil
}