如何使用Http持久连接在循环中保持Java套接字低延迟?

时间:2019-05-06 11:42:11

标签: java sockets http

我正在用Java中的程序反复从API下载一些数据。我大约需要此数据。每1秒低延迟对于我的应用程序很重要!所以我做了这样的事情(伪代码):

while (true)
   1: download from API
   2: sleep from remainder of 1 sec
end

平均下载时间为40-50毫秒。 当我忽略睡眠时间(步骤2)时,平均下载时间为6-8ms。

我已经在许多不同的实现中进行了尝试:HttpUrlConnection,HttpClient(Java11)和Apache HttpClient,但是问题仍然存在。所以我做了自己的套接字实现,但是问题仍然存在。有办法解决这个问题吗?

我尝试了TCP_NO_DELAY,但没有结果。

我的套接字实现:

try (Socket socket = new Socket(hostname, 80)) {
    OutputStream writer = socket.getOutputStream();                     
    InputStream input = socket.getInputStream();                       

    for (int i = 0; i < 10; i++) {                   
        //request headers
        String request = "GET " + path + " HTTP/1.1"    + "\r\n" 
                        + "Host: " + hostname           + "\r\n"
                        + "Connection: keep-alive"      + "\r\n"
                        + "\r\n";
        byte[] wb = request.getBytes(StandardCharsets.US_ASCII);        
        writer.write(wb);

        String statusLine       = readInputStatusLine(input);
        LinkedHashMap headers   = readInputHeaders(input);
        String body             = readBody(headers, input);

        //SLEEP FOR A WHILE (THIS CAUSES DELAYS!!??)
        Thread.sleep(1000);                                             
    }
    socket.close();                                                    
}

1 个答案:

答案 0 :(得分:0)

另一个戴夫是对的!此问题是由某些服务器端(可能是缓存)问题引起的。通过进行两次后续的轮询,我能够对此进行测试并做出变通方法,然后使线程休眠。我只是丢弃了第一次(慢速)轮询,而仅使用了第二次(快速)轮询。设法使速度提高了约4倍。感谢您的帮助。 伪代码:

while (true)
   1: Do GET and trow away
   2: Do GET and use
   3: sleep for remainder of 1 sec
 end

代码调整:

//SLEEP every second time
if (i % 2 == 0 ){
    totalTimeFast += dur;
    System.out.println(i + ": " + statusLine + ", body length = "
        + body.length() + ", dur=" + dur + " ms   FAST");
    Thread.sleep(Math.max(0, 1000-dur));   
} else {
    totalTimeSlow += dur;
    System.out.println(i + ": " + statusLine + ", body length = "
        + body.length() + ", dur=" + dur + " ms   SLOW");
}   

以及N = 100测试的部分结果:

90: HTTP/1.1 200 OK, body length = 187136, dur=8 ms   FAST
91: HTTP/1.1 200 OK, body length = 186658, dur=51 ms   SLOW
92: HTTP/1.1 200 OK, body length = 186658, dur=34 ms   FAST
93: HTTP/1.1 200 OK, body length = 186461, dur=51 ms   SLOW
94: HTTP/1.1 200 OK, body length = 186461, dur=7 ms   FAST
95: HTTP/1.1 200 OK, body length = 186374, dur=42 ms   SLOW
96: HTTP/1.1 200 OK, body length = 186374, dur=7 ms   FAST
97: HTTP/1.1 200 OK, body length = 186612, dur=39 ms   SLOW
98: HTTP/1.1 200 OK, body length = 186612, dur=5 ms   FAST
99: HTTP/1.1 200 OK, body length = 186550, dur=36 ms   SLOW
100: HTTP/1.1 200 OK, body length = 186550, dur=13 ms   FAST
Got 100 results with a  totaltimeFast of 1541 ms and totalTimeSlow of 4503 ms