mirror of
https://github.com/nxtrace/NTrace-core.git
synced 2026-12-03 01:34:44 +00:00
refactor(runtime): 采用 Go 1.27 标准库并发与集合接口
This commit is contained in:
1 parent
112932e546
commit
83d906bb31
9 files changed
+78
-31
No files matched your search
@@ -3,7 +3,7 @@ package latency
|
||||
import (
|
||||
"context"
|
||||
"math"
|
||||
"sort"
|
||||
"slices"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@@ -131,7 +131,7 @@ func Compute(samples []float64) Stats {
|
||||
}
|
||||
|
||||
sorted := append([]float64(nil), samples...)
|
||||
sort.Float64s(sorted)
|
||||
slices.Sort(sorted)
|
||||
minV := sorted[0]
|
||||
maxV := sorted[n-1]
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package runner
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"cmp"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
@@ -9,7 +10,7 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"sort"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -302,16 +303,7 @@ func discoverCandidates(ctx context.Context, cfg *speedconfig.Config, p provider
|
||||
seen[key] = true
|
||||
out.Candidates = append(out.Candidates, buildCandidate(ctx, cfg, p, host, key, "dns"))
|
||||
}
|
||||
sort.SliceStable(out.Candidates, func(i, j int) bool {
|
||||
li, lj := out.Candidates[i], out.Candidates[j]
|
||||
if li.Status == "ok" && lj.Status != "ok" {
|
||||
return true
|
||||
}
|
||||
if li.Status != "ok" && lj.Status == "ok" {
|
||||
return false
|
||||
}
|
||||
return li.RTTMs < lj.RTTMs
|
||||
})
|
||||
sortCandidates(out.Candidates)
|
||||
for _, cand := range out.Candidates {
|
||||
if cand.Status == "ok" {
|
||||
out.Selected = cand
|
||||
@@ -328,6 +320,18 @@ func discoverCandidates(ctx context.Context, cfg *speedconfig.Config, p provider
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func sortCandidates(candidates []candidate) {
|
||||
slices.SortStableFunc(candidates, func(a, b candidate) int {
|
||||
if a.Status == "ok" && b.Status != "ok" {
|
||||
return -1
|
||||
}
|
||||
if a.Status != "ok" && b.Status == "ok" {
|
||||
return 1
|
||||
}
|
||||
return cmp.Compare(a.RTTMs, b.RTTMs)
|
||||
})
|
||||
}
|
||||
|
||||
func buildCandidate(ctx context.Context, cfg *speedconfig.Config, p provider.Provider, host, ip, source string) candidate {
|
||||
cand := candidate{IP: ip, Source: source, Status: "degraded"}
|
||||
if !cfg.NoMetadata {
|
||||
|
||||
@@ -19,6 +19,24 @@ import (
|
||||
"github.com/nxtrace/NTrace-core/internal/speedtest/result"
|
||||
)
|
||||
|
||||
func TestSortCandidatesKeepsHealthyRTTAndStableOrder(t *testing.T) {
|
||||
candidates := []candidate{
|
||||
{IP: "192.0.2.1", Status: "degraded", RTTMs: 20},
|
||||
{IP: "192.0.2.2", Status: "ok", RTTMs: 10},
|
||||
{IP: "192.0.2.3", Status: "ok", RTTMs: 10},
|
||||
{IP: "192.0.2.4", Status: "degraded", RTTMs: 5},
|
||||
}
|
||||
|
||||
sortCandidates(candidates)
|
||||
|
||||
want := []string{"192.0.2.2", "192.0.2.3", "192.0.2.4", "192.0.2.1"}
|
||||
for i, ip := range want {
|
||||
if candidates[i].IP != ip {
|
||||
t.Fatalf("Candidates[%d].IP = %q, want %q", i, candidates[i].IP, ip)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveLocalBindIPPrefersExplicitSource(t *testing.T) {
|
||||
ip, err := resolveLocalBindIP(net.ParseIP("1.1.1.1"), "127.0.0.1", "missing0")
|
||||
if err != nil {
|
||||
|
||||
@@ -73,9 +73,7 @@ func Run(
|
||||
}()
|
||||
|
||||
for i := 0; i < threads; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
wg.Go(func() {
|
||||
var fault bool
|
||||
if dir == Download {
|
||||
_, fault = doDownload(ctx2, client, spec, timeout, &totalBytes)
|
||||
@@ -85,7 +83,7 @@ func Run(
|
||||
if fault {
|
||||
faultCount.Add(1)
|
||||
}
|
||||
}()
|
||||
})
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
+1
-4
@@ -1,8 +1,5 @@
|
||||
package trace
|
||||
|
||||
func ClearCaches() {
|
||||
geoCache.Range(func(key, value any) bool {
|
||||
geoCache.Delete(key)
|
||||
return true
|
||||
})
|
||||
geoCache.Clear()
|
||||
}
|
||||
@@ -10,6 +10,20 @@ import (
|
||||
"github.com/nxtrace/NTrace-core/ipgeo"
|
||||
)
|
||||
|
||||
func TestClearCachesRemovesAllEntries(t *testing.T) {
|
||||
geoCache.Store("198.51.100.1", &ipgeo.IPGeoData{IP: "198.51.100.1"})
|
||||
geoCache.Store("2001:db8::1", &ipgeo.IPGeoData{IP: "2001:db8::1"})
|
||||
|
||||
ClearCaches()
|
||||
|
||||
if _, ok := geoCache.Load("198.51.100.1"); ok {
|
||||
t.Fatal("IPv4 cache entry remained after ClearCaches")
|
||||
}
|
||||
if _, ok := geoCache.Load("2001:db8::1"); ok {
|
||||
t.Fatal("IPv6 cache entry remained after ClearCaches")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLookupIPGeoCachesResults(t *testing.T) {
|
||||
ClearCaches()
|
||||
t.Cleanup(ClearCaches)
|
||||
|
||||
+2
-6
@@ -11,6 +11,7 @@ import (
|
||||
"net/url"
|
||||
"os"
|
||||
"runtime"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -589,12 +590,7 @@ func GetPowProvider() string {
|
||||
}
|
||||
|
||||
func StringInSlice(val string, list []string) bool {
|
||||
for _, v := range list {
|
||||
if v == val {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
return slices.Contains(list, val)
|
||||
}
|
||||
|
||||
func HideIPPart(ip string) string {
|
||||
|
||||
+1
-4
@@ -23,10 +23,7 @@ import (
|
||||
func formatHostPort(addr, port string) string {
|
||||
clean := strings.TrimSpace(addr)
|
||||
clean = strings.Trim(clean, "[]")
|
||||
if strings.Contains(clean, ":") {
|
||||
return "[" + clean + "]:" + port
|
||||
}
|
||||
return clean + ":" + port
|
||||
return net.JoinHostPort(clean, port)
|
||||
}
|
||||
|
||||
type wsWriteJob struct {
|
||||
|
||||
@@ -17,6 +17,29 @@ import (
|
||||
"github.com/nxtrace/NTrace-core/util"
|
||||
)
|
||||
|
||||
func TestFormatHostPort(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
addr string
|
||||
port string
|
||||
want string
|
||||
}{
|
||||
{name: "IPv4", addr: " 192.0.2.1 ", port: "443", want: "192.0.2.1:443"},
|
||||
{name: "IPv6", addr: "2001:db8::1", port: "443", want: "[2001:db8::1]:443"},
|
||||
{name: "bracketed IPv6", addr: "[2001:db8::1]", port: "443", want: "[2001:db8::1]:443"},
|
||||
{name: "IPv6 zone", addr: "fe80::1%en0", port: "443", want: "[fe80::1%en0]:443"},
|
||||
{name: "empty host", addr: "", port: "443", want: ":443"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := formatHostPort(tt.addr, tt.port); got != tt.want {
|
||||
t.Fatalf("formatHostPort(%q, %q) = %q, want %q", tt.addr, tt.port, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func newStartedTestWsConn() *WsConn {
|
||||
return newStartedTestWsConnWithContext(context.Background())
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user