Skip to content

Commit ea54a88

Browse files
renichAntigravity
authored andcommitted
fix(validator): release lock on fallback and resolve regional Accept-Language
- Move regional validator registration into registerRegionalValidator so the write lock is released on every path. A failing RegisterFunc previously left the lock held and the default-language fallback then blocked on RLock. - Resolve Accept-Language through a language.Matcher built from the configured language options, so regional variants such as es-MX, pt-AO or zh-HK reach the closest configured locale instead of falling back to en_US. Unmatched languages still default to English. - Add tests for the deadlock regression and the middleware resolution table. Co-authored-by: Antigravity <antigravity@google.com> Signed-off-by: Rénich Bon Ćirić <renich@evalinux.com>
1 parent f512eeb commit ea54a88

5 files changed

Lines changed: 221 additions & 25 deletions

File tree

‎internal/base/middleware/accept_language.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ func ExtractAndSetAcceptLanguage(ctx *gin.Context) {
5454
}
5555
}
5656

57+
// A regional variant such as es-MX or zh-HK resolves to the closest configured language.
58+
if matched, ok := translator.MatchLanguage(tag[0]); ok {
59+
ctx.Set(constant.AcceptLanguageFlag, i18n.Language(matched))
60+
return
61+
}
62+
5763
// default language
5864
ctx.Set(constant.AcceptLanguageFlag, i18n.LanguageEnglish)
5965
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package middleware
21+
22+
import (
23+
"net/http"
24+
"net/http/httptest"
25+
"strings"
26+
"testing"
27+
28+
"github.com/apache/answer/internal/base/constant"
29+
"github.com/apache/answer/internal/base/translator"
30+
"github.com/gin-gonic/gin"
31+
"github.com/segmentfault/pacman/i18n"
32+
"github.com/stretchr/testify/assert"
33+
)
34+
35+
func resolveAcceptLanguage(t *testing.T, header string) i18n.Language {
36+
t.Helper()
37+
gin.SetMode(gin.TestMode)
38+
39+
var resolved i18n.Language
40+
router := gin.New()
41+
router.Use(ExtractAndSetAcceptLanguage)
42+
router.GET("/", func(ctx *gin.Context) {
43+
value, _ := ctx.Get(constant.AcceptLanguageFlag)
44+
resolved, _ = value.(i18n.Language)
45+
ctx.Status(http.StatusNoContent)
46+
})
47+
48+
req := httptest.NewRequest(http.MethodGet, "/", nil)
49+
if header != "" {
50+
req.Header.Set(constant.AcceptLanguageFlag, header)
51+
}
52+
router.ServeHTTP(httptest.NewRecorder(), req)
53+
return resolved
54+
}
55+
56+
func TestExtractAndSetAcceptLanguage(t *testing.T) {
57+
original := translator.LanguageOptions
58+
translator.SetLanguageOptions([]*translator.LangOption{
59+
{Label: "Default", Value: translator.DefaultLangOption},
60+
{Label: "English", Value: "en_US"},
61+
{Label: "Español", Value: "es_ES"},
62+
{Label: "Português do Brasil", Value: "pt_BR"},
63+
{Label: "Português", Value: "pt_PT"},
64+
{Label: "简体中文", Value: "zh_CN"},
65+
{Label: "繁體中文", Value: "zh_TW"},
66+
})
67+
t.Cleanup(func() { translator.SetLanguageOptions(original) })
68+
69+
tests := []struct {
70+
name string
71+
header string
72+
want i18n.Language
73+
}{
74+
{"exact configured locale", "es-ES", "es_ES"},
75+
{"exact locale with quality", "pt-BR,pt;q=0.8,en;q=0.5", "pt_BR"},
76+
{"regional Spanish falls back to configured Spanish", "es-MX", "es_ES"},
77+
{"Latin American Spanish", "es-419", "es_ES"},
78+
{"regional Portuguese prefers European Portuguese", "pt-AO", "pt_PT"},
79+
{"Hong Kong Chinese resolves to Traditional", "zh-HK", "zh_TW"},
80+
{"Singapore Chinese resolves to Simplified", "zh-SG", "zh_CN"},
81+
{"regional English", "en-GB", "en_US"},
82+
{"unsupported language uses English", "tlh", "en_US"},
83+
{"empty header uses English", "", "en_US"},
84+
{"malformed header uses English", "!!!", "en_US"},
85+
{"oversized header uses English", strings.Repeat("es-MX,", 100), "en_US"},
86+
}
87+
for _, tt := range tests {
88+
t.Run(tt.name, func(t *testing.T) {
89+
assert.Equal(t, tt.want, resolveAcceptLanguage(t, tt.header))
90+
})
91+
}
92+
}

‎internal/base/translator/provider.go‎

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ import (
2525
"path/filepath"
2626
"sort"
2727
"strings"
28+
"sync/atomic"
2829

2930
"github.com/google/wire"
3031
myTran "github.com/segmentfault/pacman/contrib/i18n"
3132
"github.com/segmentfault/pacman/i18n"
3233
"github.com/segmentfault/pacman/log"
34+
"golang.org/x/text/language"
3335
"gopkg.in/yaml.v3"
3436
)
3537

@@ -51,8 +53,55 @@ const DefaultLangOption = "Default"
5153
var (
5254
// LanguageOptions language
5355
LanguageOptions []*LangOption
56+
57+
// languageMatcher resolves regional language tags to a configured language option.
58+
languageMatcher atomic.Pointer[languageMatch]
5459
)
5560

61+
// languageMatch pairs a language matcher with the option values it was built from,
62+
// so the matcher index maps back to the configured value such as es_ES.
63+
type languageMatch struct {
64+
matcher language.Matcher
65+
values []string
66+
}
67+
68+
// SetLanguageOptions sets the configured language options and rebuilds the matcher
69+
// used by MatchLanguage.
70+
func SetLanguageOptions(options []*LangOption) {
71+
LanguageOptions = options
72+
73+
values := make([]string, 0, len(options))
74+
tags := make([]language.Tag, 0, len(options))
75+
for _, option := range options {
76+
if option.Value == DefaultLangOption {
77+
continue
78+
}
79+
tag, err := language.Parse(strings.ReplaceAll(option.Value, "_", "-"))
80+
if err != nil {
81+
log.Warnf("language option %q is not a valid language tag: %s", option.Value, err)
82+
continue
83+
}
84+
values = append(values, option.Value)
85+
tags = append(tags, tag)
86+
}
87+
languageMatcher.Store(&languageMatch{matcher: language.NewMatcher(tags), values: values})
88+
}
89+
90+
// MatchLanguage returns the configured language option that best matches the given
91+
// tag, for example es_ES for es-MX, pt_PT for pt-AO or zh_TW for zh-HK. It reports
92+
// false when no configured language is a plausible match for the tag.
93+
func MatchLanguage(tag language.Tag) (string, bool) {
94+
match := languageMatcher.Load()
95+
if match == nil || len(match.values) == 0 {
96+
return "", false
97+
}
98+
_, idx, confidence := match.matcher.Match(tag)
99+
if confidence == language.No || idx < 0 || idx >= len(match.values) {
100+
return "", false
101+
}
102+
return match.values[idx], true
103+
}
104+
56105
// NewTranslator new a translator
57106
func NewTranslator(c *I18n) (tr i18n.Translator, err error) {
58107
entries, err := os.ReadDir(c.BundleDir)
@@ -120,10 +169,10 @@ func NewTranslator(c *I18n) (tr i18n.Translator, err error) {
120169
if err != nil {
121170
return nil, fmt.Errorf("i18n file parsing failed: %s", err)
122171
}
123-
LanguageOptions = s.LangOption
124-
for _, option := range LanguageOptions {
172+
for _, option := range s.LangOption {
125173
option.Label = fmt.Sprintf("%s (%d%%)", option.Label, option.Progress)
126174
}
175+
SetLanguageOptions(s.LangOption)
127176
return GlobalTrans, err
128177
}
129178

‎internal/base/validator/validator.go‎

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,37 @@ func createDefaultValidator(la i18n.Language) *validator.Validate {
183183
return validate
184184
}
185185

186+
// baseLanguage returns the language subtag of a locale such as pt_BR or es-MX.
187+
func baseLanguage(lang string) string {
188+
if idx := strings.IndexAny(lang, "_-"); idx > 0 {
189+
return lang[:idx]
190+
}
191+
return lang
192+
}
193+
194+
// registerRegionalValidator builds and caches a validator for a regional locale
195+
// (such as es_MX) on top of the translator of its base language. The write lock
196+
// is scoped to this call so callers never hold it while falling back. It returns
197+
// nil if the translations cannot be registered.
198+
func registerRegionalValidator(lang i18n.Language, t *TranslatorLocal) *MyValidator {
199+
validatorMappingMu.Lock()
200+
defer validatorMappingMu.Unlock()
201+
202+
if v := GlobalValidatorMapping[lang]; v != nil {
203+
return v
204+
}
205+
tran, val := getTran(t.Lo), createDefaultValidator(lang)
206+
if t.RegisterFunc != nil {
207+
if err := t.RegisterFunc(val, tran); err != nil {
208+
log.Error(err)
209+
return nil
210+
}
211+
}
212+
v := &MyValidator{Validate: val, Tran: tran, Lang: lang}
213+
GlobalValidatorMapping[lang] = v
214+
return v
215+
}
216+
186217
func GetValidatorByLang(lang i18n.Language) *MyValidator {
187218
validatorMappingMu.RLock()
188219
if v := GlobalValidatorMapping[lang]; v != nil {
@@ -191,33 +222,15 @@ func GetValidatorByLang(lang i18n.Language) *MyValidator {
191222
}
192223
validatorMappingMu.RUnlock()
193224

194-
baseLang := string(lang)
195-
if idx := strings.IndexAny(baseLang, "_-"); idx > 0 {
196-
baseLang = baseLang[:idx]
197-
}
198-
225+
baseLang := baseLanguage(string(lang))
199226
for _, t := range allLanguageTranslators {
200-
tBase := string(t.La)
201-
if idx := strings.IndexAny(tBase, "_-"); idx > 0 {
202-
tBase = tBase[:idx]
227+
if !strings.EqualFold(baseLang, baseLanguage(string(t.La))) {
228+
continue
203229
}
204-
if strings.EqualFold(baseLang, tBase) {
205-
validatorMappingMu.Lock()
206-
defer validatorMappingMu.Unlock()
207-
if v := GlobalValidatorMapping[lang]; v != nil {
208-
return v
209-
}
210-
tran, val := getTran(t.Lo), createDefaultValidator(lang)
211-
if t.RegisterFunc != nil {
212-
if err := t.RegisterFunc(val, tran); err != nil {
213-
log.Error(err)
214-
break
215-
}
216-
}
217-
v := &MyValidator{Validate: val, Tran: tran, Lang: lang}
218-
GlobalValidatorMapping[lang] = v
230+
if v := registerRegionalValidator(lang, t); v != nil {
219231
return v
220232
}
233+
break
221234
}
222235

223236
validatorMappingMu.RLock()

‎internal/base/validator/validator_test.go‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@
2020
package validator
2121

2222
import (
23+
"errors"
2324
"strings"
2425
"testing"
26+
"time"
2527
"unicode/utf8"
2628

29+
"github.com/go-playground/locales/en"
30+
ut "github.com/go-playground/universal-translator"
31+
"github.com/go-playground/validator/v10"
2732
"github.com/segmentfault/pacman/i18n"
2833
"github.com/stretchr/testify/assert"
2934
"github.com/stretchr/testify/require"
@@ -59,6 +64,37 @@ func TestGetValidatorByLang_RegionalLocaleFallback(t *testing.T) {
5964
assert.Contains(t, errFieldsPT[0].ErrorMsg, "máximo")
6065
}
6166

67+
// A failing RegisterFunc must release the write lock before the fallback takes
68+
// the read lock, otherwise the lookup deadlocks.
69+
func TestGetValidatorByLang_RegisterFuncFailureDoesNotDeadlock(t *testing.T) {
70+
failing := &TranslatorLocal{
71+
La: i18n.Language("xx_XX"),
72+
Lo: en.New(),
73+
RegisterFunc: func(*validator.Validate, ut.Translator) error {
74+
return errors.New("register failed")
75+
},
76+
}
77+
original := allLanguageTranslators
78+
allLanguageTranslators = append(append([]*TranslatorLocal{}, original...), failing)
79+
t.Cleanup(func() { allLanguageTranslators = original })
80+
81+
result := make(chan *MyValidator, 1)
82+
go func() { result <- GetValidatorByLang(i18n.Language("xx_YY")) }()
83+
84+
select {
85+
case v := <-result:
86+
require.NotNil(t, v)
87+
assert.Equal(t, i18n.DefaultLanguage, v.Lang)
88+
case <-time.After(5 * time.Second):
89+
t.Fatal("GetValidatorByLang deadlocked after a RegisterFunc failure")
90+
}
91+
92+
validatorMappingMu.RLock()
93+
_, cached := GlobalValidatorMapping[i18n.Language("xx_YY")]
94+
validatorMappingMu.RUnlock()
95+
assert.False(t, cached, "a failed registration must not be cached")
96+
}
97+
6298
func TestCheck_MultiByteLatinCapitalization(t *testing.T) {
6399
req := &sampleAccentedFieldReq{Email: "not-an-email"}
64100

0 commit comments

Comments
 (0)