forked from fangj99/spider
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.go
More file actions
61 lines (55 loc) · 1.32 KB
/
Copy pathmonitor.go
File metadata and controls
61 lines (55 loc) · 1.32 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
package spider
import (
"log"
"os"
"time"
)
var (
countFindRequest int64
countFindResponse int64
countFindNode int64
countPing int64
countAnnounce int64
countGetPeers int64
)
var (
l = log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile)
)
//Monitor the network
func Monitor() {
var (
preCountGetPeers int64
)
for {
if len(hasFound) >= HasFoundSize {
mutex.Lock()
for k := range hasFound {
delete(hasFound, k)
}
hasFound = nil
hasFound = make(map[string]bool, HasFoundSize)
mutex.Unlock()
}
adjustFindFrequency(countGetPeers - preCountGetPeers)
preCountGetPeers = countGetPeers
logger("发出find_node请求数量", countFindRequest)
logger("收到find_node回复数量", countFindResponse)
logger("收到find_node请求数量", countFindNode)
logger("收到ping请求数量", countPing)
logger("收到announce_peer请求数量", countAnnounce)
logger("收到get_peers请求数量", preCountGetPeers)
logger("-------------------------------")
time.Sleep(time.Second * 60)
}
}
func adjustFindFrequency(count int64) {
//阶梯调整频率
//每分钟getpeer增加一万个,find sleep时间增加100ms
delay := count / 10000
if delay > 1 {
findDelayTime = time.Duration(50*delay) * time.Millisecond
}
}
func logger(v ...interface{}) {
l.Println(v)
}