Reachable hosts
This commit is contained in:
@@ -38,18 +38,18 @@ func NewIPRepository(ipv4client *HTTPClient, ipv6client *HTTPClient, testURL str
|
||||
return repo
|
||||
}
|
||||
|
||||
func (r *IPRepository) NetworkTest(forceRun bool) {
|
||||
ipv4latencyFile := "data/latency4.json"
|
||||
ipv6latencyFile := "data/latency6.json"
|
||||
func (r *IPRepository) NetworkTest(forceRun bool, persist bool) {
|
||||
ipv4HostsFile := "data/ipv4-hosts.json"
|
||||
ipv6HostsFile := "data/ipv6-hosts.json"
|
||||
if !forceRun {
|
||||
ipv4Loaded := false
|
||||
ipv6Loaded := false
|
||||
latencyData := r.readLatencyFile(ipv4latencyFile)
|
||||
latencyData := r.readLatencyFile(ipv4HostsFile)
|
||||
if latencyData != nil {
|
||||
r.ipv4latencyMap = *latencyData
|
||||
ipv4Loaded = true
|
||||
}
|
||||
latencyData = r.readLatencyFile(ipv6latencyFile)
|
||||
latencyData = r.readLatencyFile(ipv6HostsFile)
|
||||
if latencyData != nil {
|
||||
r.ipv6latencyMap = *latencyData
|
||||
ipv6Loaded = true
|
||||
@@ -57,19 +57,23 @@ func (r *IPRepository) NetworkTest(forceRun bool) {
|
||||
if ipv4Loaded && ipv6Loaded {
|
||||
return
|
||||
} else {
|
||||
r.log.Warn("Network test files not found")
|
||||
r.log.Warn("Network test files not found, running network test")
|
||||
}
|
||||
}
|
||||
|
||||
r.log.Info("Network test will start now. IGNORE THE WARNINGS!")
|
||||
r.log.Info("zurg will check for all reachable download servers. You can set 'cache_network_test_results: true' in your config to skip this test in the future.")
|
||||
r.log.Warn("IGNORE THE WARNINGS!")
|
||||
r.runLatencyTest()
|
||||
r.log.Info("Network test completed!")
|
||||
r.log.Infof("To rerun the network test, run 'zurg network-test', or delete the files %s and %s and run zurg again", ipv4latencyFile, ipv6latencyFile)
|
||||
r.writeLatencyFile(ipv4latencyFile, r.ipv4latencyMap)
|
||||
r.writeLatencyFile(ipv6latencyFile, r.ipv6latencyMap)
|
||||
|
||||
if persist {
|
||||
r.log.Infof("To rerun the network test, run 'zurg network-test', or delete the files %s and %s and run zurg again", ipv4HostsFile, ipv6HostsFile)
|
||||
r.writeLatencyFile(ipv4HostsFile, r.ipv4latencyMap)
|
||||
r.writeLatencyFile(ipv6HostsFile, r.ipv6latencyMap)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *IPRepository) GetOptimalHosts(numberOfHosts int, ipv6 bool) []string {
|
||||
func (r *IPRepository) GetHosts(numberOfHosts int, ipv6 bool) []string {
|
||||
latencyMap := r.ipv4latencyMap
|
||||
if ipv6 {
|
||||
latencyMap = r.ipv6latencyMap
|
||||
@@ -90,6 +94,9 @@ func (r *IPRepository) GetOptimalHosts(numberOfHosts int, ipv6 bool) []string {
|
||||
})
|
||||
|
||||
var optimalHosts []string
|
||||
if numberOfHosts == 0 {
|
||||
numberOfHosts = len(kvList)
|
||||
}
|
||||
for i := 0; i < numberOfHosts && i < len(kvList); i++ {
|
||||
optimalHosts = append(optimalHosts, kvList[i].Key)
|
||||
}
|
||||
@@ -161,7 +168,6 @@ func (r *IPRepository) runLatencyTest() {
|
||||
}
|
||||
|
||||
func (r *IPRepository) testDomainLatency(client *HTTPClient, domain string) (float64, error) {
|
||||
const testFileSize = 1 // byte
|
||||
const iterations = 3
|
||||
testURL := fmt.Sprintf("https://%s/speedtest/test.rar/%f", domain, rand.Float64())
|
||||
if r.testURL != "" {
|
||||
@@ -177,17 +183,13 @@ func (r *IPRepository) testDomainLatency(client *HTTPClient, domain string) (flo
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, testURL, nil)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodHead, testURL, nil)
|
||||
if err != nil {
|
||||
r.log.Warnf("Failed to create request for %s: %v", domain, err)
|
||||
retErr = err
|
||||
break
|
||||
}
|
||||
|
||||
headers := make(http.Header)
|
||||
headers.Set("Range", fmt.Sprintf("bytes=0-%d", testFileSize-1))
|
||||
req.Header = headers
|
||||
|
||||
start := time.Now()
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
@@ -195,23 +197,11 @@ func (r *IPRepository) testDomainLatency(client *HTTPClient, domain string) (flo
|
||||
retErr = err
|
||||
break
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusPartialContent {
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
r.log.Warnf("Failed to download from %s: %s", domain, resp.Status)
|
||||
retErr = fmt.Errorf("status code: %s", resp.Status)
|
||||
break
|
||||
}
|
||||
|
||||
limitedReader := io.LimitReader(resp.Body, testFileSize)
|
||||
|
||||
_, err = io.Copy(io.Discard, limitedReader)
|
||||
resp.Body.Close()
|
||||
|
||||
if err != nil && err != io.EOF {
|
||||
r.log.Warnf("Failed to read from %s: %v", domain, err)
|
||||
retErr = err
|
||||
break
|
||||
}
|
||||
|
||||
duration := time.Since(start).Seconds()
|
||||
totalDuration += duration
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user