Manager: fix race condition in logging of worker name/UUID
Instead of updating the logger in the context, just store a new logger in a new sub-context.
This commit is contained in:
parent
21a294a267
commit
a368230afa
@ -9,7 +9,6 @@ import (
|
|||||||
oapi_middle "github.com/deepmap/oapi-codegen/pkg/middleware"
|
oapi_middle "github.com/deepmap/oapi-codegen/pkg/middleware"
|
||||||
"github.com/getkin/kin-openapi/openapi3filter"
|
"github.com/getkin/kin-openapi/openapi3filter"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/rs/zerolog"
|
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
|
||||||
"git.blender.org/flamenco/internal/manager/persistence"
|
"git.blender.org/flamenco/internal/manager/persistence"
|
||||||
@ -65,19 +64,14 @@ func requestWorkerStore(e echo.Context, w *persistence.Worker) {
|
|||||||
req := e.Request()
|
req := e.Request()
|
||||||
reqCtx := context.WithValue(req.Context(), workerKey, w)
|
reqCtx := context.WithValue(req.Context(), workerKey, w)
|
||||||
|
|
||||||
// Take copies now to avoid race conditions later.
|
|
||||||
wUUID := w.UUID
|
|
||||||
wName := w.Name
|
|
||||||
|
|
||||||
// Update the logger in this context to reflect the Worker.
|
// Update the logger in this context to reflect the Worker.
|
||||||
l := zerolog.Ctx(reqCtx)
|
logger := requestLogger(e).With().
|
||||||
l.UpdateContext(func(c zerolog.Context) zerolog.Context {
|
Str("wUUID", w.UUID).
|
||||||
return c.
|
Str("wName", w.Name).
|
||||||
Str("wUUID", wUUID).
|
Logger()
|
||||||
Str("wName", wName)
|
|
||||||
})
|
|
||||||
|
|
||||||
e.SetRequest(req.WithContext(reqCtx))
|
newCtx := logger.WithContext(reqCtx)
|
||||||
|
e.SetRequest(req.WithContext(newCtx))
|
||||||
}
|
}
|
||||||
|
|
||||||
// requestWorker returns the Worker associated with this HTTP request, or nil if there is none.
|
// requestWorker returns the Worker associated with this HTTP request, or nil if there is none.
|
||||||
|
@ -5,10 +5,9 @@ package api_impl
|
|||||||
import (
|
import (
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func requestLogger(e echo.Context) zerolog.Logger {
|
func requestLogger(e echo.Context) zerolog.Logger {
|
||||||
logger := log.Ctx(e.Request().Context())
|
logger := zerolog.Ctx(e.Request().Context())
|
||||||
return *logger
|
return *logger
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user