Allow custom url

This commit is contained in:
Ben Adrian Sarmiento
2024-06-16 21:11:40 +02:00
parent e5bb026f87
commit f2331c6a2e
4 changed files with 20 additions and 7 deletions

View File

@@ -8,6 +8,7 @@ import (
"math/rand"
"net"
"net/http"
"net/url"
"os"
"sort"
"time"
@@ -20,15 +21,17 @@ type IPRepository struct {
ipv6client *HTTPClient
ipv4latencyMap map[string]float64
ipv6latencyMap map[string]float64
testURL string
log *logutil.Logger
}
func NewIPRepository(ipv4client *HTTPClient, ipv6client *HTTPClient, log *logutil.Logger) *IPRepository {
func NewIPRepository(ipv4client *HTTPClient, ipv6client *HTTPClient, testURL string, log *logutil.Logger) *IPRepository {
repo := &IPRepository{
ipv4client: ipv4client,
ipv6client: ipv6client,
ipv4latencyMap: make(map[string]float64),
ipv6latencyMap: make(map[string]float64),
testURL: testURL,
log: log,
}
@@ -169,7 +172,13 @@ func (r *IPRepository) runLatencyTest() {
func (r *IPRepository) testDomainLatency(client *HTTPClient, domain string) (float64, error) {
const testFileSize = 1 // byte
const iterations = 3
url := fmt.Sprintf("https://%s/speedtest/test.rar/%f", domain, rand.Float64())
testURL := fmt.Sprintf("https://%s/speedtest/test.rar/%f", domain, rand.Float64())
if r.testURL != "" {
parsedURL, err := url.Parse(r.testURL)
if err == nil {
testURL = fmt.Sprintf("https://%s%s", domain, parsedURL.Path)
}
}
var totalDuration float64
var retErr error
@@ -177,7 +186,7 @@ func (r *IPRepository) testDomainLatency(client *HTTPClient, domain string) (flo
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, testURL, nil)
if err != nil {
r.log.Warnf("Failed to create request for %s: %v", domain, err)
retErr = err