refactor(healthcheck): streamline health check configuration and defaults

- Moved health check constants from common package alongside type definition.
- Updated health check configuration to use struct directly instead of pointers.
- Introduced global default health check config
This commit is contained in:
yusing
2025-12-04 15:19:10 +08:00
parent 48627753d6
commit 55a42b81de
15 changed files with 66 additions and 51 deletions

View File

@@ -3,25 +3,39 @@ package types
import (
"context"
"time"
"github.com/yusing/godoxy/internal/common"
)
type HealthCheckConfig struct {
Disable bool `json:"disable,omitempty" aliases:"disabled"`
Path string `json:"path,omitempty" validate:"omitempty,uri,startswith=/"`
UseGet bool `json:"use_get,omitempty"`
Path string `json:"path,omitempty" validate:"omitempty,uri,startswith=/"`
Interval time.Duration `json:"interval" validate:"omitempty,min=1s" swaggertype:"primitive,integer"`
Timeout time.Duration `json:"timeout" validate:"omitempty,min=1s" swaggertype:"primitive,integer"`
Retries int64 `json:"retries"` // <0: immediate, >=0: threshold
Retries int64 `json:"retries"` // <0: immediate, 0: default, >0: threshold
BaseContext func() context.Context `json:"-"`
} // @name HealthCheckConfig
func DefaultHealthConfig() *HealthCheckConfig {
return &HealthCheckConfig{
Interval: common.HealthCheckIntervalDefault,
Timeout: common.HealthCheckTimeoutDefault,
Retries: int64(common.HealthCheckDownNotifyDelayDefault / common.HealthCheckIntervalDefault),
const (
HealthCheckIntervalDefault = 5 * time.Second
HealthCheckTimeoutDefault = 5 * time.Second
HealthCheckDownNotifyDelayDefault = 15 * time.Second
)
func (hc *HealthCheckConfig) ApplyDefaults(defaults HealthCheckConfig) {
if hc.Interval == 0 {
hc.Interval = defaults.Interval
if hc.Interval == 0 {
hc.Interval = HealthCheckIntervalDefault
}
}
if hc.Timeout == 0 {
hc.Timeout = defaults.Timeout
if hc.Timeout == 0 {
hc.Timeout = HealthCheckTimeoutDefault
}
}
if hc.Retries == 0 {
hc.Retries = int64(HealthCheckDownNotifyDelayDefault / hc.Interval)
}
}

View File

@@ -29,7 +29,7 @@ type (
Started() <-chan struct{}
IdlewatcherConfig() *IdlewatcherConfig
HealthCheckConfig() *HealthCheckConfig
HealthCheckConfig() HealthCheckConfig
LoadBalanceConfig() *LoadBalancerConfig
HomepageItem() homepage.Item
DisplayName() string