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