chore: inMemoryAuthenticator unneed sync map

This commit is contained in:
wwqgtxx 2023-10-24 21:25:03 +08:00
parent 8755618910
commit e1e999180a

View file

@ -1,9 +1,5 @@
package auth package auth
import (
"github.com/puzpuzpuz/xsync/v2"
)
type Authenticator interface { type Authenticator interface {
Verify(user string, pass string) bool Verify(user string, pass string) bool
Users() []string Users() []string
@ -15,12 +11,12 @@ type AuthUser struct {
} }
type inMemoryAuthenticator struct { type inMemoryAuthenticator struct {
storage *xsync.MapOf[string, string] storage map[string]string
usernames []string usernames []string
} }
func (au *inMemoryAuthenticator) Verify(user string, pass string) bool { func (au *inMemoryAuthenticator) Verify(user string, pass string) bool {
realPass, ok := au.storage.Load(user) realPass, ok := au.storage[user]
return ok && realPass == pass return ok && realPass == pass
} }
@ -30,17 +26,13 @@ func NewAuthenticator(users []AuthUser) Authenticator {
if len(users) == 0 { if len(users) == 0 {
return nil return nil
} }
au := &inMemoryAuthenticator{
au := &inMemoryAuthenticator{storage: xsync.NewMapOf[string]()} storage: make(map[string]string),
for _, user := range users { usernames: make([]string, 0, len(users)),
au.storage.Store(user.User, user.Pass) }
for _, user := range users {
au.storage[user.User] = user.Pass
au.usernames = append(au.usernames, user.User)
} }
usernames := make([]string, 0, len(users))
au.storage.Range(func(key string, value string) bool {
usernames = append(usernames, key)
return true
})
au.usernames = usernames
return au return au
} }