mirror of
https://github.com/zitadel/zitadel.git
synced 2025-12-07 07:16:54 +00:00
fix: use a single translator for middleware (#10633)
# Which Problems Are Solved
Comparing the v3 and v4 deployments we noticed an increase in memory
usage. A first analysis revealed that it might be related to the
(multiple) initialization of the `i18n.Translator`, partially related
# How the Problems Are Solved
Initialize the tranlator once (apart from the translator interceptor,
which uses context / request specific information) and pass it to all
necessary middleware.
# Additional Changes
Removed unnecessary error return parameter from the translator
initialization.
# Additional Context
- noticed internally
- backport to v4.x
(cherry picked from commit a0c3ccecf7)
This commit is contained in:
@@ -9,7 +9,6 @@ import (
|
||||
"strconv"
|
||||
|
||||
"github.com/zitadel/logging"
|
||||
"golang.org/x/text/language"
|
||||
|
||||
http_util "github.com/zitadel/zitadel/internal/api/http"
|
||||
zhttp_middleware "github.com/zitadel/zitadel/internal/api/http/middleware"
|
||||
@@ -67,24 +66,22 @@ const (
|
||||
ScimTypeUniqueness scimErrorType = "uniqueness"
|
||||
)
|
||||
|
||||
var translator *i18n.Translator
|
||||
func ErrorHandler(translator *i18n.Translator) func(next zhttp_middleware.HandlerFuncWithError) http.Handler {
|
||||
return func(next zhttp_middleware.HandlerFuncWithError) http.Handler {
|
||||
var err error
|
||||
|
||||
func ErrorHandler(next zhttp_middleware.HandlerFuncWithError) http.Handler {
|
||||
var err error
|
||||
translator, err = i18n.NewZitadelTranslator(language.English)
|
||||
logging.OnError(err).Panic("unable to get translator")
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if err = next(w, r); err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if err = next(w, r); err == nil {
|
||||
return
|
||||
}
|
||||
scimErr := MapToScimError(r.Context(), translator, err)
|
||||
w.WriteHeader(scimErr.StatusCode)
|
||||
|
||||
scimErr := MapToScimError(r.Context(), err)
|
||||
w.WriteHeader(scimErr.StatusCode)
|
||||
|
||||
jsonErr := json.NewEncoder(w).Encode(scimErr)
|
||||
logging.OnError(jsonErr).Warn("Failed to marshal scim error response")
|
||||
})
|
||||
jsonErr := json.NewEncoder(w).Encode(scimErr)
|
||||
logging.OnError(jsonErr).Warn("Failed to marshal scim error response")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func ThrowInvalidValue(parent error) error {
|
||||
@@ -146,7 +143,7 @@ func (err *wrappedScimError) Error() string {
|
||||
return fmt.Sprintf("SCIM Error: %s: %s", err.ScimType, err.Parent.Error())
|
||||
}
|
||||
|
||||
func MapToScimError(ctx context.Context, err error) *ScimError {
|
||||
func MapToScimError(ctx context.Context, translator *i18n.Translator, err error) *ScimError {
|
||||
scimError := new(ScimError)
|
||||
if ok := errors.As(err, &scimError); ok {
|
||||
return scimError
|
||||
@@ -154,7 +151,7 @@ func MapToScimError(ctx context.Context, err error) *ScimError {
|
||||
|
||||
scimWrappedError := new(wrappedScimError)
|
||||
if ok := errors.As(err, &scimWrappedError); ok {
|
||||
mappedErr := MapToScimError(ctx, scimWrappedError.Parent)
|
||||
mappedErr := MapToScimError(ctx, translator, scimWrappedError.Parent)
|
||||
if scimWrappedError.ScimType != "" {
|
||||
mappedErr.ScimType = scimWrappedError.ScimType
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"golang.org/x/text/language"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/i18n"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
@@ -97,9 +98,10 @@ func TestErrorHandler(t *testing.T) {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req := httptest.NewRequest("GET", "/", nil)
|
||||
recorder := httptest.NewRecorder()
|
||||
ErrorHandler(func(http.ResponseWriter, *http.Request) error {
|
||||
return tt.err
|
||||
}).ServeHTTP(recorder, req)
|
||||
ErrorHandler(i18n.NewZitadelTranslator(language.English))(
|
||||
func(http.ResponseWriter, *http.Request) error {
|
||||
return tt.err
|
||||
}).ServeHTTP(recorder, req)
|
||||
assert.Equal(t, tt.wantStatus, recorder.Code)
|
||||
|
||||
if tt.wantBody != "" {
|
||||
|
||||
Reference in New Issue
Block a user