ning / async-http-client

Asynchronous Http Client for Java
135 stars 50 forks source link

Is setMaximumConnectionsTotal() working? #8

Closed gregrluck closed 14 years ago

gregrluck commented 14 years ago

Hi Jean-Francois :)

I have this code:

AsyncHttpClientConfig asyncHttpClientConfig = new AsyncHttpClientConfig.Builder(). setKeepAlive(true).setConnectionTimeoutInMs(1000).setMaximumConnectionsTotal(1).build(); AsyncHttpClient asyncHttpClient = new AsyncHttpClient(asyncHttpClientConfig);

    for (int i = 0; i < cacheOperations; i++) {
        String url = new StringBuffer(cacheUrl).append('/').append(keyBase).append(i).toString();
        final int finalI = i;

// LOG.info("Request " + i); // openConnections.incrementAndGet(); // if (openConnections.get() <= 1) { asyncHttpClient.prepareGet(url).execute(new AsyncCompletionHandler() {

                @Override
                public Response onCompleted(Response response) throws Exception {
                    assertEquals(200, response.getStatusCode());
                    openConnections.decrementAndGet();
                    return response;
                }

                @Override
                public void onThrowable(Throwable t) {
                    openConnections.decrementAndGet();
                    LOG.error("On " + finalI + "th request" + t.getMessage());
                }
            });

// }

    }

I get:

java.io.IOException: Too many connections at com.ning.http.client.providers.NettyAsyncHttpProvider.execute(NettyAsyncHttpProvider.java:495) at com.ning.http.client.AsyncHttpClient.executeRequest(AsyncHttpClient.java:291) at com.ning.http.client.AsyncHttpClient$BoundRequestBuilder.execute(AsyncHttpClient.java:192) at net.sf.ehcache.server.rest.resources.SpeedTest.testMemCachedBench(SpeedTest.java:213) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

But when I control myself the maximum number of open connections and limit those to 10 it works fine. So it seems to me that setMaximumConnectionsTotal() is not working.

jfarcand commented 14 years ago

Hi Greg! Nice to see you here! We had already a unit test that cover that API, but I've just added another one here:

In you code snippet above you set the max to 1, meaning only one connection will be allowed if keep-alive is used, hence the exception. Can you describe what you were expected? Maybe I need to clarify the docs.

Thanks!

-- Jeanfrancois

gregrluck commented 14 years ago

JF

Yes, I am full-time on Ehcache these days, although I still don't seem to be coding that much more. The intent of the code I wrote was to use 10 connections to Jetty and rip through 10,000 gets as fast as possible. I was hoping to set the maximum number of connections to 10 so as not to overwhelm Jetty.

BTW, the result so far with HttpClient is line ball with Memcache.

jfarcand commented 14 years ago

So it doesn't do what you want? Can you take a look at the unit test and see if you can share a test that reproduce the issue? Thanks

jfarcand commented 14 years ago

I'm closing the issue as a non issue. Please re-open if you think the current behavior is wrong.