chore: store latency data more reasonably (#964)

This commit is contained in:
tommy 2024-01-30 15:51:12 +08:00 committed by GitHub
parent d1337f39ed
commit 947ad9b308
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 8 deletions

View File

@ -3,7 +3,6 @@ package adapter
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"net" "net"
"net/http" "net/http"
@ -163,6 +162,8 @@ func (p *Proxy) MarshalJSON() ([]byte, error) {
// URLTest get the delay for the specified URL // URLTest get the delay for the specified URL
// implements C.Proxy // implements C.Proxy
func (p *Proxy) URLTest(ctx context.Context, url string, expectedStatus utils.IntRanges[uint16]) (t uint16, err error) { func (p *Proxy) URLTest(ctx context.Context, url string, expectedStatus utils.IntRanges[uint16]) (t uint16, err error) {
var satisfied bool
defer func() { defer func() {
alive := err == nil alive := err == nil
record := C.DelayHistory{Time: time.Now()} record := C.DelayHistory{Time: time.Now()}
@ -185,6 +186,11 @@ func (p *Proxy) URLTest(ctx context.Context, url string, expectedStatus utils.In
p.extra.Store(url, state) p.extra.Store(url, state)
} }
if !satisfied {
record.Delay = 0
alive = false
}
state.alive.Store(alive) state.alive.Store(alive)
state.history.Put(record) state.history.Put(record)
if state.history.Len() > defaultHistoriesNum { if state.history.Len() > defaultHistoriesNum {
@ -253,15 +259,10 @@ func (p *Proxy) URLTest(ctx context.Context, url string, expectedStatus utils.In
} }
} }
if expectedStatus != nil && !expectedStatus.Check(uint16(resp.StatusCode)) { satisfied = resp != nil && (expectedStatus == nil || expectedStatus.Check(uint16(resp.StatusCode)))
// maybe another value should be returned for differentiation
err = errors.New("response status is inconsistent with the expected status")
}
t = uint16(time.Since(start) / time.Millisecond) t = uint16(time.Since(start) / time.Millisecond)
return return
} }
func NewProxy(adapter C.ProxyAdapter) *Proxy { func NewProxy(adapter C.ProxyAdapter) *Proxy {
return &Proxy{ return &Proxy{
ProxyAdapter: adapter, ProxyAdapter: adapter,

View File

@ -788,6 +788,7 @@ proxy-groups:
- vmess1 - vmess1
# tolerance: 150 # tolerance: 150
# lazy: true # lazy: true
# expected-status: 204 # 当健康检查返回状态码与期望值不符时,认为节点不可用
url: "https://cp.cloudflare.com/generate_204" url: "https://cp.cloudflare.com/generate_204"
interval: 300 interval: 300
@ -851,6 +852,7 @@ proxy-providers:
interval: 600 interval: 600
# lazy: true # lazy: true
url: https://cp.cloudflare.com/generate_204 url: https://cp.cloudflare.com/generate_204
# expected-status: 204 # 当健康检查返回状态码与期望值不符时,认为节点不可用
override: # 覆写节点加载时的一些配置项 override: # 覆写节点加载时的一些配置项
skip-cert-verify: true skip-cert-verify: true
udp: true udp: true