Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(auth): validate user through cache to avoid duplicate hash calculation #1460

Merged
merged 2 commits into from
May 20, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class StandardAuthManager implements AuthManager {
private final HugeGraphParams graph;
private final EventListener eventListener;
private final Cache<Id, HugeUser> usersCache;
private final Cache<Id, String> pwdCache;

private final EntityManager<HugeUser> users;
private final EntityManager<HugeGroup> groups;
Expand All @@ -59,6 +60,7 @@ public StandardAuthManager(HugeGraphParams graph) {
this.graph = graph;
this.eventListener = this.listenChanges();
this.usersCache = this.cache("users");
this.pwdCache = this.cache("users_pwd");

this.users = new EntityManager<>(this.graph, HugeUser.P.USER,
HugeUser::fromVertex);
Expand All @@ -73,9 +75,9 @@ public StandardAuthManager(HugeGraphParams graph) {
HugeAccess::fromEdge);
}

private Cache<Id, HugeUser> cache(String prefix) {
private <V> Cache<Id, V> cache(String prefix) {
String name = prefix + "-" + this.graph.name();
Cache<Id, HugeUser> cache = CacheManager.instance().cache(name);
Cache<Id, V> cache = CacheManager.instance().cache(name);
cache.expire(CACHE_EXPIRE);
return cache;
}
Expand Down Expand Up @@ -120,6 +122,7 @@ private void initSchemaIfNeeded() {

private void invalidCache() {
this.usersCache.clear();
this.pwdCache.clear();
}

@Override
Expand Down Expand Up @@ -341,8 +344,17 @@ public HugeUser matchUser(String name, String password) {
E.checkArgumentNotNull(name, "User name can't be null");
E.checkArgumentNotNull(password, "User password can't be null");
HugeUser user = this.findUser(name);
if (user != null &&
StringEncoding.checkPassword(password, user.password())) {
if (user == null) {
return null;
}

Id id = IdGenerator.of(user.id());
if (password.equals(pwdCache.get(id))) {
return user;
}

if (StringEncoding.checkPassword(password, user.password())) {
pwdCache.update(id, password);
return user;
}
return null;
Expand Down