fix: URL test result with expectedStatus

This commit is contained in:
lucidhz 2025-08-28 19:12:30 +08:00
parent 84086a6e6c
commit a61c140f81
2 changed files with 10 additions and 15 deletions

View File

@ -185,33 +185,22 @@ func (p *Proxy) URLTest(ctx context.Context, url string, expectedStatus utils.In
if alive { if alive {
record.Delay = t record.Delay = t
} }
if !satisfied {
p.alive.Store(alive) alive = false
p.history.Put(record)
if p.history.Len() > defaultHistoriesNum {
p.history.Pop()
} }
state, ok := p.extra.Load(url) state, ok := p.extra.Load(url)
if !ok { if !ok {
state = &internalProxyState{ state = &internalProxyState{
history: queue.New[C.DelayHistory](defaultHistoriesNum), history: queue.New[C.DelayHistory](defaultHistoriesNum),
alive: atomic.NewBool(true), alive: atomic.NewBool(alive),
} }
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 {
state.history.Pop() state.history.Pop()
} }
}() }()
unifiedDelay := UnifiedDelay.Load() unifiedDelay := UnifiedDelay.Load()
@ -285,6 +274,9 @@ func (p *Proxy) URLTest(ctx context.Context, url string, expectedStatus utils.In
satisfied = resp != nil && (expectedStatus == nil || expectedStatus.Check(uint16(resp.StatusCode))) satisfied = resp != nil && (expectedStatus == nil || expectedStatus.Check(uint16(resp.StatusCode)))
t = uint16(time.Since(start) / time.Millisecond) t = uint16(time.Since(start) / time.Millisecond)
if !satisfied {
err = fmt.Errorf("failed by unsatisfied!status:%d", resp.StatusCode)
}
return return
} }

View File

@ -135,7 +135,10 @@ func getProxyDelay(w http.ResponseWriter, r *http.Request) {
if err != nil || delay == 0 { if err != nil || delay == 0 {
render.Status(r, http.StatusServiceUnavailable) render.Status(r, http.StatusServiceUnavailable)
if err != nil && delay != 0 { if err != nil && delay != 0 {
render.JSON(w, r, err) render.JSON(w, r, render.M{
"delay": delay,
"error": err.Error(),
})
} else { } else {
render.JSON(w, r, newError("An error occurred in the delay test")) render.JSON(w, r, newError("An error occurred in the delay test"))
} }