feat: 测试交付 Q1 混沌压测骨架与 Q4 镜像骨架
This commit is contained in:
+51
-13
@@ -3,36 +3,74 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.asio.asia/nixevol/NixMsg/internal/config"
|
||||
)
|
||||
|
||||
// healthcheck 请求本机 /healthz,成功退出 0。给 Docker HEALTHCHECK 用。
|
||||
func cmdHealthcheck(_ []string) error {
|
||||
cfg, err := loadAndValidateConfig()
|
||||
cfgPath := config.PathFromEnv()
|
||||
cfg, err := config.Load(cfgPath)
|
||||
if err != nil {
|
||||
// 无配置时仍尝试 data_dir 默认值下的 listen.addr
|
||||
cfg = config.Default()
|
||||
}
|
||||
addr, err := resolveHealthAddr(cfg.DataDir, cfg.Listen)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
addrBytes, err := os.ReadFile(filepath.Join(cfg.DataDir, "listen.addr"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("read listen.addr: %w", err)
|
||||
}
|
||||
addr := strings.TrimSpace(string(addrBytes))
|
||||
if addr == "" {
|
||||
return fmt.Errorf("listen.addr is empty")
|
||||
}
|
||||
url := "http://" + addr + "/healthz"
|
||||
client := &http.Client{Timeout: 5 * time.Second}
|
||||
client := &http.Client{Timeout: 3 * time.Second}
|
||||
resp, err := client.Get(url)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("healthcheck %s: %w", url, err)
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
body, _ := io.ReadAll(io.LimitReader(resp.Body, 1024))
|
||||
_, _ = io.Copy(io.Discard, resp.Body)
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("healthz status %d: %s", resp.StatusCode, strings.TrimSpace(string(body)))
|
||||
return fmt.Errorf("healthcheck %s: status %d", url, resp.StatusCode)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func resolveHealthAddr(dataDir, listen string) (string, error) {
|
||||
path := filepath.Join(dataDir, "listen.addr")
|
||||
if b, err := os.ReadFile(path); err == nil {
|
||||
addr := strings.TrimSpace(string(b))
|
||||
if addr != "" {
|
||||
return rewriteLoopback(addr), nil
|
||||
}
|
||||
}
|
||||
if listen == "" {
|
||||
return "", fmt.Errorf("no listen.addr and empty listen")
|
||||
}
|
||||
host, port, err := net.SplitHostPort(listen)
|
||||
if err != nil {
|
||||
// listen 可能是 ":7443"
|
||||
if strings.HasPrefix(listen, ":") {
|
||||
return "127.0.0.1" + listen, nil
|
||||
}
|
||||
return "", fmt.Errorf("parse listen %q: %w", listen, err)
|
||||
}
|
||||
if host == "" || host == "0.0.0.0" || host == "::" {
|
||||
host = "127.0.0.1"
|
||||
}
|
||||
return net.JoinHostPort(host, port), nil
|
||||
}
|
||||
|
||||
func rewriteLoopback(addr string) string {
|
||||
host, port, err := net.SplitHostPort(addr)
|
||||
if err != nil {
|
||||
return addr
|
||||
}
|
||||
if host == "" || host == "0.0.0.0" || host == "::" {
|
||||
host = "127.0.0.1"
|
||||
}
|
||||
return net.JoinHostPort(host, port)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user