mirror of https://github.com/XTLS/Xray-core
Merge 360bd4716e
into fe57507fd9
commit
a713dfa8f4
|
@ -36,21 +36,23 @@ func (o *Observer) createResult() []*observatory.OutboundStatus {
|
|||
var result []*observatory.OutboundStatus
|
||||
o.hp.access.Lock()
|
||||
defer o.hp.access.Unlock()
|
||||
|
||||
for name, value := range o.hp.Results {
|
||||
curStat := value.getStatistics()
|
||||
status := observatory.OutboundStatus{
|
||||
Alive: value.getStatistics().All != value.getStatistics().Fail,
|
||||
Delay: value.getStatistics().Average.Milliseconds(),
|
||||
Alive: curStat.All != curStat.Fail,
|
||||
Delay: curStat.Average.Milliseconds(),
|
||||
LastErrorReason: "",
|
||||
OutboundTag: name,
|
||||
LastSeenTime: 0,
|
||||
LastTryTime: 0,
|
||||
LastSeenTime: curStat.LastSeenTime,
|
||||
LastTryTime: curStat.LastTryTime,
|
||||
HealthPing: &observatory.HealthPingMeasurementResult{
|
||||
All: int64(value.getStatistics().All),
|
||||
Fail: int64(value.getStatistics().Fail),
|
||||
Deviation: int64(value.getStatistics().Deviation),
|
||||
Average: int64(value.getStatistics().Average),
|
||||
Max: int64(value.getStatistics().Max),
|
||||
Min: int64(value.getStatistics().Min),
|
||||
All: int64(curStat.All),
|
||||
Fail: int64(curStat.Fail),
|
||||
Deviation: int64(curStat.Deviation),
|
||||
Average: int64(curStat.Average),
|
||||
Max: int64(curStat.Max),
|
||||
Min: int64(curStat.Min),
|
||||
},
|
||||
}
|
||||
result = append(result, &status)
|
||||
|
|
|
@ -7,12 +7,14 @@ import (
|
|||
|
||||
// HealthPingStats is the statistics of HealthPingRTTS
|
||||
type HealthPingStats struct {
|
||||
All int
|
||||
Fail int
|
||||
Deviation time.Duration
|
||||
Average time.Duration
|
||||
Max time.Duration
|
||||
Min time.Duration
|
||||
All int
|
||||
Fail int
|
||||
Deviation time.Duration
|
||||
Average time.Duration
|
||||
Max time.Duration
|
||||
Min time.Duration
|
||||
LastSeenTime int64
|
||||
LastTryTime int64
|
||||
}
|
||||
|
||||
// HealthPingRTTS holds ping rtts for health Checker
|
||||
|
@ -87,6 +89,10 @@ func (h *HealthPingRTTS) getStatistics() *HealthPingStats {
|
|||
cnt := 0
|
||||
validRTTs := make([]time.Duration, 0)
|
||||
for _, rtt := range h.rtts {
|
||||
timestamp := rtt.time.Unix()
|
||||
if timestamp > stats.LastTryTime {
|
||||
stats.LastTryTime = timestamp
|
||||
}
|
||||
switch {
|
||||
case rtt.value == 0 || time.Since(rtt.time) > h.validity:
|
||||
continue
|
||||
|
@ -94,6 +100,9 @@ func (h *HealthPingRTTS) getStatistics() *HealthPingStats {
|
|||
stats.Fail++
|
||||
continue
|
||||
}
|
||||
if timestamp > stats.LastSeenTime {
|
||||
stats.LastSeenTime = timestamp
|
||||
}
|
||||
cnt++
|
||||
sum += rtt.value
|
||||
validRTTs = append(validRTTs, rtt.value)
|
||||
|
|
|
@ -25,6 +25,8 @@ func TestHealthPingResults(t *testing.T) {
|
|||
Min: 60,
|
||||
}
|
||||
actual := hr.Get()
|
||||
actual.LastSeenTime = 0
|
||||
actual.LastTryTime = 0
|
||||
if !reflect.DeepEqual(expected, actual) {
|
||||
t.Errorf("expected: %v, actual: %v", expected, actual)
|
||||
}
|
||||
|
@ -32,6 +34,8 @@ func TestHealthPingResults(t *testing.T) {
|
|||
hr.Put(rttFailed)
|
||||
expected.Fail = 2
|
||||
actual = hr.Get()
|
||||
actual.LastSeenTime = 0
|
||||
actual.LastTryTime = 0
|
||||
if !reflect.DeepEqual(expected, actual) {
|
||||
t.Errorf("failed half-failures test, expected: %v, actual: %v", expected, actual)
|
||||
}
|
||||
|
@ -46,6 +50,8 @@ func TestHealthPingResults(t *testing.T) {
|
|||
Min: 0,
|
||||
}
|
||||
actual = hr.Get()
|
||||
actual.LastSeenTime = 0
|
||||
actual.LastTryTime = 0
|
||||
if !reflect.DeepEqual(expected, actual) {
|
||||
t.Errorf("failed all-failures test, expected: %v, actual: %v", expected, actual)
|
||||
}
|
||||
|
@ -71,6 +77,8 @@ func TestHealthPingResultsIgnoreOutdated(t *testing.T) {
|
|||
Min: 60,
|
||||
}
|
||||
actual := hr.Get()
|
||||
actual.LastSeenTime = 0
|
||||
actual.LastTryTime = 0
|
||||
if !reflect.DeepEqual(expected, actual) {
|
||||
t.Errorf("failed 'half-outdated' test, expected: %v, actual: %v", expected, actual)
|
||||
}
|
||||
|
@ -85,6 +93,8 @@ func TestHealthPingResultsIgnoreOutdated(t *testing.T) {
|
|||
Min: 0,
|
||||
}
|
||||
actual = hr.Get()
|
||||
actual.LastSeenTime = 0
|
||||
actual.LastTryTime = 0
|
||||
if !reflect.DeepEqual(expected, actual) {
|
||||
t.Errorf("failed 'outdated / not-tested' test, expected: %v, actual: %v", expected, actual)
|
||||
}
|
||||
|
@ -100,6 +110,8 @@ func TestHealthPingResultsIgnoreOutdated(t *testing.T) {
|
|||
Min: 60,
|
||||
}
|
||||
actual = hr.Get()
|
||||
actual.LastSeenTime = 0
|
||||
actual.LastTryTime = 0
|
||||
if !reflect.DeepEqual(expected, actual) {
|
||||
t.Errorf("expected: %v, actual: %v", expected, actual)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue