[FEATURE] Команды AbstractLimiter - gc,clear,addExclude,rmExclude, clearExclude

This commit is contained in:
Gravit 2019-08-25 14:30:14 +07:00
parent 82e3ae797c
commit 248450373a
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
5 changed files with 61 additions and 4 deletions

View file

@ -745,7 +745,6 @@ public LaunchServer(Path dir, boolean testEnv, String[] args) throws IOException
LogHelper.debug("Init components");
config.components.forEach((k, v) -> {
LogHelper.subDebug("Init component %s", k);
registerObject("component.".concat(k), v);
v.init(this);
});
LogHelper.debug("Init components successful");

View file

@ -6,12 +6,62 @@
import java.util.Map;
import pro.gravit.launcher.NeedGarbageCollection;
import pro.gravit.launchserver.Reconfigurable;
import pro.gravit.utils.command.Command;
import pro.gravit.utils.command.SubCommand;
import pro.gravit.utils.helper.LogHelper;
public abstract class AbstractLimiter<T> extends Component implements NeedGarbageCollection {
public abstract class AbstractLimiter<T> extends Component implements NeedGarbageCollection, Reconfigurable {
public int rateLimit;
public int rateLimitMillis;
public List<T> exclude = new ArrayList<>();
@Override
public Map<String, Command> getCommands() {
Map<String, Command> commands = new HashMap<>();
commands.put("gc", new SubCommand() {
@Override
public void invoke(String... args) throws Exception {
long size = map.size();
garbageCollection();
LogHelper.info("Cleared %d entity", size);
}
});
commands.put("clear", new SubCommand() {
@Override
public void invoke(String... args) throws Exception {
long size = map.size();
map.clear();
LogHelper.info("Cleared %d entity", size);
}
});
commands.put("addExclude", new SubCommand() {
@Override
public void invoke(String... args) throws Exception {
verifyArgs(args, 1);
exclude.add(getFromString(args[0]));
}
});
commands.put("rmExclude", new SubCommand() {
@Override
public void invoke(String... args) throws Exception {
verifyArgs(args, 1);
exclude.remove(getFromString(args[0]));
}
});
commands.put("clearExclude", new SubCommand() {
@Override
public void invoke(String... args) throws Exception {
verifyArgs(args, 1);
exclude.clear();
}
});
return commands;
}
protected abstract T getFromString(String str);
@Override
public void garbageCollection() {
long time = System.currentTimeMillis();

View file

@ -9,7 +9,7 @@
import pro.gravit.launchserver.socket.response.auth.AuthResponse;
import pro.gravit.utils.HookException;
public class AuthLimiterComponent extends AbstractLimiter<String> implements NeedGarbageCollection, AutoCloseable {
public class AuthLimiterComponent extends IPLimiter implements NeedGarbageCollection, AutoCloseable {
private transient LaunchServer srv;
@Override
public void preInit(LaunchServer launchServer) {

View file

@ -0,0 +1,8 @@
package pro.gravit.launchserver.components;
public abstract class IPLimiter extends AbstractLimiter<String> {
@Override
protected String getFromString(String str) {
return str;
}
}

View file

@ -8,7 +8,7 @@
import pro.gravit.launchserver.manangers.hook.AuthHookManager;
import pro.gravit.utils.HookException;
public class RegLimiterComponent extends AbstractLimiter<String> implements NeedGarbageCollection, AutoCloseable {
public class RegLimiterComponent extends IPLimiter implements NeedGarbageCollection, AutoCloseable {
public transient LaunchServer launchServer;
public String message;