From 50e2b909f11382f2aaf1728be451a86eec827747 Mon Sep 17 00:00:00 2001 From: P0nk Date: Mon, 28 Jun 2021 12:29:01 +0200 Subject: [PATCH] Use Instant instead of long for time measurement --- .../coordinator/login/LoginStorage.java | 68 ++++++++----------- .../session/MapleSessionCoordinator.java | 2 +- 2 files changed, 30 insertions(+), 40 deletions(-) diff --git a/src/main/java/net/server/coordinator/login/LoginStorage.java b/src/main/java/net/server/coordinator/login/LoginStorage.java index c4957f6899..d8e14e49f4 100644 --- a/src/main/java/net/server/coordinator/login/LoginStorage.java +++ b/src/main/java/net/server/coordinator/login/LoginStorage.java @@ -22,70 +22,60 @@ package net.server.coordinator.login; import config.YamlConfig; import net.server.Server; +import java.time.Instant; +import java.util.ArrayList; import java.util.Collections; -import java.util.LinkedList; import java.util.List; import java.util.Map.Entry; import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; /** * * @author Ronan */ public class LoginStorage { - - private ConcurrentHashMap> loginHistory = new ConcurrentHashMap<>(); + private final ConcurrentHashMap> loginHistory = new ConcurrentHashMap<>(); // Key: accountId public boolean registerLogin(int accountId) { - List accHist = loginHistory.get(accountId); - if (accHist == null) { - accHist = new LinkedList<>(); - loginHistory.put(accountId, accHist); - } - - synchronized (accHist) { - if (accHist.size() > YamlConfig.config.server.MAX_ACCOUNT_LOGIN_ATTEMPT) { - long blockExpiration = Server.getInstance().getCurrentTime() + YamlConfig.config.server.LOGIN_ATTEMPT_DURATION; - Collections.fill(accHist, blockExpiration); + List attempts = loginHistory.computeIfAbsent(accountId, k -> new ArrayList<>()); + synchronized (attempts) { + final Instant attemptExpiry = Instant.ofEpochMilli(Server.getInstance().getCurrentTime() + YamlConfig.config.server.LOGIN_ATTEMPT_DURATION); + + if (attempts.size() > YamlConfig.config.server.MAX_ACCOUNT_LOGIN_ATTEMPT) { + Collections.fill(attempts, attemptExpiry); return false; } - accHist.add(Server.getInstance().getCurrentTime() + YamlConfig.config.server.LOGIN_ATTEMPT_DURATION); + attempts.add(attemptExpiry); return true; } } - public void updateLoginHistory() { - long timeNow = Server.getInstance().getCurrentTime(); - List toRemove = new LinkedList<>(); - List toRemoveAttempt = new LinkedList<>(); - - for (Entry> loginEntries : loginHistory.entrySet()) { - toRemoveAttempt.clear(); - - List accAttempts = loginEntries.getValue(); - synchronized (accAttempts) { - for (Long loginAttempt : accAttempts) { - if (loginAttempt < timeNow) { - toRemoveAttempt.add(loginAttempt); - } + public void clearExpiredAttempts() { + final Instant now = Instant.ofEpochMilli(Server.getInstance().getCurrentTime()); + List accountIdsToClear = new ArrayList<>(); + + for (Entry> loginEntries : loginHistory.entrySet()) { + final List attempts = loginEntries.getValue(); + synchronized (attempts) { + List attemptsToRemove = attempts.stream() + .filter(attempt -> attempt.isBefore(now)) + .collect(Collectors.toList()); + + for (Instant attemptToRemove : attemptsToRemove) { + attempts.remove(attemptToRemove); } - if (!toRemoveAttempt.isEmpty()) { - for (Long trAttempt : toRemoveAttempt) { - accAttempts.remove(trAttempt); - } - - if (accAttempts.isEmpty()) { - toRemove.add(loginEntries.getKey()); - } + if (attempts.isEmpty()) { + accountIdsToClear.add(loginEntries.getKey()); } } } - - for (Integer tr : toRemove) { - loginHistory.remove(tr); + + for (Integer accountId : accountIdsToClear) { + loginHistory.remove(accountId); } } } diff --git a/src/main/java/net/server/coordinator/session/MapleSessionCoordinator.java b/src/main/java/net/server/coordinator/session/MapleSessionCoordinator.java index 07fca506a0..dc1e45ca7b 100644 --- a/src/main/java/net/server/coordinator/session/MapleSessionCoordinator.java +++ b/src/main/java/net/server/coordinator/session/MapleSessionCoordinator.java @@ -399,7 +399,7 @@ public class MapleSessionCoordinator { } public void runUpdateLoginHistory() { - loginStorage.updateLoginHistory(); + loginStorage.clearExpiredAttempts(); } public void printSessionTrace() {