-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathseed.go
63 lines (53 loc) · 1.15 KB
/
seed.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
package p2p
import (
"net"
"strings"
"time"
log "github.com/sirupsen/logrus"
)
type seed struct {
url string
cache []Endpoint
cacheTTL time.Duration
cacheTime time.Time
logger *log.Entry
}
func newSeed(url string, cacheTTL time.Duration) *seed {
s := new(seed)
s.url = url
s.logger = packageLogger.WithFields(log.Fields{"subpackage": "Seed", "url": url})
s.cacheTTL = cacheTTL
return s
}
func (s *seed) retrieve() []Endpoint {
if s.cache != nil && time.Since(s.cacheTime) <= s.cacheTTL {
return s.cache
}
eps := make([]Endpoint, 0)
err := WebScanner(s.url, func(line string) {
line = strings.TrimSpace(line)
if line == "" {
return
}
host, port, err := net.SplitHostPort(line)
if err != nil {
s.logger.Errorf("Badly formatted line [%s]", line)
return
}
if ep, err := NewEndpoint(host, port); err != nil {
s.logger.WithError(err).Errorf("Bad peer [%s]", line)
} else {
eps = append(eps, ep)
}
})
if err != nil {
s.logger.WithError(err).Errorf("unable to retrieve data from seed")
}
s.cacheTime = time.Now()
s.cache = eps
return eps
}
func (s *seed) size() int {
s.retrieve()
return len(s.cache)
}