-
Notifications
You must be signed in to change notification settings - Fork 75
/
pexlist.go
85 lines (73 loc) · 1.94 KB
/
pexlist.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
package pexlist
import (
"net"
"strings"
"github.com/cenkalti/rain/internal/tracker"
)
const (
// BEP 11: Except for the initial PEX message the combined amount of added v4/v6 contacts should not exceed 50 entries.
// The same applies to dropped entries.
maxPeers = 50
)
// PEXList contains the list of peer address for sending them to a peer at certain interval.
// List contains 2 separate lists for added and dropped addresses.
type PEXList struct {
added map[tracker.CompactPeer]struct{}
dropped map[tracker.CompactPeer]struct{}
flushed bool
}
// New returns a new empty PEXList.
func New() *PEXList {
return &PEXList{
added: make(map[tracker.CompactPeer]struct{}),
dropped: make(map[tracker.CompactPeer]struct{}),
}
}
// NewWithRecentlySeen returns a new PEXList with given peers added to the dropped part.
func NewWithRecentlySeen(rs []tracker.CompactPeer) *PEXList {
l := New()
for _, cp := range rs {
l.dropped[cp] = struct{}{}
}
return l
}
// Add adds the address to the added part and removes from dropped part.
func (l *PEXList) Add(addr *net.TCPAddr) {
p := tracker.NewCompactPeer(addr)
l.added[p] = struct{}{}
delete(l.dropped, p)
}
// Drop adds the address to the dropped part and removes from added part.
func (l *PEXList) Drop(addr *net.TCPAddr) {
peer := tracker.NewCompactPeer(addr)
l.dropped[peer] = struct{}{}
delete(l.added, peer)
}
// Flush returns added and dropped parts and empty the list.
func (l *PEXList) Flush() (added, dropped string) {
added = l.flush(l.added, l.flushed)
dropped = l.flush(l.dropped, l.flushed)
l.flushed = true
return
}
func (l *PEXList) flush(m map[tracker.CompactPeer]struct{}, limit bool) string {
count := len(m)
if limit && count > maxPeers {
count = maxPeers
}
var s strings.Builder
s.Grow(count * 6)
for p := range m {
if count == 0 {
break
}
count--
b, err := p.MarshalBinary()
if err != nil {
panic(err)
}
s.Write(b)
delete(m, p)
}
return s.String()
}