Skip to content

Commit

Permalink
[FAB-4515] Fix concurrency issue with enroll
Browse files Browse the repository at this point in the history
There was an issue with the logic being used
to update the state of a user (which
reflects the number of times the user
has enrolled).  In the case of MySQL, this
resulted in the error in the [FAB-4515] but
also allowed for a user to exceed max
enrollments when using sqlite3 as the
database.

The fix is pretty straightforward - it moves
the logic into the SQL statement in order to
avoid / depend on locks and or the concurrency
mode set for a database.

Change-Id: I06980375d2928850b390e6c644911e2d5b200f9c
Signed-off-by: Gari Singh <gari.r.singh@gmail.com>
  • Loading branch information
mastersingh24 committed Jun 11, 2017
1 parent ef110bc commit 1424b33
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/dbaccessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@ func (u *DBUser) GetName() string {

// Login the user with a password
func (u *DBUser) Login(pass string, caMaxEnrollments int) error {
var stateUpdateSQL string
var args []interface{}

log.Debugf("DB: Login user %s with max enrollments of %d and state of %d", u.Name, u.MaxEnrollments, u.State)

// Check the password
Expand Down Expand Up @@ -371,7 +374,16 @@ func (u *DBUser) Login(pass string, caMaxEnrollments int) error {

// Not exceeded, so attempt to increment the count
state := u.State + 1
res, err := u.db.Exec(u.db.Rebind("UPDATE users SET state = ? WHERE (id = ?)"), state, u.Name)
args = append(args, u.Name)
if u.MaxEnrollments == -1 {
// unlimited so no state check
stateUpdateSQL = "UPDATE users SET state = state + 1 WHERE (id = ?)"
} else {
// state must be less than max enrollments
stateUpdateSQL = "UPDATE users SET state = state + 1 WHERE (id = ? AND state < ?)"
args = append(args, u.MaxEnrollments)
}
res, err := u.db.Exec(u.db.Rebind(stateUpdateSQL), args...)
if err != nil {
return fmt.Errorf("Failed to update state of identity %s to %d: %s", u.Name, state, err)
}
Expand Down

0 comments on commit 1424b33

Please sign in to comment.