Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using http2 in okhttp, why multi requests to the same host didn't use just one connectoin

Tags:

java

http2

okhttp

I would like to test okhttp's http2 function. And I make multi requsts to same host in async style. But, I found that, it involved multi connections, since the protocol is h2, It should use just one connection, right? The code is below. Ah, I'm using okhttp2.5

public class Performance {
    private final OkHttpClient client = new OkHttpClient();
    private final Dispatcher dispatcher = new Dispatcher();
    private final int times = 20;

    public Performance(){
        dispatcher.setMaxRequestsPerHost(2);
        client.setDispatcher(dispatcher);

        // Configure the sslContext
        // MySSLSocketFactory mySSLSocketFactory = new MySSLSocketFactory();
        // client.setSslSocketFactory(mySSLSocketFactory);
        // client.setHostnameVerifier(new HostnameVerifier() {
        //     public boolean verify(String s, SSLSession sslSession) {
        //         return true;
        //     }
        // });
    }
    public void run()throws Exception{
        for(int i=0; i<times; i++) {
            Request request = new Request.Builder()
                .url("https://http2bin.org/delay/1")
                .build();
            client.newCall(request).enqueue(new Callback() {
                public void onFailure(Request request, IOException e) {
                    e.printStackTrace();
                }

                public void onResponse(Response response) throws IOException {
                    System.out.println(response.headers().get("OkHttp-Selected-Protocol"));
                }
            });
        }
    }
    public static void main(String[] args)throws Exception{
        Performance performance = new Performance();
        performance.run();
    }
}
like image 247
郑福真 Avatar asked Sep 17 '15 07:09

郑福真


People also ask

Does OkHttp support http2?

OkHttp (https://square.github.io/okhttp/) is an efficient HTTP & HTTP/2 client for Android and Java applications. It's been widely used in Android and has good documentation also.

What is OkHttp connection pool?

OkHttp is an HTTP client from Square for Java and Android applications. It's designed to load resources faster and save bandwidth. OkHttp is widely used in open-source projects and is the backbone of libraries like Retrofit, Picasso, and many others.

Is OkHttp asynchronous?

OkHttp doesn't currently offer asynchronous APIs to receive a response body in parts.

What is the underlying library of OkHttp?

Beginning with Mobile SDK 4.2, the Android REST request system uses OkHttp (v3. 2.0), an open-source external library from Square Open Source, as its underlying architecture. This library replaces the Google Volley library from past releases.


1 Answers

There's a bug in OkHttp where multiple simultaneous requests each create their own socket connection, rather than coordinating a shared connection. This only happens when the connections are created simultaneously. Work around by yielding 500ms before the 2nd connection.

like image 127
Jesse Wilson Avatar answered Oct 25 '22 10:10

Jesse Wilson