Добавить X-Instance-Id, задержку drain при SIGTERM и отдельный запуск миграций
This commit is contained in:
@@ -20,8 +20,9 @@ const (
|
||||
)
|
||||
|
||||
// NewEngine builds an engine whose every response, including routing
|
||||
// failures and panics, carries X-Request-Id and the contract error body.
|
||||
func NewEngine(log *slog.Logger, maxBodyBytes int64) *gin.Engine {
|
||||
// failures and panics, carries X-Instance-Id, X-Request-Id and the contract
|
||||
// error body.
|
||||
func NewEngine(log *slog.Logger, maxBodyBytes int64, instanceID string) *gin.Engine {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
e := gin.New()
|
||||
// Redirects would answer with text/plain and without a request id.
|
||||
@@ -29,7 +30,7 @@ func NewEngine(log *slog.Logger, maxBodyBytes int64) *gin.Engine {
|
||||
e.RedirectFixedPath = false
|
||||
e.HandleMethodNotAllowed = true
|
||||
|
||||
e.Use(requestID(log), accessLog(), recovery(), bodyLimit(maxBodyBytes))
|
||||
e.Use(instance(instanceID), requestID(log), accessLog(), recovery(), bodyLimit(maxBodyBytes))
|
||||
e.NoRoute(func(c *gin.Context) { Error(c, apperr.NotFound("route not found")) })
|
||||
e.NoMethod(func(c *gin.Context) { Fail(c, http.StatusMethodNotAllowed, "invalid_request", "method not allowed") })
|
||||
return e
|
||||
|
||||
@@ -33,6 +33,8 @@ type payload struct {
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
const testInstance = "0123456789abcdef"
|
||||
|
||||
func newEngine(t *testing.T, logs *bytes.Buffer) *gin.Engine {
|
||||
t.Helper()
|
||||
return newEngineAt(t, logs, slog.LevelDebug)
|
||||
@@ -44,7 +46,7 @@ func newEngineAt(t *testing.T, logs *bytes.Buffer, level slog.Level) *gin.Engine
|
||||
logs = &bytes.Buffer{}
|
||||
}
|
||||
log := slog.New(slog.NewJSONHandler(logs, &slog.HandlerOptions{Level: level}))
|
||||
e := httpx.NewEngine(log, 64)
|
||||
e := httpx.NewEngine(log, 64, testInstance)
|
||||
e.GET("/ok", func(c *gin.Context) { httpx.JSON(c, http.StatusOK, gin.H{"text": "<a&b>"}) })
|
||||
e.GET("/panic", func(c *gin.Context) { panic("secret detail") })
|
||||
e.GET("/fail", func(c *gin.Context) { httpx.Error(c, errors.New("db password leaked")) })
|
||||
@@ -106,6 +108,25 @@ func assertError(t *testing.T, rec *httptest.ResponseRecorder, eb errorBody, sta
|
||||
}
|
||||
}
|
||||
|
||||
// The balancer spreads one client's requests over the replicas; the header
|
||||
// is how a client tells which process answered, so no response may lack it.
|
||||
func TestEveryResponseCarriesInstanceID(t *testing.T) {
|
||||
e := newEngine(t, nil)
|
||||
for _, r := range []struct{ method, path string }{
|
||||
{http.MethodGet, "/ok"},
|
||||
{http.MethodGet, "/missing"},
|
||||
{http.MethodDelete, "/ok"},
|
||||
{http.MethodGet, "/panic"},
|
||||
{http.MethodGet, "/fail"},
|
||||
{http.MethodPost, "/decode"},
|
||||
} {
|
||||
rec, _ := do(t, e, r.method, r.path, "", nil)
|
||||
if got := rec.Header().Get("X-Instance-Id"); got != testInstance {
|
||||
t.Errorf("%s %s (status %d): X-Instance-Id = %q, want %q", r.method, r.path, rec.Code, got, testInstance)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnknownRouteIsJSON404(t *testing.T) {
|
||||
rec, eb := do(t, newEngine(t, nil), http.MethodGet, "/nope", "", nil)
|
||||
assertError(t, rec, eb, http.StatusNotFound, "not_found")
|
||||
|
||||
@@ -13,6 +13,15 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// instance names the process that answered, so a client behind the load
|
||||
// balancer can tell the replicas apart.
|
||||
func instance(id string) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
c.Writer.Header().Set("X-Instance-Id", id)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// requestID assigns a fresh identifier to every request.
|
||||
//
|
||||
// An incoming X-Request-Id is deliberately not reused. This service is
|
||||
|
||||
Reference in New Issue
Block a user