GoDoxy/internal/watcher/health/status.go
Yuzerion 80bc018a7f
feat: custom json marshaling implementation, replace json and yaml library (#89)
* chore: replace gopkg.in/yaml.v3 vs goccy/go-yaml; replace encoding/json with bytedance/sonic

* fix: yaml unmarshal panic

* feat: custom json marshaler implementation

* chore: fix import and err marshal handling

---------

Co-authored-by: yusing <yusing@6uo.me>
2025-04-16 15:02:11 +08:00

72 lines
1.2 KiB
Go

package health
import (
"github.com/yusing/go-proxy/pkg/json"
)
type Status uint8
const (
StatusUnknown Status = 0
StatusHealthy = (1 << iota)
StatusNapping
StatusStarting
StatusUnhealthy
StatusError
NumStatuses int = iota - 1
HealthyMask = StatusHealthy | StatusNapping | StatusStarting
IdlingMask = StatusNapping | StatusStarting
)
func (s Status) String() string {
switch s {
case StatusHealthy:
return "healthy"
case StatusUnhealthy:
return "unhealthy"
case StatusNapping:
return "napping"
case StatusStarting:
return "starting"
case StatusError:
return "error"
default:
return "unknown"
}
}
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
}
func (s Status) Bad() bool {
return s&HealthyMask == 0
}
func (s Status) Idling() bool {
return s&IdlingMask != 0
}