2020-04-21 12:16:28 +07:00
|
|
|
package check
|
|
|
|
|
|
|
|
import (
|
2020-04-23 07:41:02 +07:00
|
|
|
"encoding/json"
|
2020-04-21 12:16:28 +07:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
//Result denotes the status from a status check.
|
|
|
|
type Result uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
//Online means all requests were fulfilled by the remote end.
|
|
|
|
Online Result = iota
|
|
|
|
//Unstable means some requests were fulfilled by the remote end.
|
|
|
|
Unstable
|
|
|
|
//Offline means every requests to the remote end has failed.
|
|
|
|
Offline
|
|
|
|
)
|
|
|
|
|
|
|
|
//TryCount is the amount of times requests will be made to the remote end.
|
|
|
|
const TryCount = 3
|
|
|
|
|
|
|
|
func statusFromSuccessCount(count int) Result {
|
|
|
|
if count == 0 {
|
|
|
|
return Offline
|
|
|
|
}
|
|
|
|
if count < TryCount {
|
|
|
|
return Unstable
|
|
|
|
}
|
|
|
|
return Online
|
|
|
|
}
|
|
|
|
|
|
|
|
func divide(t time.Duration, dividend int) time.Duration {
|
|
|
|
return time.Duration(int(t) / dividend)
|
|
|
|
}
|
2020-04-23 07:41:02 +07:00
|
|
|
|
|
|
|
func (r Result) String() string {
|
|
|
|
switch r {
|
|
|
|
case Online:
|
|
|
|
return "online"
|
|
|
|
case Unstable:
|
|
|
|
return "unstable"
|
|
|
|
case Offline:
|
|
|
|
return "offline"
|
|
|
|
}
|
|
|
|
panic("Unexpected value")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r Result) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(r.String())
|
|
|
|
}
|