Skip to content

Commit

Permalink
Switch to sync.RWMutex (#10)
Browse files Browse the repository at this point in the history
In order to allow atomic reloads for passwd and group files this libraries uses
mutexes to prevent lookups during reloads. In the current form this also means
that concurrent lookups are not possible. This change replaces sync.Mutex with
sync.RWMutex which allows concurrent read access.

Signed-off-by: Christian Pointner <[email protected]>
  • Loading branch information
equinox0815 committed Nov 11, 2023
1 parent bcdfb95 commit c172617
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions htgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type userGroupMap map[string][]string
// A HTGroup encompasses an Apache-style group file.
type HTGroup struct {
filePath string
mutex sync.Mutex
mutex sync.RWMutex
userGroups userGroupMap
}

Expand Down Expand Up @@ -123,9 +123,9 @@ func (htGroup *HTGroup) IsUserInGroup(user string, group string) bool {
// GetUserGroups reads all groups of a user.
// Returns all groups as a string array or an empty array.
func (htGroup *HTGroup) GetUserGroups(user string) []string {
htGroup.mutex.Lock()
htGroup.mutex.RLock()
groups := htGroup.userGroups[user]
htGroup.mutex.Unlock()
htGroup.mutex.RUnlock()

if groups == nil {
return []string{}
Expand Down
6 changes: 3 additions & 3 deletions htpasswd.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type BadLineHandler func(err error)
// An File encompasses an Apache-style htpasswd file for HTTP Basic authentication
type File struct {
filePath string
mutex sync.Mutex
mutex sync.RWMutex
passwds passwdTable
parsers []PasswdParser
}
Expand Down Expand Up @@ -104,9 +104,9 @@ func NewFromReader(r io.Reader, parsers []PasswdParser, bad BadLineHandler) (*Fi
// Match checks the username and password combination to see if it represents
// a valid account from the htpassword file.
func (bf *File) Match(username, password string) bool {
bf.mutex.Lock()
bf.mutex.RLock()
matcher, ok := bf.passwds[username]
bf.mutex.Unlock()
bf.mutex.RUnlock()

if ok && matcher.MatchesPassword(password) {
// we are good
Expand Down

0 comments on commit c172617

Please sign in to comment.