refactor(healthz): скрыть Server за интерфейсом
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -29,7 +29,7 @@ var (
|
|||||||
) {
|
) {
|
||||||
return cron.NewCronScheduler(expr, job)
|
return cron.NewCronScheduler(expr, job)
|
||||||
}
|
}
|
||||||
newHealthz = func(port int) (*healthz.Server, error) {
|
newHealthz = func(port int) (healthz.Server, error) {
|
||||||
return healthz.New(port)
|
return healthz.New(port)
|
||||||
}
|
}
|
||||||
runOnceFunc = backup.RunOnce
|
runOnceFunc = backup.RunOnce
|
||||||
|
|||||||
@@ -9,23 +9,34 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Server is a minimal HTTP health check server.
|
// Server is the public interface of the minimal HTTP health check server.
|
||||||
type Server struct {
|
type Server interface {
|
||||||
|
// Addr returns the bound network address (e.g. "127.0.0.1:8080").
|
||||||
|
Addr() string
|
||||||
|
// Start begins serving HTTP requests. It blocks until Stop is called.
|
||||||
|
Start() error
|
||||||
|
// Stop initiates graceful shutdown. After Stop is called the /healthz
|
||||||
|
// endpoint returns 503 while in-flight requests complete.
|
||||||
|
Stop(ctx context.Context) error
|
||||||
|
}
|
||||||
|
|
||||||
|
// server is the private implementation of Server.
|
||||||
|
type server struct {
|
||||||
listener net.Listener
|
listener net.Listener
|
||||||
server *http.Server
|
httpServer *http.Server
|
||||||
shuttingDown atomic.Bool
|
shuttingDown atomic.Bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a health check server listening on the given port.
|
// New creates a health check server listening on the given port.
|
||||||
// Passing port 0 binds to an available ephemeral port.
|
// Passing port 0 binds to an available ephemeral port.
|
||||||
func New(port int) (*Server, error) {
|
func New(port int) (Server, error) {
|
||||||
listener, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
|
listener, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to create listener: %w", err)
|
return nil, fmt.Errorf("failed to create listener: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
s := &Server{listener: listener}
|
s := &server{listener: listener}
|
||||||
s.server = &http.Server{
|
s.httpServer = &http.Server{
|
||||||
Handler: http.HandlerFunc(s.handleHealthz),
|
Handler: http.HandlerFunc(s.handleHealthz),
|
||||||
ReadHeaderTimeout: 5 * time.Second,
|
ReadHeaderTimeout: 5 * time.Second,
|
||||||
}
|
}
|
||||||
@@ -33,7 +44,7 @@ func New(port int) (*Server, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Addr returns the bound network address (e.g. "127.0.0.1:8080").
|
// Addr returns the bound network address (e.g. "127.0.0.1:8080").
|
||||||
func (s *Server) Addr() string {
|
func (s *server) Addr() string {
|
||||||
if s.listener == nil {
|
if s.listener == nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
@@ -41,21 +52,21 @@ func (s *Server) Addr() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Start begins serving HTTP requests. It blocks until Stop is called.
|
// Start begins serving HTTP requests. It blocks until Stop is called.
|
||||||
func (s *Server) Start() error {
|
func (s *server) Start() error {
|
||||||
return s.server.Serve(s.listener)
|
return s.httpServer.Serve(s.listener)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop initiates graceful shutdown. After Stop is called the /healthz
|
// Stop initiates graceful shutdown. After Stop is called the /healthz
|
||||||
// endpoint returns 503 while in-flight requests complete.
|
// endpoint returns 503 while in-flight requests complete.
|
||||||
func (s *Server) Stop(ctx context.Context) error {
|
func (s *server) Stop(ctx context.Context) error {
|
||||||
s.shuttingDown.Store(true)
|
s.shuttingDown.Store(true)
|
||||||
// Grace period so that health checks can observe the 503 state
|
// Grace period so that health checks can observe the 503 state
|
||||||
// before the listener is closed.
|
// before the listener is closed.
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
return s.server.Shutdown(ctx)
|
return s.httpServer.Shutdown(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) handleHealthz(w http.ResponseWriter, r *http.Request) {
|
func (s *server) handleHealthz(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.URL.Path != "/healthz" {
|
if r.URL.Path != "/healthz" {
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user