refactor: clean up code and enhance utilities with new functions

This commit is contained in:
yusing
2025-03-28 08:08:33 +08:00
parent 7707fc6f36
commit cdb3ffe439
9 changed files with 103 additions and 17 deletions

View File

@@ -34,6 +34,8 @@ const (
ActionContainerDie
ActionContainerDestroy
ActionForceReload
actionContainerWakeMask = ActionContainerCreate | ActionContainerStart | ActionContainerUnpause
actionContainerSleepMask = ActionContainerKill | ActionContainerStop | ActionContainerPause | ActionContainerDie
)

View File

@@ -14,7 +14,9 @@ type HealthCheckConfig struct {
Timeout time.Duration `json:"timeout" validate:"omitempty,min=1s"`
}
var DefaultHealthConfig = &HealthCheckConfig{
Interval: common.HealthCheckIntervalDefault,
Timeout: common.HealthCheckTimeoutDefault,
func DefaultHealthConfig() *HealthCheckConfig {
return &HealthCheckConfig{
Interval: common.HealthCheckIntervalDefault,
Timeout: common.HealthCheckTimeoutDefault,
}
}

View File

@@ -1,5 +1,7 @@
package health
import "encoding/json"
type Status uint8
const (
@@ -13,6 +15,7 @@ const (
NumStatuses int = iota - 1
HealthyMask = StatusHealthy | StatusNapping | StatusStarting
IdlingMask = StatusNapping | StatusStarting
)
func (s Status) String() string {
@@ -36,6 +39,28 @@ func (s Status) MarshalJSON() ([]byte, error) {
return []byte(`"` + s.String() + `"`), nil
}
func (s *Status) UnmarshalJSON(data []byte) error {
var str string
if err := json.Unmarshal(data, &str); err != nil {
return err
}
switch str {
case "healthy":
*s = StatusHealthy
case "unhealthy":
*s = StatusUnhealthy
case "napping":
*s = StatusNapping
case "starting":
*s = StatusStarting
case "error":
*s = StatusError
default:
*s = StatusUnknown
}
return nil
}
func (s Status) Good() bool {
return s&HealthyMask != 0
}
@@ -43,3 +68,7 @@ func (s Status) Good() bool {
func (s Status) Bad() bool {
return s&HealthyMask == 0
}
func (s Status) Idling() bool {
return s&IdlingMask != 0
}

View File

@@ -11,9 +11,9 @@ import (
type (
HealthCheckResult struct {
Healthy bool
Detail string
Latency time.Duration
Healthy bool `json:"healthy"`
Detail string `json:"detail"`
Latency time.Duration `json:"latency"`
}
WithHealthInfo interface {
Status() Status