chore: inMemoryAuthenticator unneed sync map

This commit is contained in:
gVisor bot 2023-10-24 21:25:03 +08:00
parent 32e923442a
commit 8bfd92ea7d

View File

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