-
-
Notifications
You must be signed in to change notification settings - Fork 417
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
fix skipping over pingers #1086
base: master
Are you sure you want to change the base?
Conversation
📝 WalkthroughWalkthroughThe Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant ServerFinderScreen
participant Server
User->>ServerFinderScreen: Initiate server search
ServerFinderScreen->>Server: Ping servers
alt Server found
ServerFinderScreen->>User: Display server list
else UnknownHostException
ServerFinderScreen->>User: Show error UNKNOWN_HOST
else Other Exception
ServerFinderScreen->>User: Show error ERROR
end
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Outside diff range and nitpick comments (3)
src/main/java/net/wurstclient/serverfinder/ServerFinderScreen.java (3)
Line range hint
89-117
: Security: Consider implementing rate limitingThe server scanning functionality could potentially be misused for network scanning or DDoS attacks. Consider implementing:
- Rate limiting to prevent excessive connection attempts
- Cooldown period between scans
- Maximum scan range limits
- More appropriate server naming convention than "Grief me #"
Line range hint
89-117
: Performance: Consider using ExecutorService for better thread managementThe current implementation creates threads without proper resource management. Consider:
- Using ExecutorService/ThreadPoolExecutor instead of raw threads
- Implementing proper shutdown handling
- Adding timeout mechanism for unresponsive servers
Example implementation:
private final ExecutorService executor = Executors.newFixedThreadPool(maxThreads); private void findServers() { try { // ... existing IP resolution code ... List<Future<?>> futures = new ArrayList<>(); for (int change : changes) { for (int i2 = 0; i2 <= 255; i2++) { if (state == ServerFinderState.CANCELLED) { executor.shutdownNow(); return; } // ... existing IP construction code ... futures.add(executor.submit(() -> { WurstServerPinger pinger = new WurstServerPinger(); pinger.ping(ip); return pinger; })); } } // Wait for completion with timeout executor.shutdown(); if (!executor.awaitTermination(5, TimeUnit.MINUTES)) { executor.shutdownNow(); } } catch (Exception e) { executor.shutdownNow(); // ... existing error handling ... } }Also applies to: 144-166
Line range hint
144-166
: Improve error handling and user feedbackThe current error handling implementation could be enhanced:
- Use more specific exception types instead of catching generic Exception
- Provide more meaningful error messages to users
- Ensure proper cleanup in error cases
- Add logging for debugging
try { // ... existing code ... } catch(UnknownHostException e) { state = ServerFinderState.UNKNOWN_HOST; + LOGGER.warn("Unable to resolve host: " + ipBox.getText(), e); } catch(Exception e) { - e.printStackTrace(); state = ServerFinderState.ERROR; + LOGGER.error("Server finder encountered an error", e); + // Cleanup + pingers.forEach(WurstServerPinger::cancel); + pingers.clear(); }
@@ -200,6 +200,7 @@ private void updatePingers(ArrayList<WurstServerPinger> pingers) | |||
} | |||
} | |||
pingers.remove(i); | |||
i -= 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider using removeIf for safer collection modification
While the fix correctly handles the index adjustment after removal, modifying collections during iteration is generally discouraged. Consider refactoring to use removeIf
for a cleaner and safer implementation:
private void updatePingers(ArrayList<WurstServerPinger> pingers)
{
- for(int i = 0; i < pingers.size(); i++)
- if(!pingers.get(i).isStillPinging())
- {
- checked++;
- if(pingers.get(i).isWorking())
- {
- working++;
-
- if(!isServerInList(pingers.get(i).getServerIP()))
- {
- prevScreen.getServerList()
- .add(new ServerInfo("Grief me #" + working,
- pingers.get(i).getServerIP(), ServerType.OTHER),
- false);
- prevScreen.getServerList().saveFile();
- ((IMultiplayerScreen)prevScreen).getServerListSelector()
- .setSelected(null);
- ((IMultiplayerScreen)prevScreen).getServerListSelector()
- .setServers(prevScreen.getServerList());
- }
- }
- pingers.remove(i);
- i -= 1;
- }
+ pingers.removeIf(pinger -> {
+ if (!pinger.isStillPinging()) {
+ checked++;
+ if (pinger.isWorking()) {
+ working++;
+ if (!isServerInList(pinger.getServerIP())) {
+ prevScreen.getServerList()
+ .add(new ServerInfo("Grief me #" + working,
+ pinger.getServerIP(), ServerType.OTHER),
+ false);
+ prevScreen.getServerList().saveFile();
+ ((IMultiplayerScreen)prevScreen).getServerListSelector()
+ .setSelected(null);
+ ((IMultiplayerScreen)prevScreen).getServerListSelector()
+ .setServers(prevScreen.getServerList());
+ }
+ }
+ return true;
+ }
+ return false;
+ });
}
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
i -= 1; | |
private void updatePingers(ArrayList<WurstServerPinger> pingers) | |
{ | |
pingers.removeIf(pinger -> { | |
if (!pinger.isStillPinging()) { | |
checked++; | |
if (pinger.isWorking()) { | |
working++; | |
if (!isServerInList(pinger.getServerIP())) { | |
prevScreen.getServerList() | |
.add(new ServerInfo("Grief me #" + working, | |
pinger.getServerIP(), ServerType.OTHER), | |
false); | |
prevScreen.getServerList().saveFile(); | |
((IMultiplayerScreen)prevScreen).getServerListSelector() | |
.setSelected(null); | |
((IMultiplayerScreen)prevScreen).getServerListSelector() | |
.setServers(prevScreen.getServerList()); | |
} | |
} | |
return true; | |
} | |
return false; | |
}); | |
} |
I think I should really look into rewriting this class one day... |
Description
Fixed the loop in
updatePingers()
not decrementing a counter when a pinger instance is removed. (Why not just use removeIf though?)Testing
Patch was tested separately from the project, works fine.