Delay multiple TCP connections from Java to the same machine

(see this question in serverfault)

I have a java client using socket to open concurrent connections to the same machine I've seen one request complete very quickly, but the latency of other requests is 100-3000 milliseconds Packet checking using Wireshark shows that all syn packets wait longer than the first time before leaving the client I see this on both windows and Linux clients What may be the cause? This happens when the client is a Windows 2008 or Linux box

Code attachment:

import java.util.*;
import java.net.*;

public class Tester {
    public static void main(String[] args) throws Exception {
        if (args.length < 3) {
            usage();
            return;
        }
        final int n = Integer.parseInt(args[0]);
        final String ip = args[1];
        final int port = Integer.parseInt(args[2]);

        ExecutorService executor = Executors.newFixedThreadPool(n);

        ArrayList<Callable<Long>> tasks = new ArrayList<Callable<Long>>();
        for (int i = 0; i < n; ++i)
            tasks.add(new Callable<Long>() {
                public Long call() {
                    Date before = new Date();
                    try {
                        Socket socket = new Socket();
                        socket.connect(new InetSocketAddress(ip,port));
                    }

                    catch (Throwable e) {
                        e.printStackTrace();
                    }
                    Date after = new Date();
                    return after.getTime() - before.getTime();
                }
            });
        System.out.println("Invoking");
        List<Future<Long>> results = executor.invokeAll(tasks);
        System.out.println("Invoked");
        for (Future<Long> future : results) {
            System.out.println(future.get());
        }
        executor.shutdown();
    }

    private static void usage() {
        System.out.println("Usage: prog <threads> <url/IP Port>");
        System.out.println("Examples:");
        System.out.println("  prog tcp 10 127.0.0.1 2000");
    }
}

Update – if the relevant ARP entry is cleared before running the test program, the problem will recur again and again I tried to adjust TCP retransmission timeout, but it didn't help In addition, we transplant the program to Net, but the problem still occurs

Update 2 – 3 seconds is the specified delay for creating a new connection from RFC 1122 I still don't fully understand why there is a retransmission here. It should be handled by the MAC layer In addition, we reproduced this problem using netcat, so it is not Java related

Solution

It looks like you use a single underlying HTTP connection Therefore, other requests cannot be completed before the InputStream of httpurlconnection calls close(), I. that is, before processing the response

Or you should use an HTTP connection pool

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>