Skip to content
Snippets Groups Projects
Commit c5773539 authored by Lukas Braun's avatar Lukas Braun
Browse files

embed faildelay.lock

parent ba908c88
No related branches found
No related tags found
No related merge requests found
...@@ -52,12 +52,8 @@ var cfg struct { ...@@ -52,12 +52,8 @@ var cfg struct {
// protect the _sending of responses_, not database access or anything else. // protect the _sending of responses_, not database access or anything else.
// The reason is that we want to avoid keeping state for non-existent users and // The reason is that we want to avoid keeping state for non-existent users and
// only after talking to the database do we know if a user exists. // only after talking to the database do we know if a user exists.
//
// Access to the map itself is synchronized by faildelay.lock. It is only
// neccessary to grab the lock for writing when a user is inserted into the map
// (i.e first login attempt), otherwise a read lock suffices.
var faildelay struct { var faildelay struct {
lock sync.RWMutex sync.RWMutex // protects userlocks
userlocks map[string]*sync.Mutex userlocks map[string]*sync.Mutex
} }
...@@ -265,20 +261,20 @@ func handle_conn(db *sql.DB, conn net.Conn) { ...@@ -265,20 +261,20 @@ func handle_conn(db *sql.DB, conn net.Conn) {
log.Printf("%v: %v", name, result) log.Printf("%v: %v", name, result)
// name exists, get or create its lock // name exists, get or create its lock
faildelay.lock.RLock() faildelay.RLock()
delay, exists := faildelay.userlocks[name] delay, exists := faildelay.userlocks[name]
faildelay.lock.RUnlock() faildelay.RUnlock()
if !exists { if !exists {
debugf("[%v] not yet in faildelay.userlocks", remote) debugf("[%v] not yet in faildelay.userlocks", remote)
// no atomic upgrade with sync.RWMutex, so we have to do the lookup again // no atomic upgrade with sync.RWMutex, so we have to do the lookup again
faildelay.lock.Lock() faildelay.Lock()
delay, exists = faildelay.userlocks[name] delay, exists = faildelay.userlocks[name]
if !exists { if !exists {
delay = new(sync.Mutex) delay = new(sync.Mutex)
faildelay.userlocks[name] = delay faildelay.userlocks[name] = delay
} }
faildelay.lock.Unlock() faildelay.Unlock()
} }
delay.Lock() delay.Lock()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment