forked from google/namebench
-
Notifications
You must be signed in to change notification settings - Fork 37
/
nsstore.go
90 lines (83 loc) · 1.92 KB
/
nsstore.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
86
87
88
89
90
package main
import (
"sync"
"time"
)
type NInfo struct {
IPAddr string
Name string
Country string
Count int
ErrorsConnection int
ErrorsValidation int
ID int64
rtt []time.Duration
rttAvg time.Duration
rttMin time.Duration
rttMax time.Duration
}
type nsInfoMap struct {
ns map[string]NInfo
mutex sync.RWMutex
}
// Get IP address entry // DEBUG
func nsStoreGetRecord(nsStore *nsInfoMap, ipAddr string) NInfo {
nsStore.mutex.RLock()
defer nsStore.mutex.RUnlock()
entry, found := nsStore.ns[ipAddr]
if !found {
entry.IPAddr = ipAddr
}
return entry
}
// Get nameserver average time
func nsStoreGetMeasurement(nsStore *nsInfoMap, ipAddr string) NInfo {
var nsMeasurement = NInfo{}
entry, found := nsStore.ns[ipAddr]
if !found {
entry.IPAddr = ipAddr
}
var total time.Duration = 0
var min time.Duration = 10000000
var max time.Duration = 0
for _, value := range entry.rtt {
// check for new min record
if value < min {
min = value
}
// check for new max record
if value > max {
max = value
}
// add for total time
total += value
}
nsMeasurement.rttAvg = total / time.Duration(len(entry.rtt))
nsMeasurement.rttMin = min
nsMeasurement.rttMax = max
return nsMeasurement
}
// add rtt to the nameserver slice
func nsStoreSetRTT(nsStore *nsInfoMap, ipAddr string, rtt time.Duration) {
nsStore.mutex.Lock()
defer nsStore.mutex.Unlock()
entry, found := nsStore.ns[ipAddr]
if !found {
entry.IPAddr = ipAddr
}
entry.rtt = append(entry.rtt, rtt)
entry.Count++
nsStore.ns[ipAddr] = entry
}
// add rtt to the nameserver slice
func nsStoreAddNS(nsStore *nsInfoMap, ipAddr string, name string, country string) {
nsStore.mutex.Lock()
defer nsStore.mutex.Unlock()
entry, found := nsStore.ns[ipAddr]
if !found {
entry.IPAddr = ipAddr
}
entry.Name = name
entry.Country = country
nsStore.ns[ipAddr] = entry
}