发布网络质量探测 1.1.2:更新服务端探测与节点源码

署名:言灸
This commit is contained in:
YanJiu committed 2026-11-20 19:01:21 +08:00
1 parent 3e912d4d3c
commit 1d35747b62
3 files changed
+77 -29

No files matched your search

+48 -4
View File
@@ -21,6 +21,7 @@ type jobSpec struct {
Port int
Profile string
IP net.IP
state *dialState
Interval time.Duration
Timeout time.Duration
Duration time.Duration
@@ -110,6 +111,7 @@ func (n *Node) info(w http.ResponseWriter, _ *http.Request) {
vis = "public"
}
writeJSON(w, http.StatusOK, map[string]any{
"version": appVersion,
"visibility": vis,
"authRequired": !n.Public(),
"profiles": []string{
@@ -185,10 +187,19 @@ func (n *Node) create(w http.ResponseWriter, r *http.Request) {
writeErr(w, http.StatusInternalServerError, "error", "任务没有启动")
return
}
resolved := make([]map[string]string, 0, len(spec.candidates()))
for _, ip := range spec.candidates() {
resolved = append(resolved, map[string]string{
"address": ip.String(),
"family": familyOf(ip),
})
}
writeJSON(w, http.StatusCreated, map[string]any{
"jobId": job.id,
"expiresAt": job.expires.UTC().Format(time.RFC3339),
"visibility": map[bool]string{true: "public", false: "private"}[n.Public()],
"version": appVersion,
"resolved": resolved,
})
}
@@ -246,7 +257,7 @@ func (n *Node) prepare(ctx context.Context, req createReq) (jobSpec, error) {
}
}
}
ip, err := targetOK(ctx, n.Resolver, req.Target, n.AllowPrivate, n.ExtraNets)
ips, err := resolveTargets(ctx, n.Resolver, req.Target, n.AllowPrivate, n.ExtraNets)
if err != nil {
var dnsErr *net.DNSError
if errors.As(err, &dnsErr) {
@@ -258,7 +269,8 @@ func (n *Node) prepare(ctx context.Context, req createReq) (jobSpec, error) {
return jobSpec{}, &apiError{http.StatusBadRequest, "bad_target", "目标地址无效"}
}
return jobSpec{
Host: req.Target, Port: req.Port, Profile: req.Profile, IP: ip,
Host: req.Target, Port: req.Port, Profile: req.Profile, IP: ips[0],
state: &dialState{picker: newEndpointPicker(ips)},
Interval: interval, Timeout: timeout, Duration: duration, PacketSize: size,
Echo: echo, DNSName: req.DNSName, Payload: payload, Expect: expect,
}, nil
@@ -334,18 +346,50 @@ func (n *Node) run(ctx context.Context, job *probeJob, spec jobSpec, p prober) {
case <-ctx.Done():
}
}
ledger := &selectionLedger{candidates: len(spec.candidates())}
datagram := datagramProfile(spec.Profile)
for {
if err := n.pace.wait(ctx, spec.PacketSize); err != nil {
return
}
started := time.Now()
pctx, cancel := context.WithTimeout(ctx, spec.Timeout)
budget := spec.Timeout
if budget <= 0 {
budget = 1500 * time.Millisecond
}
if !datagram && spec.state != nil && spec.state.picker != nil && !spec.state.picker.locked && len(spec.state.picker.ips) > 1 {
budget *= time.Duration(len(spec.state.picker.ips))
}
if datagram && spec.state != nil {
if ip := spec.dialIP(); ip != nil {
spec.state.used = append(net.IP(nil), ip...)
}
}
pctx, cancel := context.WithTimeout(ctx, budget)
out := p.Probe(pctx)
cancel()
if ctx.Err() != nil {
return
}
emit(out)
out = spec.decorate(out)
var commit []Outcome
if datagram && spec.state != nil && spec.state.picker != nil {
responded := selectionResponded(out.Status)
wasMeasuring := ledger.measuring
changed := spec.state.picker.record(responded)
commit, _ = ledger.take(out, responded)
if wasMeasuring && changed {
ledger.measuring = false
}
if len(commit) == 0 {
continue
}
} else {
commit = []Outcome{out}
}
for _, item := range commit {
emit(item)
}
wait := spec.Interval - time.Since(started)
if wait < 0 {
wait = 0
+6
View File
@@ -163,6 +163,12 @@ func TestNodeNQAndTCPAndUDP(t *testing.T) {
if sample.Status != statusOK || sample.RttMs == nil || *sample.RttMs <= 0 {
t.Fatalf("nq tcp %+v", sample)
}
if sample.Detail["address"] != "127.0.0.1" || sample.Detail["family"] != "IPv4" {
t.Fatalf("tcp did not report the connected address %+v", sample.Detail)
}
if sample.Summary.MinRttMs == nil || sample.Summary.P95RttMs == nil {
t.Fatalf("latency stats missing %+v", sample.Summary)
}
if sample.Summary.ProbeLossPct == nil || *sample.Summary.ProbeLossPct != 0 {
t.Fatalf("loss %+v", sample.Summary)
}
+23 -25
View File
@@ -127,7 +127,7 @@ func (p *nqTCP) Close() error {
}
func (p *nqTCP) dial(ctx context.Context) error {
conn, err := dialTCP(ctx, p.spec.IP, p.spec.Port)
conn, err := dialSpecTCP(ctx, &p.spec)
if err != nil {
return err
}
@@ -208,11 +208,9 @@ func (p *nqUDP) Close() error {
}
func (p *nqUDP) ensure() error {
if p.conn != nil {
return nil
}
c, err := dialUDP(p.spec.IP, p.spec.Port)
c, err := ensureUDP(p.conn, p.spec)
if err != nil {
p.conn = nil
return err
}
p.conn = c
@@ -273,7 +271,7 @@ func (p *tcpConnect) Close() error { return nil }
func (p *tcpConnect) Probe(ctx context.Context) Outcome {
start := time.Now()
conn, err := dialTCP(ctx, p.spec.IP, p.spec.Port)
conn, err := dialSpecTCP(ctx, &p.spec)
if err != nil {
return classifyDial(err)
}
@@ -297,13 +295,12 @@ func (p *udpQuery) Close() error {
}
func (p *udpQuery) Probe(ctx context.Context) Outcome {
if p.conn == nil {
c, err := dialUDP(p.spec.IP, p.spec.Port)
if err != nil {
return classifyDial(err)
}
p.conn = c
c, err := ensureUDP(p.conn, p.spec)
if err != nil {
p.conn = nil
return classifyDial(err)
}
p.conn = c
payload := p.spec.Payload
if p.generic {
n := p.spec.PacketSize
@@ -358,13 +355,12 @@ func (p *dnsQuery) Close() error {
}
func (p *dnsQuery) Probe(ctx context.Context) Outcome {
if p.conn == nil {
c, err := dialUDP(p.spec.IP, p.spec.Port)
if err != nil {
return classifyDial(err)
}
p.conn = c
c, err := ensureUDP(p.conn, p.spec)
if err != nil {
p.conn = nil
return classifyDial(err)
}
p.conn = c
p.id++
if p.id == 0 {
p.id = 1
@@ -449,12 +445,14 @@ func (p *mcBedrock) Close() error {
}
func (p *mcBedrock) Probe(ctx context.Context) Outcome {
if p.conn == nil {
c, err := dialUDP(p.spec.IP, p.spec.Port)
if err != nil {
return classifyDial(err)
}
p.conn = c
fresh := p.conn == nil
c, err := ensureUDP(p.conn, p.spec)
if err != nil {
p.conn = nil
return classifyDial(err)
}
p.conn = c
if fresh || p.guid == 0 {
p.guid = uint64(time.Now().UnixNano())
}
ts := uint64(time.Now().UnixNano())
@@ -529,7 +527,7 @@ type mcJava struct{ spec jobSpec }
func (p *mcJava) Close() error { return nil }
func (p *mcJava) Probe(ctx context.Context) Outcome {
conn, err := dialTCP(ctx, p.spec.IP, p.spec.Port)
conn, err := dialSpecTCP(ctx, &p.spec)
if err != nil {
return classifyDial(err)
}