Перенести решение лабораторной 1 как основу для лабораторной 2
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
// Package server runs the HTTP listener with the lifecycle the lab
|
||||
// requires: readiness drops first, the listener stops accepting, in-flight
|
||||
// requests drain within a bounded timeout, then the process exits.
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"messenger/internal/app"
|
||||
)
|
||||
|
||||
// Run serves until ctx is cancelled, then shuts down within shutdownTimeout.
|
||||
func Run(ctx context.Context, log *slog.Logger, ln net.Listener, a *app.App, shutdownTimeout time.Duration) error {
|
||||
srv := &http.Server{
|
||||
Handler: a.Engine,
|
||||
ReadHeaderTimeout: 5 * time.Second,
|
||||
ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
IdleTimeout: 60 * time.Second,
|
||||
MaxHeaderBytes: 64 * 1024,
|
||||
ErrorLog: slog.NewLogLogger(log.Handler(), slog.LevelWarn),
|
||||
}
|
||||
|
||||
served := make(chan error, 1)
|
||||
go func() {
|
||||
err := srv.Serve(ln)
|
||||
if errors.Is(err, http.ErrServerClosed) {
|
||||
err = nil
|
||||
}
|
||||
served <- err
|
||||
}()
|
||||
log.Info("listening", "addr", ln.Addr().String())
|
||||
|
||||
select {
|
||||
case err := <-served:
|
||||
return err
|
||||
case <-ctx.Done():
|
||||
}
|
||||
|
||||
log.Info("shutdown started", "timeout", shutdownTimeout.String())
|
||||
a.Health.SetReady(false)
|
||||
drain, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
|
||||
defer cancel()
|
||||
if err := srv.Shutdown(drain); err != nil {
|
||||
log.Error("shutdown forced", "error", err.Error())
|
||||
_ = srv.Close()
|
||||
return err
|
||||
}
|
||||
<-served
|
||||
log.Info("shutdown completed")
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package server_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/internal/app"
|
||||
"messenger/internal/server"
|
||||
)
|
||||
|
||||
func TestServesUntilContextCancelledThenStopsCleanly(t *testing.T) {
|
||||
log := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
a := app.New(log, 1024)
|
||||
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
done := make(chan error, 1)
|
||||
go func() { done <- server.Run(ctx, log, ln, a, 5*time.Second) }()
|
||||
|
||||
base := "http://" + ln.Addr().String()
|
||||
waitFor(t, base+"/health/ready", http.StatusOK)
|
||||
|
||||
cancel()
|
||||
select {
|
||||
case err := <-done:
|
||||
if err != nil {
|
||||
t.Fatalf("Run returned error: %v", err)
|
||||
}
|
||||
case <-time.After(10 * time.Second):
|
||||
t.Fatal("Run did not stop within 10s after cancel")
|
||||
}
|
||||
if _, err := http.Get(base + "/health/live"); err == nil {
|
||||
t.Fatal("listener must be closed after shutdown")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadinessDropsBeforeListenerCloses(t *testing.T) {
|
||||
log := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
a := app.New(log, 1024)
|
||||
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
done := make(chan error, 1)
|
||||
go func() { done <- server.Run(ctx, log, ln, a, 5*time.Second) }()
|
||||
base := "http://" + ln.Addr().String()
|
||||
waitFor(t, base+"/health/ready", http.StatusOK)
|
||||
|
||||
// Hold one request open so shutdown has something to drain.
|
||||
slow := make(chan struct{})
|
||||
go func() {
|
||||
resp, err := http.Get(base + "/health/live")
|
||||
if err == nil {
|
||||
resp.Body.Close()
|
||||
}
|
||||
close(slow)
|
||||
}()
|
||||
|
||||
cancel()
|
||||
<-done
|
||||
<-slow
|
||||
if !a.Health.IsReady() {
|
||||
return
|
||||
}
|
||||
t.Fatal("readiness must be switched off during shutdown")
|
||||
}
|
||||
|
||||
func waitFor(t *testing.T, url string, status int) {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
resp, err := http.Get(url)
|
||||
if err == nil {
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode == status {
|
||||
return
|
||||
}
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
t.Fatalf("%s did not return %d in time", url, status)
|
||||
}
|
||||
Reference in New Issue
Block a user