Skip to content

Commit

Permalink
fix(fiberi18n): use sync.Map and utils.CopyString to fix concurrent p…
Browse files Browse the repository at this point in the history
…roblem
  • Loading branch information
Skyenought committed Aug 22, 2023
1 parent f574b80 commit 5d1eac5
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 46 deletions.
2 changes: 1 addition & 1 deletion casbin/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/gofiber/contrib/casbin
go 1.18

require (
github.com/casbin/casbin/v2 v2.75.0
github.com/casbin/casbin/v2 v2.75.1
github.com/gofiber/fiber/v2 v2.48.0
)

Expand Down
4 changes: 2 additions & 2 deletions casbin/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible h1
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/casbin/casbin/v2 v2.75.0 h1:vSgtloFgyijYrFAoMKH1u9vdT7R55WDU74hJDDWT+5w=
github.com/casbin/casbin/v2 v2.75.0/go.mod h1:mzGx0hYW9/ksOSpw3wNjk3NRAroq5VMFYUQ6G43iGPk=
github.com/casbin/casbin/v2 v2.75.1 h1:TZpaeiIgS5jl6zhtwBCcYwpsSGdV63JbKiXsRE8b0k4=
github.com/casbin/casbin/v2 v2.75.1/go.mod h1:mzGx0hYW9/ksOSpw3wNjk3NRAroq5VMFYUQ6G43iGPk=
github.com/gofiber/fiber/v2 v2.48.0 h1:cRVMCb9aUJDsyHxGFLwz/sGzDggdailZZyptU9F9cU0=
github.com/gofiber/fiber/v2 v2.48.0/go.mod h1:xqJgfqrc23FJuqGOW6DVgi3HyZEm2Mn9pRqUb2kHSX8=
github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc=
Expand Down
11 changes: 6 additions & 5 deletions fiberi18n/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package fiberi18n

import (
"os"
"sync"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
"github.com/nicksnyder/go-i18n/v2/i18n"
"golang.org/x/text/language"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -52,7 +54,7 @@ type Config struct {

ctx *fiber.Ctx
bundle *i18n.Bundle
localizerMap map[string]*i18n.Localizer
localizerMap *sync.Map
}

type Loader interface {
Expand All @@ -77,18 +79,16 @@ var ConfigDefault = &Config{

func defaultLangHandler(c *fiber.Ctx, defaultLang string) string {
var lang string
lang = c.Query("lang")
lang = utils.CopyString(c.Query("lang"))
if lang != "" {
return lang
}

lang = c.Get("Accept-Language")
lang = utils.CopyString(c.Get("Accept-Language"))
if lang != "" {
return lang
}

return defaultLang

}

func configDefault(config ...*Config) *Config {
Expand Down Expand Up @@ -135,5 +135,6 @@ func configDefault(config ...*Config) *Config {
if cfg.LangHandler == nil {
cfg.LangHandler = ConfigDefault.LangHandler
}

return cfg
}
69 changes: 34 additions & 35 deletions fiberi18n/i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fiberi18n
import (
"fmt"
"path"
"sync"

"github.com/gofiber/fiber/v2"
"github.com/nicksnyder/go-i18n/v2/i18n"
Expand All @@ -13,59 +14,56 @@ var appCfg *Config
// New creates a new middleware handler
func New(config ...*Config) fiber.Handler {
appCfg = configDefault(config...)
appCfg.bundle = initBundle()
loadMessages()
appCfg.localizerMap = initLocalizerMap()
// init bundle
bundle := i18n.NewBundle(appCfg.DefaultLanguage)
bundle.RegisterUnmarshalFunc(appCfg.FormatBundleFile, appCfg.UnmarshalFunc)
appCfg.bundle = bundle

appCfg.loadMessages().initLocalizerMap()

return func(c *fiber.Ctx) error {
if appCfg.Next != nil && appCfg.Next(c) {
return c.Next()
}

appCfg.ctx = c

return c.Next()
}
}

func initBundle() *i18n.Bundle {
bundle := i18n.NewBundle(appCfg.DefaultLanguage)
bundle.RegisterUnmarshalFunc(appCfg.FormatBundleFile, appCfg.UnmarshalFunc)

return bundle
}

func loadMessage(filepath string) {
buf, err := appCfg.Loader.LoadMessage(filepath)
func (c *Config) loadMessage(filepath string) {
buf, err := c.Loader.LoadMessage(filepath)
if err != nil {
panic(err)
}
if _, err := appCfg.bundle.ParseMessageFileBytes(buf, filepath); err != nil {
if _, err := c.bundle.ParseMessageFileBytes(buf, filepath); err != nil {
panic(err)
}
}

func loadMessages() {
for _, lang := range appCfg.AcceptLanguages {
bundleFile := fmt.Sprintf("%s.%s", lang.String(), appCfg.FormatBundleFile)
filepath := path.Join(appCfg.RootPath, bundleFile)

loadMessage(filepath)
func (c *Config) loadMessages() *Config {
for _, lang := range c.AcceptLanguages {
bundleFilePath := fmt.Sprintf("%s.%s", lang.String(), c.FormatBundleFile)
filepath := path.Join(c.RootPath, bundleFilePath)
c.loadMessage(filepath)
}
return c
}

func initLocalizerMap() map[string]*i18n.Localizer {
localizerMap := map[string]*i18n.Localizer{}
func (c *Config) initLocalizerMap() {
localizerMap := &sync.Map{}

for _, lang := range appCfg.AcceptLanguages {
for _, lang := range c.AcceptLanguages {
s := lang.String()
localizerMap[s] = i18n.NewLocalizer(appCfg.bundle, s)
localizerMap.Store(s, i18n.NewLocalizer(c.bundle, s))
}

lang := appCfg.DefaultLanguage.String()
if _, ok := localizerMap[lang]; !ok {
localizerMap[lang] = i18n.NewLocalizer(appCfg.bundle, lang)
lang := c.DefaultLanguage.String()
if _, ok := localizerMap.Load(lang); !ok {
localizerMap.Store(lang, i18n.NewLocalizer(c.bundle, lang))
}

return localizerMap
c.localizerMap = localizerMap
}

/*
Expand Down Expand Up @@ -100,24 +98,25 @@ GetMessage get the i18n message
})
*/
func GetMessage(params interface{}) (string, error) {
var localizeConfig *i18n.LocalizeConfig

lang := appCfg.LangHandler(appCfg.ctx, appCfg.DefaultLanguage.String())
localizer, hasValue := appCfg.localizerMap[lang]
if !hasValue {
localizer = appCfg.localizerMap[appCfg.DefaultLanguage.String()]

localizer, _ := appCfg.localizerMap.Load(lang)
if localizer == nil {
defaultLang := appCfg.DefaultLanguage.String()
localizer, _ = appCfg.localizerMap.Load(defaultLang)
}

var localizeConfig *i18n.LocalizeConfig
switch paramValue := params.(type) {
case string:
localizeConfig = &i18n.LocalizeConfig{MessageID: paramValue}
case *i18n.LocalizeConfig:
localizeConfig = paramValue
}

message, err := localizer.Localize(localizeConfig)
message, err := localizer.(*i18n.Localizer).Localize(localizeConfig)
if err != nil {
return "", fmt.Errorf("i18n.Localize error: %v", err.Error())
return "", fmt.Errorf("i18n.Localize error: %v", err)
}
return message, nil
}
2 changes: 1 addition & 1 deletion paseto/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/gofiber/fiber/v2 v2.48.0
github.com/google/uuid v1.3.0
github.com/google/uuid v1.3.1
github.com/o1egl/paseto v1.0.0
golang.org/x/crypto v0.12.0
)
Expand Down
4 changes: 2 additions & 2 deletions paseto/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gofiber/fiber/v2 v2.48.0 h1:cRVMCb9aUJDsyHxGFLwz/sGzDggdailZZyptU9F9cU0=
github.com/gofiber/fiber/v2 v2.48.0/go.mod h1:xqJgfqrc23FJuqGOW6DVgi3HyZEm2Mn9pRqUb2kHSX8=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/klauspost/compress v1.16.3 h1:XuJt9zzcnaz6a16/OU53ZjWp/v7/42WcR5t2a0PcNQY=
github.com/klauspost/compress v1.16.3/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
Expand Down

0 comments on commit 5d1eac5

Please sign in to comment.