chore: refactor; unify vault mutex usage with wrappers;

This commit is contained in:
Atanas Janeshliev
2025-07-01 13:20:23 +02:00
parent 26cc6169fa
commit e9b1befae4
2 changed files with 7 additions and 28 deletions

View File

@ -30,10 +30,7 @@ import (
// If CertPEMPath is set, it will attempt to read the certificate from the file. // If CertPEMPath is set, it will attempt to read the certificate from the file.
// Otherwise, or on read/validation failure, it will return the certificate from the vault. // Otherwise, or on read/validation failure, it will return the certificate from the vault.
func (vault *Vault) GetBridgeTLSCert() ([]byte, []byte) { func (vault *Vault) GetBridgeTLSCert() ([]byte, []byte) {
vault.lock.RLock() certs := vault.getSafe().Certs
defer vault.lock.RUnlock()
certs := vault.getUnsafe().Certs
if certPath, keyPath := certs.CustomCertPath, certs.CustomKeyPath; certPath != "" && keyPath != "" { if certPath, keyPath := certs.CustomCertPath, certs.CustomKeyPath; certPath != "" && keyPath != "" {
if certPEM, keyPEM, err := readPEMCert(certPath, keyPath); err == nil { if certPEM, keyPEM, err := readPEMCert(certPath, keyPath); err == nil {

View File

@ -88,10 +88,7 @@ func New(vaultDir, gluonCacheDir string, key []byte, panicHandler async.PanicHan
// GetUserIDs returns the user IDs and usernames of all users in the vault. // GetUserIDs returns the user IDs and usernames of all users in the vault.
func (vault *Vault) GetUserIDs() []string { func (vault *Vault) GetUserIDs() []string {
vault.lock.RLock() return xslices.Map(vault.getSafe().Users, func(user UserData) string {
defer vault.lock.RUnlock()
return xslices.Map(vault.getUnsafe().Users, func(user UserData) string {
return user.UserID return user.UserID
}) })
} }
@ -140,10 +137,7 @@ func (vault *Vault) getUsers() ([]*User, error) {
// HasUser returns true if the vault contains a user with the given ID. // HasUser returns true if the vault contains a user with the given ID.
func (vault *Vault) HasUser(userID string) bool { func (vault *Vault) HasUser(userID string) bool {
vault.lock.RLock() return xslices.IndexFunc(vault.getSafe().Users, func(user UserData) bool {
defer vault.lock.RUnlock()
return xslices.IndexFunc(vault.getUnsafe().Users, func(user UserData) bool {
return user.UserID == userID return user.UserID == userID
}) >= 0 }) >= 0
} }
@ -299,26 +293,17 @@ func (vault *Vault) DeleteUser(userID string) error {
} }
func (vault *Vault) Migrated() bool { func (vault *Vault) Migrated() bool {
vault.lock.RLock() return vault.getSafe().Migrated
defer vault.lock.RUnlock()
return vault.getUnsafe().Migrated
} }
func (vault *Vault) SetMigrated() error { func (vault *Vault) SetMigrated() error {
vault.lock.Lock() return vault.modSafe(func(data *Data) {
defer vault.lock.Unlock()
return vault.modUnsafe(func(data *Data) {
data.Migrated = true data.Migrated = true
}) })
} }
func (vault *Vault) Reset(gluonDir string) error { func (vault *Vault) Reset(gluonDir string) error {
vault.lock.Lock() return vault.modSafe(func(data *Data) {
defer vault.lock.Unlock()
return vault.modUnsafe(func(data *Data) {
*data = newDefaultData(gluonDir) *data = newDefaultData(gluonDir)
}) })
} }
@ -459,10 +444,7 @@ func (vault *Vault) modUnsafe(fn func(data *Data)) error {
} }
func (vault *Vault) getUser(userID string) UserData { func (vault *Vault) getUser(userID string) UserData {
vault.lock.RLock() users := vault.getSafe().Users
defer vault.lock.RUnlock()
users := vault.getUnsafe().Users
idx := xslices.IndexFunc(users, func(user UserData) bool { idx := xslices.IndexFunc(users, func(user UserData) bool {
return user.UserID == userID return user.UserID == userID