Skip to content

Commit

Permalink
Improve proxy performance a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Oct 2, 2023
1 parent ad58a14 commit 0eba0ce
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions src/main/java/net/pistonmaster/serverwrecker/AttackManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public class AttackManager {
@Setter
private AttackState attackState = AttackState.STOPPED;

@SuppressWarnings("UnstableApiUsage")
public CompletableFuture<Void> start(SettingsHolder settingsHolder) {
if (!attackState.isStopped()) {
throw new IllegalStateException("Attack is already running");
Expand Down Expand Up @@ -132,7 +131,7 @@ public CompletableFuture<Void> start(SettingsHolder settingsHolder) {

Queue<BotConnectionFactory> factories = new ArrayBlockingQueue<>(botAmount);
for (int botId = 1; botId <= botAmount; botId++) {
SWProxy proxyData = getProxy(botsPerProxy, proxyUseMap);
SWProxy proxyData = getProxy(botsPerProxy, proxyUseMap).orElse(null);
MinecraftAccount minecraftAccount = getAccount(accountSettings, accounts, botId);

// AuthData will be used internally instead of the MCProtocol data
Expand Down Expand Up @@ -203,21 +202,20 @@ private MinecraftAccount getAccount(AccountSettings accountSettings, List<Minecr
return accounts.remove(0);
}

private SWProxy getProxy(int accountsPerProxy, Map<SWProxy, Integer> proxyUseMap) {
private Optional<SWProxy> getProxy(int accountsPerProxy, Map<SWProxy, Integer> proxyUseMap) {
if (proxyUseMap.isEmpty()) {
return null; // No proxies available
} else {
SWProxy selectedProxy = proxyUseMap.entrySet().stream()
.filter(entry -> accountsPerProxy == -1 || entry.getValue() < accountsPerProxy)
.min(Comparator.comparingInt(Map.Entry::getValue))
.map(Map.Entry::getKey)
.orElseThrow(() -> new IllegalStateException("No proxies available!")); // Should never happen
return Optional.empty(); // No proxies available
}

// Always present
proxyUseMap.computeIfPresent(selectedProxy, (proxy, useCount) -> useCount + 1);
var selectedProxy = proxyUseMap.entrySet().stream()
.filter(entry -> accountsPerProxy == -1 || entry.getValue() < accountsPerProxy)
.min(Comparator.comparingInt(Map.Entry::getValue))
.orElseThrow(() -> new IllegalStateException("No proxies available!")); // Should never happen

return selectedProxy;
}
// Always present
selectedProxy.setValue(selectedProxy.getValue() + 1);

return Optional.of(selectedProxy.getKey());
}

public CompletableFuture<Void> stop() {
Expand Down

0 comments on commit 0eba0ce

Please sign in to comment.