Search in sources :

Example 1 with HttpStreamResponseHandler

use of software.amazon.awssdk.crt.http.HttpStreamResponseHandler in project aws-crt-java by awslabs.

the class ProxyTest method doHttpConnectionManagerProxyTest.

private void doHttpConnectionManagerProxyTest(HttpClientConnectionManager manager) {
    HttpRequest request = new HttpRequest("GET", "/");
    CompletableFuture requestCompleteFuture = new CompletableFuture();
    manager.acquireConnection().whenComplete((conn, throwable) -> {
        if (throwable != null) {
            requestCompleteFuture.completeExceptionally(throwable);
        }
        HttpStream stream = conn.makeRequest(request, new HttpStreamResponseHandler() {

            @Override
            public void onResponseHeaders(HttpStream stream, int responseStatusCode, int blockType, HttpHeader[] nextHeaders) {
                ;
            }

            @Override
            public void onResponseComplete(HttpStream stream, int errorCode) {
                // When this Request is complete, release the conn back to the pool
                manager.releaseConnection(conn);
                stream.close();
                if (errorCode != CRT.AWS_CRT_SUCCESS) {
                    requestCompleteFuture.completeExceptionally(new CrtRuntimeException(errorCode));
                } else {
                    requestCompleteFuture.complete(null);
                }
            }
        });
        if (stream != null) {
            stream.activate();
        }
    });
    requestCompleteFuture.join();
}
Also used : HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) CompletableFuture(java.util.concurrent.CompletableFuture) HttpHeader(software.amazon.awssdk.crt.http.HttpHeader) HttpStreamResponseHandler(software.amazon.awssdk.crt.http.HttpStreamResponseHandler) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) HttpStream(software.amazon.awssdk.crt.http.HttpStream)

Example 2 with HttpStreamResponseHandler

use of software.amazon.awssdk.crt.http.HttpStreamResponseHandler in project aws-crt-java by awslabs.

the class HttpRequestResponseTest method testHttpRequestUnActivated.

@Test
public void testHttpRequestUnActivated() throws Exception {
    skipIfNetworkUnavailable();
    URI uri = new URI("https://httpbin.org");
    HttpHeader[] requestHeaders = new HttpHeader[] { new HttpHeader("Host", uri.getHost()) };
    HttpRequest request = new HttpRequest("GET", "/get", requestHeaders, null);
    CompletableFuture<Void> shutdownComplete = null;
    try (HttpClientConnectionManager connPool = createConnectionPoolManager(uri)) {
        shutdownComplete = connPool.getShutdownCompleteFuture();
        try (HttpClientConnection conn = connPool.acquireConnection().get(60, TimeUnit.SECONDS)) {
            HttpStreamResponseHandler streamHandler = new HttpStreamResponseHandler() {

                @Override
                public void onResponseHeaders(HttpStream stream, int responseStatusCode, int blockType, HttpHeader[] nextHeaders) {
                // do nothing
                }

                @Override
                public void onResponseHeadersDone(HttpStream stream, int blockType) {
                // do nothing
                }

                @Override
                public int onResponseBody(HttpStream stream, byte[] bodyBytesIn) {
                    // do nothing
                    return bodyBytesIn.length;
                }

                @Override
                public void onResponseComplete(HttpStream stream, int errorCode) {
                // do nothing.
                }
            };
            HttpStream stream = conn.makeRequest(request, streamHandler);
            stream.close();
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    if (shutdownComplete != null) {
        shutdownComplete.get();
    }
    CrtResource.waitForNoResources();
}
Also used : HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) HttpHeader(software.amazon.awssdk.crt.http.HttpHeader) HttpClientConnection(software.amazon.awssdk.crt.http.HttpClientConnection) HttpStreamResponseHandler(software.amazon.awssdk.crt.http.HttpStreamResponseHandler) HttpClientConnectionManager(software.amazon.awssdk.crt.http.HttpClientConnectionManager) URI(java.net.URI) HttpStream(software.amazon.awssdk.crt.http.HttpStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Test(org.junit.Test)

Example 3 with HttpStreamResponseHandler

use of software.amazon.awssdk.crt.http.HttpStreamResponseHandler in project aws-crt-java by awslabs.

the class HttpRequestResponseTest method getResponse.

public TestHttpResponse getResponse(URI uri, HttpRequest request, byte[] chunkedData) throws Exception {
    boolean actuallyConnected = false;
    final CompletableFuture<Void> reqCompleted = new CompletableFuture<>();
    final TestHttpResponse response = new TestHttpResponse();
    CompletableFuture<Void> shutdownComplete = null;
    try (HttpClientConnectionManager connPool = createConnectionPoolManager(uri)) {
        shutdownComplete = connPool.getShutdownCompleteFuture();
        try (HttpClientConnection conn = connPool.acquireConnection().get(60, TimeUnit.SECONDS)) {
            actuallyConnected = true;
            HttpStreamResponseHandler streamHandler = new HttpStreamResponseHandler() {

                @Override
                public void onResponseHeaders(HttpStream stream, int responseStatusCode, int blockType, HttpHeader[] nextHeaders) {
                    response.statusCode = responseStatusCode;
                    Assert.assertEquals(responseStatusCode, stream.getResponseStatusCode());
                    response.headers.addAll(Arrays.asList(nextHeaders));
                }

                @Override
                public void onResponseHeadersDone(HttpStream stream, int blockType) {
                    response.blockType = blockType;
                }

                @Override
                public int onResponseBody(HttpStream stream, byte[] bodyBytesIn) {
                    response.bodyBuffer.put(bodyBytesIn);
                    int amountRead = bodyBytesIn.length;
                    // Slide the window open by the number of bytes just read
                    return amountRead;
                }

                @Override
                public void onResponseComplete(HttpStream stream, int errorCode) {
                    response.onCompleteErrorCode = errorCode;
                    reqCompleted.complete(null);
                    stream.close();
                }
            };
            HttpStream stream = conn.makeRequest(request, streamHandler);
            stream.activate();
            if (chunkedData != null) {
                stream.writeChunk(chunkedData, true).get(5, TimeUnit.SECONDS);
            }
            // Give the request up to 60 seconds to complete, otherwise throw a TimeoutException
            reqCompleted.get(60, TimeUnit.SECONDS);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    Assert.assertTrue(actuallyConnected);
    shutdownComplete.get();
    CrtResource.waitForNoResources();
    return response;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) HttpClientConnection(software.amazon.awssdk.crt.http.HttpClientConnection) HttpStreamResponseHandler(software.amazon.awssdk.crt.http.HttpStreamResponseHandler) HttpClientConnectionManager(software.amazon.awssdk.crt.http.HttpClientConnectionManager) HttpStream(software.amazon.awssdk.crt.http.HttpStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 4 with HttpStreamResponseHandler

use of software.amazon.awssdk.crt.http.HttpStreamResponseHandler in project aws-crt-java by awslabs.

the class HttpClientConnectionManagerTest method testParallelConnections.

private void testParallelConnections(HttpClientConnectionManager connPool, HttpRequest request, int numThreads, int numRequests) {
    final AtomicInteger numRequestsMade = new AtomicInteger(0);
    final AtomicInteger numConnectionFailures = new AtomicInteger(0);
    final ConcurrentHashMap<Integer, Integer> reqIdToStatus = new ConcurrentHashMap<>();
    final AtomicInteger numErrorCode = new AtomicInteger(0);
    final ExecutorService threadPool = Executors.newFixedThreadPool(numThreads);
    List<CompletableFuture> requestCompleteFutures = new ArrayList<>();
    for (int i = 0; i < numRequests; i++) {
        Log.log(Log.LogLevel.Trace, Log.LogSubject.HttpConnectionManager, String.format("Starting request %d", i));
        CompletableFuture requestCompleteFuture = new CompletableFuture();
        requestCompleteFutures.add(requestCompleteFuture);
        threadPool.execute(() -> {
            // Request a connection from the connection pool
            connPool.acquireConnection().whenComplete((conn, throwable) -> {
                if (throwable != null) {
                    numConnectionFailures.incrementAndGet();
                    connPool.releaseConnection(conn);
                    requestCompleteFuture.completeExceptionally(throwable);
                }
                int requestId = numRequestsMade.incrementAndGet();
                HttpStream stream = conn.makeRequest(request, new HttpStreamResponseHandler() {

                    @Override
                    public void onResponseHeaders(HttpStream stream, int responseStatusCode, int blockType, HttpHeader[] nextHeaders) {
                        reqIdToStatus.put(requestId, responseStatusCode);
                    }

                    @Override
                    public void onResponseComplete(HttpStream stream, int errorCode) {
                        if (errorCode != CRT.AWS_CRT_SUCCESS) {
                            numErrorCode.incrementAndGet();
                        }
                        // When this Request is complete, release the conn back to the pool
                        connPool.releaseConnection(conn);
                        stream.close();
                        requestCompleteFuture.complete(null);
                    }
                });
                if (stream != null) {
                    stream.activate();
                }
            });
        });
    }
    // Wait for all Requests to complete
    for (CompletableFuture f : requestCompleteFutures) {
        f.join();
    }
    final int requiredSuccesses = (int) Math.floor(numRequests * 0.95);
    final int allowedFailures = numRequests - requiredSuccesses;
    // Verify we got some Http Status Code for each Request
    Assert.assertTrue(reqIdToStatus.size() >= requiredSuccesses);
    // Verify that the failure counts aren't too high
    Assert.assertTrue(numErrorCode.get() <= allowedFailures);
    Assert.assertTrue(numConnectionFailures.get() <= allowedFailures);
}
Also used : HttpStreamResponseHandler(software.amazon.awssdk.crt.http.HttpStreamResponseHandler) ArrayList(java.util.ArrayList) HttpStream(software.amazon.awssdk.crt.http.HttpStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletableFuture(java.util.concurrent.CompletableFuture) HttpHeader(software.amazon.awssdk.crt.http.HttpHeader) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

HttpStream (software.amazon.awssdk.crt.http.HttpStream)4 HttpStreamResponseHandler (software.amazon.awssdk.crt.http.HttpStreamResponseHandler)4 CompletableFuture (java.util.concurrent.CompletableFuture)3 HttpHeader (software.amazon.awssdk.crt.http.HttpHeader)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 HttpClientConnection (software.amazon.awssdk.crt.http.HttpClientConnection)2 HttpClientConnectionManager (software.amazon.awssdk.crt.http.HttpClientConnectionManager)2 HttpRequest (software.amazon.awssdk.crt.http.HttpRequest)2 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ExecutorService (java.util.concurrent.ExecutorService)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Test (org.junit.Test)1 CrtRuntimeException (software.amazon.awssdk.crt.CrtRuntimeException)1