This article has been authorized by the author Zhao Huili netease cloud community.

Welcome to visit netease Cloud Community to learn more about Netease’s technical product operation experience.

One, foreword


Recently doing Content distribution Network (Content Delivery Network, hereinafter referred to as CDN) CDN ends after the last collection monitoring, often appear when performing multiple consecutive cases submitted to the “org. Apache. HTTP. NoHttpResponseException” error, Executing a single use case does not report this error. After analyzing why the interface test does not have this problem, but the online regression will trigger this error? The reason is that the online regression set is to check whether the CDN is successfully deployed to verify the actual situation, so the execution time of an online use case is more than 30 minutes, while the offline interface test only needs milliseconds. Next I will introduce why “org. Apache. HTTP. NoHttpResponseException”, and how to solve this problem.


Second, appear “org. Apache. HTTP. NoHttpResponseException”


The implementation class for HttpClient is CloseableHttpClient. There are two ways to create an instance of CloseableHttpClient: (1) Use the method of CloseableHttpClient’s factory class HttpClients to create the instance. HttpClients provides a quick way to create CloseableHttpClient instances based on various default configurations. The simplest way of instantiation is invoked HttpClients createDefault (). (2) Use the CloseableHttpClient builder class HttpClientBuilder to configure some properties and then call the build method to create the instance. The above HttpClients. CreateDefault () call is actually HttpClientBuilder. Create (). The build (). When executing multiple test methods in a row, an error will be reported. Executing one of the test methods directly does not cause this problem:


java.lang.AssertionError: org.apache.http.NoHttpResponseException: nos-yq-yanlian.netease.com:8080 failed to respond
    at org.testng.Assert.fail(Assert.java:89)
    at com.netease.pubncdn.test.testcase.OnlineDomainDeployTest.testCreateHttpDomain(OnlineDomainDeployTest.java:192)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)Copy the code

A search reveals that the Http specification does not specify how long a persistent connection should remain alive. Some Http servers use non-standard keep-alive headers to interact with clients, and the server will hold the connection for several seconds. HttpClient also makes use of this header message. If the server returns a response that does not contain a keep-alive header message, HttpClient assumes that the connection can be maintained forever. However, many servers save server resources by closing a connection that is inactive for a certain period of time without notifying the client. In some cases the default policy is too optimistic and we may need to customize the connection survival policy.


3. Customize the long connection policy


You can customize the connection survival policy by:


CloseableHttpClient client = HttpClients.custom()  
        .setKeepAliveStrategy(keepAliveStrategy)  
        .build();Copy the code
ConnectionKeepAliveStrategy keepAliveStrategy = new ConnectionKeepAliveStrategy() {  
    public long getKeepAliveDuration(HttpResponse response, HttpContext context) {  
        // Honor 'keep-alive' header  
        HeaderElementIterator it = new BasicHeaderElementIterator(  
                response.headerIterator(HTTP.CONN_KEEP_ALIVE));  
        while (it.hasNext()) {  
            HeaderElement he = it.nextElement();  
            String param = he.getName();  
            String value = he.getValue();  
            if(value ! = null && param.equalsIgnoreCase("timeout")) {  
                try {  
                    return Long.parseLong(value) * 1000;  
                } catch(NumberFormatException ignore) {  
                }  
            }  
        }  
        HttpHost target = (HttpHost) context.getAttribute(  
                HttpClientContext.HTTP_TARGET_HOST);  
        if ("www.baidu.com".equalsIgnoreCase(target.getHostName())) {  
            // Keep alive for 5 seconds only  
            return 5 * 1000;  
        } else {  
            // otherwise keep alive for 30 seconds  
            return30 * 1000; }}};Copy the code

4. How to solve the error reporting problem in CDN


In (3) in the way can solve the “org. Apache. HTTP. NoHttpResponseException” error, but considering the actual application scenario is not suitable for this method. Since the interval between one request and the next is more than 30 minutes, it is not appropriate to change the connection time to more than 30 minutes (since many servers save server resources by closing a connection that is inactive for a certain period of time without notifying the client). Modify the test code to create instances in each test case instead of @beforeClass to circumvent this problem.


Five, the summary


To locate the above error, you need to understand the steps of requesting an Http request using HttpClient and how to customize the connection survival policy. After finding the solution, we should consider whether it is really suitable for our application scenarios, and whether this method will cause a waste of resources. Finally, we should choose a suitable way to avoid solving this problem.


Free experience cloud security (EASY Shield) content security, verification code and other services

For more information about netease’s technology, products and operating experience, please click here.


Relevant article: “recommended” flex layout of flex – turns and flex – the shrink how to calculate the “recommended” to know things by learning | future potential safety hazard: soft rib of the AI – how to recommend 】 【 deliberate deception neural network through artificial intelligence “to avoid the” content “pit” security?