forked from cenkalti/rain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peeraddr.go
45 lines (34 loc) 路 791 Bytes
/
peeraddr.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
package addrlist
import (
"net"
"sort"
"time"
"github.com/google/btree"
"github.com/kyoto44/rain/peerpriority"
"github.com/kyoto44/rain/peersource"
)
type peerAddr struct {
addr *net.TCPAddr
timestamp time.Time
source peersource.Source
priority peerpriority.Priority
// index in AddrList.peerByTime slice
index int
}
var _ btree.Item = (*peerAddr)(nil)
func (p *peerAddr) Less(than btree.Item) bool {
return p.priority < than.(*peerAddr).priority
}
type byTimestamp []*peerAddr
var _ sort.Interface = (byTimestamp)(nil)
func (a byTimestamp) Len() int {
return len(a)
}
func (a byTimestamp) Less(i, j int) bool {
return a[i].timestamp.Before(a[j].timestamp)
}
func (a byTimestamp) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
a[i].index = i
a[j].index = j
}