Skip to content

Commit

Permalink
Substitude std::map with folly::ConcurrentHashMap
Browse files Browse the repository at this point in the history
  • Loading branch information
Aiee committed Jan 5, 2022
1 parent e4923e2 commit 4b0efef
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions src/clients/meta/MetaClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2421,13 +2421,10 @@ Status MetaClient::authCheckFromCache(const std::string& account, const std::str
return Status::Error("User not exist");
}

folly::RWSpinLock::WriteHolder holder(localCacheLock_);
auto lockedSince = userLoginLockTime_[account];
auto passwordAttemtRemain = userPasswordAttemptsRemain_[account];

auto& lockedSince = userLoginLockTime_[account];
auto& passwordAttemtRemain = userPasswordAttemptsRemain_[account];
LOG(INFO) << "Thread id: " << std::this_thread::get_id()
<< " ,passwordAttemtRemain: " << passwordAttemtRemain;
// lockedSince is non-zero means the account has been locked
// If lockedSince is non-zero, it means the account has been locked
if (lockedSince != 0) {
auto remainingLockTime =
(lockedSince + FLAGS_password_lock_time_in_secs) - time::WallClock::fastNowInSec();
Expand All @@ -2441,8 +2438,9 @@ Status MetaClient::authCheckFromCache(const std::string& account, const std::str
remainingLockTime);
}
// Clear lock state and reset attempts
lockedSince = 0;
passwordAttemtRemain = FLAGS_failed_login_attempts;
userLoginLockTime_.assign_if_equal(account, lockedSince, 0);
userPasswordAttemptsRemain_.assign_if_equal(
account, passwordAttemtRemain, FLAGS_failed_login_attempts);
}

if (iter->second != password) {
Expand All @@ -2453,27 +2451,29 @@ Status MetaClient::authCheckFromCache(const std::string& account, const std::str

// If the password is not correct and passwordAttemtRemain > 0,
// Allow another attemp
passwordAttemtRemain = userPasswordAttemptsRemain_[account];
if (passwordAttemtRemain > 0) {
--passwordAttemtRemain;
if (passwordAttemtRemain == 0) {
auto newAttemtRemain = passwordAttemtRemain - 1;
userPasswordAttemptsRemain_.assign_if_equal(account, passwordAttemtRemain, newAttemtRemain);
if (newAttemtRemain == 0) {
// If the remaining attemps is 0, failed to authenticate
// Block user login
lockedSince = time::WallClock::fastNowInSec();
userLoginLockTime_.assign_if_equal(account, 0, time::WallClock::fastNowInSec());
return Status::Error(
"%d times consecutive incorrect passwords has been input, user name: %s has been "
"locked, try again in %d seconds",
FLAGS_failed_login_attempts,
account.c_str(),
FLAGS_password_lock_time_in_secs);
}
LOG(ERROR) << "Invalid password, remaining attempts: " << passwordAttemtRemain;
return Status::Error("Invalid password, remaining attempts: %d", passwordAttemtRemain);
LOG(ERROR) << "Invalid password, remaining attempts: " << newAttemtRemain;
return Status::Error("Invalid password, remaining attempts: %d", newAttemtRemain);
}
}

// Reset password attempts
passwordAttemtRemain = FLAGS_failed_login_attempts;
lockedSince = 0;
// Authentication succeed, reset password attempts
userPasswordAttemptsRemain_.assign(account, FLAGS_failed_login_attempts);
userLoginLockTime_.assign(account, 0);
return Status::OK();
}

Expand Down

0 comments on commit 4b0efef

Please sign in to comment.