Рыба проекта. Минимальная функциональность
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package healthz
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Server is a minimal HTTP health check server.
|
||||
type Server struct {
|
||||
listener net.Listener
|
||||
server *http.Server
|
||||
shuttingDown atomic.Bool
|
||||
}
|
||||
|
||||
// New creates a health check server listening on the given port.
|
||||
// Passing port 0 binds to an available ephemeral port.
|
||||
func New(port int) (*Server, error) {
|
||||
listener, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create listener: %w", err)
|
||||
}
|
||||
|
||||
s := &Server{listener: listener}
|
||||
s.server = &http.Server{
|
||||
Handler: http.HandlerFunc(s.handleHealthz),
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// Addr returns the bound network address (e.g. "127.0.0.1:8080").
|
||||
func (s *Server) Addr() string {
|
||||
if s.listener == nil {
|
||||
return ""
|
||||
}
|
||||
return s.listener.Addr().String()
|
||||
}
|
||||
|
||||
// Start begins serving HTTP requests. It blocks until Stop is called.
|
||||
func (s *Server) Start() error {
|
||||
return s.server.Serve(s.listener)
|
||||
}
|
||||
|
||||
// Stop initiates graceful shutdown. After Stop is called the /healthz
|
||||
// endpoint returns 503 while in-flight requests complete.
|
||||
func (s *Server) Stop(ctx context.Context) error {
|
||||
s.shuttingDown.Store(true)
|
||||
// Grace period so that health checks can observe the 503 state
|
||||
// before the listener is closed.
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
return s.server.Shutdown(ctx)
|
||||
}
|
||||
|
||||
func (s *Server) handleHealthz(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/healthz" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if s.shuttingDown.Load() {
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package healthz
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestHealthzReturns200WhileAlive(t *testing.T) {
|
||||
srv, err := New(0)
|
||||
if err != nil {
|
||||
t.Fatalf("New failed: %v", err)
|
||||
}
|
||||
|
||||
go func() {
|
||||
if err := srv.Start(); err != nil && err != http.ErrServerClosed {
|
||||
t.Errorf("Start returned unexpected error: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
// Give the server a moment to start listening.
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
|
||||
url := fmt.Sprintf("http://%s/healthz", srv.Addr())
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
t.Fatalf("GET /healthz failed: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Errorf("/healthz status = %d, want %d", resp.StatusCode, http.StatusOK)
|
||||
}
|
||||
|
||||
if err := srv.Stop(context.Background()); err != nil {
|
||||
t.Fatalf("Stop failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHealthzReturns503WhileShuttingDown(t *testing.T) {
|
||||
srv, err := New(0)
|
||||
if err != nil {
|
||||
t.Fatalf("New failed: %v", err)
|
||||
}
|
||||
|
||||
go func() {
|
||||
if err := srv.Start(); err != nil && err != http.ErrServerClosed {
|
||||
t.Errorf("Start returned unexpected error: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
|
||||
// Initiate shutdown but don't wait for it to finish.
|
||||
shutdownCtx, cancel := context.WithCancel(context.Background())
|
||||
go func() {
|
||||
_ = srv.Stop(shutdownCtx)
|
||||
}()
|
||||
|
||||
// Give the shutdown flag time to flip.
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
|
||||
url := fmt.Sprintf("http://%s/healthz", srv.Addr())
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
t.Fatalf("GET /healthz during shutdown failed: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusServiceUnavailable {
|
||||
t.Errorf("/healthz status during shutdown = %d, want %d", resp.StatusCode, http.StatusServiceUnavailable)
|
||||
}
|
||||
|
||||
cancel()
|
||||
}
|
||||
Reference in New Issue
Block a user