Repository navigation
fix(net): hold a connect to a full accept queue until accept makes room, as Linux does #89
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a576837
fix(net): hold a connect to a full accept queue until accept makes ro…
Project516 8fa4ef4
test(jtreg): run HttpGetInCancelledFuture
Project516 4b29df3
fix(net): leave a connect unheld when several listeners share its port
Project516 aa97346
docs(net): note the backlog + 1 queue size where a connection is queued
Project516 5e8a026
fix(net): hold a connect only when it goes to this host, not to a rem…
Project516 ef97aaa
docs(net): say why the connect pairing is registered on the next tick
Project516 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| import java.io.IOException; | ||
| import java.net.*; | ||
| import java.nio.channels.*; | ||
| import java.util.*; | ||
| import java.util.concurrent.*; | ||
|
|
||
| public class ListenBacklog { | ||
| static final InetAddress LO = InetAddress.getLoopbackAddress(); | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| fullQueueTimesOut(); | ||
| nonBlockingBurst(); | ||
| acceptMakesRoom(); | ||
| listenerCloseRefuses(); | ||
| } | ||
|
|
||
| static String connect(int port, int timeoutMs, List<Socket> keep) { | ||
| Socket s = new Socket(); | ||
| try { | ||
| s.connect(new InetSocketAddress(LO, port), timeoutMs); | ||
| keep.add(s); | ||
| return "ok"; | ||
| } catch (SocketTimeoutException e) { | ||
| close(s); | ||
| return "timeout"; | ||
| } catch (IOException e) { | ||
| close(s); | ||
| return e.getClass().getSimpleName(); | ||
| } | ||
| } | ||
|
|
||
| static void close(Socket s) { | ||
| try { s.close(); } catch (IOException e) { } | ||
| } | ||
|
|
||
| // a listener that never accepts holds backlog + 1 connections, the rest time out | ||
| static void fullQueueTimesOut() throws Exception { | ||
| List<Socket> clients = new ArrayList<>(); | ||
| try (ServerSocket ss = new ServerSocket(0, 2, LO)) { | ||
| StringBuilder sb = new StringBuilder(); | ||
| for (int i = 0; i < 8; i++) sb.append(connect(ss.getLocalPort(), 300, clients)).append(' '); | ||
| System.out.println("backlog 2: " + sb.toString().trim()); | ||
| } | ||
| clients.forEach(ListenBacklog::close); | ||
| } | ||
|
|
||
| // non-blocking connects made at once: backlog + 1 finish, the rest stay pending | ||
| static void nonBlockingBurst() throws Exception { | ||
| try (ServerSocket ss = new ServerSocket(0, 10, LO); Selector sel = Selector.open()) { | ||
| List<SocketChannel> channels = new ArrayList<>(); | ||
| for (int i = 0; i < 40; i++) { | ||
| SocketChannel sc = SocketChannel.open(); | ||
| sc.configureBlocking(false); | ||
| if (!sc.connect(new InetSocketAddress(LO, ss.getLocalPort()))) sc.register(sel, SelectionKey.OP_CONNECT); | ||
| channels.add(sc); | ||
| } | ||
| int finished = 0; | ||
| long end = System.currentTimeMillis() + 1500; | ||
| while (System.currentTimeMillis() < end) { | ||
| sel.select(100); | ||
| for (SelectionKey k : sel.selectedKeys()) { | ||
| if (((SocketChannel) k.channel()).finishConnect()) { | ||
| finished++; | ||
| k.cancel(); | ||
| } | ||
| } | ||
| sel.selectedKeys().clear(); | ||
| } | ||
| System.out.println("burst finished " + finished); | ||
| for (SocketChannel sc : channels) sc.close(); | ||
| } | ||
| } | ||
|
|
||
| // a blocked connect finishes once accept removes a connection from the queue | ||
| static void acceptMakesRoom() throws Exception { | ||
| List<Socket> clients = new ArrayList<>(); | ||
| try (ServerSocket ss = new ServerSocket(0, 1, LO)) { | ||
| int port = ss.getLocalPort(); | ||
| int ok = 0; | ||
| for (int i = 0; i < 2; i++) if (connect(port, 1000, clients).equals("ok")) ok++; | ||
| System.out.println("queued " + ok); | ||
| System.out.println("full " + connect(port, 300, clients)); | ||
| ExecutorService pool = Executors.newSingleThreadExecutor(); | ||
| Future<String> waiting = pool.submit(() -> connect(port, 10000, clients)); | ||
| Thread.sleep(300); | ||
| System.out.println("still waiting " + !waiting.isDone()); | ||
| try (Socket a = ss.accept()) { | ||
| System.out.println("accepted " + a.getInetAddress().isLoopbackAddress()); | ||
| } | ||
| System.out.println("waiting connect " + waiting.get(10, TimeUnit.SECONDS)); | ||
| try (Socket a = ss.accept()) { | ||
| System.out.println("accepted again " + a.getInetAddress().isLoopbackAddress()); | ||
| } | ||
| pool.shutdown(); | ||
| } | ||
| clients.forEach(ListenBacklog::close); | ||
| } | ||
|
|
||
| // a listener that closes while a connect waits refuses it | ||
| static void listenerCloseRefuses() throws Exception { | ||
| List<Socket> clients = new ArrayList<>(); | ||
| ServerSocket ss = new ServerSocket(0, 1, LO); | ||
| int port = ss.getLocalPort(); | ||
| for (int i = 0; i < 2; i++) connect(port, 1000, clients); | ||
| ExecutorService pool = Executors.newSingleThreadExecutor(); | ||
| Future<String> waiting = pool.submit(() -> connect(port, 10000, clients)); | ||
| Thread.sleep(300); | ||
| ss.close(); | ||
| System.out.println("after close " + waiting.get(10, TimeUnit.SECONDS)); | ||
| pool.shutdown(); | ||
| clients.forEach(ListenBacklog::close); | ||
| } | ||
| } |
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.