发布网络质量探测 1.1.2:更新服务端探测与节点源码
署名:言灸
This commit is contained in:
1 parent
3e912d4d3c
commit
1d35747b62
3 files changed
+67
-19
No files matched your search
+48
-4
@@ -21,6 +21,7 @@ type jobSpec struct {
|
|||||||
Port int
|
Port int
|
||||||
Profile string
|
Profile string
|
||||||
IP net.IP
|
IP net.IP
|
||||||
|
state *dialState
|
||||||
Interval time.Duration
|
Interval time.Duration
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
Duration time.Duration
|
Duration time.Duration
|
||||||
@@ -110,6 +111,7 @@ func (n *Node) info(w http.ResponseWriter, _ *http.Request) {
|
|||||||
vis = "public"
|
vis = "public"
|
||||||
}
|
}
|
||||||
writeJSON(w, http.StatusOK, map[string]any{
|
writeJSON(w, http.StatusOK, map[string]any{
|
||||||
|
"version": appVersion,
|
||||||
"visibility": vis,
|
"visibility": vis,
|
||||||
"authRequired": !n.Public(),
|
"authRequired": !n.Public(),
|
||||||
"profiles": []string{
|
"profiles": []string{
|
||||||
@@ -185,10 +187,19 @@ func (n *Node) create(w http.ResponseWriter, r *http.Request) {
|
|||||||
writeErr(w, http.StatusInternalServerError, "error", "任务没有启动")
|
writeErr(w, http.StatusInternalServerError, "error", "任务没有启动")
|
||||||
return
|
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{
|
writeJSON(w, http.StatusCreated, map[string]any{
|
||||||
"jobId": job.id,
|
"jobId": job.id,
|
||||||
"expiresAt": job.expires.UTC().Format(time.RFC3339),
|
"expiresAt": job.expires.UTC().Format(time.RFC3339),
|
||||||
"visibility": map[bool]string{true: "public", false: "private"}[n.Public()],
|
"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 {
|
if err != nil {
|
||||||
var dnsErr *net.DNSError
|
var dnsErr *net.DNSError
|
||||||
if errors.As(err, &dnsErr) {
|
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{}, &apiError{http.StatusBadRequest, "bad_target", "目标地址无效"}
|
||||||
}
|
}
|
||||||
return jobSpec{
|
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,
|
Interval: interval, Timeout: timeout, Duration: duration, PacketSize: size,
|
||||||
Echo: echo, DNSName: req.DNSName, Payload: payload, Expect: expect,
|
Echo: echo, DNSName: req.DNSName, Payload: payload, Expect: expect,
|
||||||
}, nil
|
}, nil
|
||||||
@@ -334,18 +346,50 @@ func (n *Node) run(ctx context.Context, job *probeJob, spec jobSpec, p prober) {
|
|||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
ledger := &selectionLedger{candidates: len(spec.candidates())}
|
||||||
|
datagram := datagramProfile(spec.Profile)
|
||||||
for {
|
for {
|
||||||
if err := n.pace.wait(ctx, spec.PacketSize); err != nil {
|
if err := n.pace.wait(ctx, spec.PacketSize); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
started := time.Now()
|
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)
|
out := p.Probe(pctx)
|
||||||
cancel()
|
cancel()
|
||||||
if ctx.Err() != nil {
|
if ctx.Err() != nil {
|
||||||
return
|
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)
|
wait := spec.Interval - time.Since(started)
|
||||||
if wait < 0 {
|
if wait < 0 {
|
||||||
wait = 0
|
wait = 0
|
||||||
|
|||||||
@@ -163,6 +163,12 @@ func TestNodeNQAndTCPAndUDP(t *testing.T) {
|
|||||||
if sample.Status != statusOK || sample.RttMs == nil || *sample.RttMs <= 0 {
|
if sample.Status != statusOK || sample.RttMs == nil || *sample.RttMs <= 0 {
|
||||||
t.Fatalf("nq tcp %+v", sample)
|
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 {
|
if sample.Summary.ProbeLossPct == nil || *sample.Summary.ProbeLossPct != 0 {
|
||||||
t.Fatalf("loss %+v", sample.Summary)
|
t.Fatalf("loss %+v", sample.Summary)
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-15
@@ -127,7 +127,7 @@ func (p *nqTCP) Close() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *nqTCP) dial(ctx context.Context) 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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -208,11 +208,9 @@ func (p *nqUDP) Close() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *nqUDP) ensure() error {
|
func (p *nqUDP) ensure() error {
|
||||||
if p.conn != nil {
|
c, err := ensureUDP(p.conn, p.spec)
|
||||||
return nil
|
|
||||||
}
|
|
||||||
c, err := dialUDP(p.spec.IP, p.spec.Port)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
p.conn = nil
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
p.conn = c
|
p.conn = c
|
||||||
@@ -273,7 +271,7 @@ func (p *tcpConnect) Close() error { return nil }
|
|||||||
|
|
||||||
func (p *tcpConnect) Probe(ctx context.Context) Outcome {
|
func (p *tcpConnect) Probe(ctx context.Context) Outcome {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
conn, err := dialTCP(ctx, p.spec.IP, p.spec.Port)
|
conn, err := dialSpecTCP(ctx, &p.spec)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return classifyDial(err)
|
return classifyDial(err)
|
||||||
}
|
}
|
||||||
@@ -297,13 +295,12 @@ func (p *udpQuery) Close() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *udpQuery) Probe(ctx context.Context) Outcome {
|
func (p *udpQuery) Probe(ctx context.Context) Outcome {
|
||||||
if p.conn == nil {
|
c, err := ensureUDP(p.conn, p.spec)
|
||||||
c, err := dialUDP(p.spec.IP, p.spec.Port)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
p.conn = nil
|
||||||
return classifyDial(err)
|
return classifyDial(err)
|
||||||
}
|
}
|
||||||
p.conn = c
|
p.conn = c
|
||||||
}
|
|
||||||
payload := p.spec.Payload
|
payload := p.spec.Payload
|
||||||
if p.generic {
|
if p.generic {
|
||||||
n := p.spec.PacketSize
|
n := p.spec.PacketSize
|
||||||
@@ -358,13 +355,12 @@ func (p *dnsQuery) Close() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *dnsQuery) Probe(ctx context.Context) Outcome {
|
func (p *dnsQuery) Probe(ctx context.Context) Outcome {
|
||||||
if p.conn == nil {
|
c, err := ensureUDP(p.conn, p.spec)
|
||||||
c, err := dialUDP(p.spec.IP, p.spec.Port)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
p.conn = nil
|
||||||
return classifyDial(err)
|
return classifyDial(err)
|
||||||
}
|
}
|
||||||
p.conn = c
|
p.conn = c
|
||||||
}
|
|
||||||
p.id++
|
p.id++
|
||||||
if p.id == 0 {
|
if p.id == 0 {
|
||||||
p.id = 1
|
p.id = 1
|
||||||
@@ -449,12 +445,14 @@ func (p *mcBedrock) Close() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *mcBedrock) Probe(ctx context.Context) Outcome {
|
func (p *mcBedrock) Probe(ctx context.Context) Outcome {
|
||||||
if p.conn == nil {
|
fresh := p.conn == nil
|
||||||
c, err := dialUDP(p.spec.IP, p.spec.Port)
|
c, err := ensureUDP(p.conn, p.spec)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
p.conn = nil
|
||||||
return classifyDial(err)
|
return classifyDial(err)
|
||||||
}
|
}
|
||||||
p.conn = c
|
p.conn = c
|
||||||
|
if fresh || p.guid == 0 {
|
||||||
p.guid = uint64(time.Now().UnixNano())
|
p.guid = uint64(time.Now().UnixNano())
|
||||||
}
|
}
|
||||||
ts := 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) Close() error { return nil }
|
||||||
|
|
||||||
func (p *mcJava) Probe(ctx context.Context) Outcome {
|
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 {
|
if err != nil {
|
||||||
return classifyDial(err)
|
return classifyDial(err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user