Modernize agent I/O with CompletableFuture and BufferedReader.#79
Draft
DomiKoPL wants to merge 1 commit intoCodinGame:masterfrom
Draft
Modernize agent I/O with CompletableFuture and BufferedReader.#79DomiKoPL wants to merge 1 commit intoCodinGame:masterfrom
DomiKoPL wants to merge 1 commit intoCodinGame:masterfrom
Conversation
- Replace busy-wait polling loop in RefereeAgent.getOutput() with CompletableFuture + orTimeout for precise, non-blocking timeouts - Use a dedicated single-thread ExecutorService (AGENT_IO_EXECUTOR) for blocking I/O to avoid ForkJoinPool starvation - Switch to BufferedReader (readLine) instead of raw InputStream reads - Remove unused processStdout InputStream field and abstract getOutputStream() method — only the BufferedReader is needed now - Deduplicate timeout logic: RefereeAgent now delegates to Agent.getOutput() instead of reimplementing it - Clean up unused imports (InputStreamReader, TimeoutException)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
During the last contest we observed a high timeout rate, especially during league openings, the final stretch, and the rerun. Players at the top of Legend got eliminated by timeouts that were clearly caused by platform load rather than their own code.
One likely contributor is the polling loop in
RefereeAgent.getOutput(), which usedInputStream.available()to check whether data was ready to read, sleeping 1 ms between checks.available()only returns bytes already in the kernel buffer — it is not aware of bytes that are in transit or buffered elsewhere. Under server load, this may cause the loop to spin past the timeout deadline even when the bot had already responded in time, producing spurious timeouts.See also: #78 (soft-timeout approach tackling the same problem).
Changes
CompletableFuture.orTimeout— replaces theavailable()busy-wait loop with a blockingBufferedReader.readLine()offloaded to a dedicated I/O thread. The future times out precisely aftertimeoutms regardless of server load.ExecutorService(AGENT_IO_EXECUTOR) — a single daemon thread handles all blocking reads, avoidingForkJoinPoolstarvation thatCompletableFuture.supplyAsyncwith the common pool would risk.BufferedReaderinstead of raw InputStream byte-reads — simpler line-based reading replaces the manual byte-by-byte loop with CR/LF bookkeeping.processStdout/getOutputStream()— the raw InputStream field is no longer needed; only theBufferedReaderis used.RefereeAgent.getOutput()— ~50 lines of duplicated timing logic removed; now delegates toAgent.getOutput()with the appropriate buffer size limit.