refactor(handler/autologin): use sync.Map for cache

This commit is contained in:
Trong Huu Nguyen
2023-02-14 14:20:35 +01:00
parent 5a56c24bcc
commit d17feacc34

View File

@@ -18,8 +18,7 @@ var DefaultIgnorePatterns = []string{
type AutoLogin struct {
Enabled bool
IgnorePatterns []string
cache map[string]bool
lock sync.Mutex
cache sync.Map
}
func (a *AutoLogin) NeedsLogin(r *http.Request, isAuthenticated bool) bool {
@@ -27,9 +26,6 @@ func (a *AutoLogin) NeedsLogin(r *http.Request, isAuthenticated bool) bool {
return false
}
a.lock.Lock()
defer a.lock.Unlock()
path := r.URL.Path
if !strings.HasPrefix(path, "/") {
path = "/" + path
@@ -39,19 +35,19 @@ func (a *AutoLogin) NeedsLogin(r *http.Request, isAuthenticated bool) bool {
path = strings.TrimSuffix(path, "/")
}
if result, found := a.cache[path]; found {
return result
if result, found := a.cache.Load(path); found {
return result.(bool)
}
for _, pattern := range a.IgnorePatterns {
match, _ := doublestar.Match(pattern, path)
if match {
a.cache[path] = false
a.cache.Store(path, false)
return false
}
}
a.cache[path] = true
a.cache.Store(path, true)
return true
}
@@ -77,6 +73,6 @@ func New(cfg *config.Config) (*AutoLogin, error) {
return &AutoLogin{
Enabled: cfg.AutoLogin,
IgnorePatterns: patterns,
cache: make(map[string]bool),
cache: sync.Map{},
}, nil
}