当前位置:   article > 正文

聊聊AsyncHttpClient的默认配置_org.asynchttpclient.asynchttpclient

org.asynchttpclient.asynchttpclient

本文主要研究一下AsyncHttpClient的默认配置

maven

        <dependency>
            <groupId>org.asynchttpclient</groupId>
            <artifactId>async-http-client</artifactId>
            <version>2.2.0</version>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

AsyncHttpClientConfig

org/asynchttpclient/AsyncHttpClientConfig.java

public interface AsyncHttpClientConfig {

  /**
   * @return the version of AHC
   */
  String getAhcVersion();

  /**
   * Return the name of {@link AsyncHttpClient}, which is used for thread naming and debugging.
   *
   * @return the name.
   */
  String getThreadPoolName();

  /**
   * Return the maximum number of connections an {@link AsyncHttpClient} can handle.
   *
   * @return the maximum number of connections an {@link AsyncHttpClient} can handle.
   */
  int getMaxConnections();

  /**
   * Return the maximum number of connections per hosts an {@link AsyncHttpClient} can handle.
   *
   * @return the maximum number of connections per host an {@link AsyncHttpClient} can handle.
   */
  int getMaxConnectionsPerHost();

  /**
   * Return the maximum time in millisecond an {@link AsyncHttpClient} can wait when connecting to a remote host
   *
   * @return the maximum time in millisecond an {@link AsyncHttpClient} can wait when connecting to a remote host
   */
  int getConnectTimeout();

  /**
   * Return the maximum time in millisecond an {@link AsyncHttpClient} can stay idle.
   *
   * @return the maximum time in millisecond an {@link AsyncHttpClient} can stay idle.
   */
  int getReadTimeout();

  /**
   * Return the maximum time in millisecond an {@link AsyncHttpClient} will keep connection in pool.
   *
   * @return the maximum time in millisecond an {@link AsyncHttpClient} will keep connection in pool.
   */
  int getPooledConnectionIdleTimeout();

  /**
   * @return the period in millis to clean the pool of dead and idle connections.
   */
  int getConnectionPoolCleanerPeriod();

  /**
   * Return the maximum time in millisecond an {@link AsyncHttpClient} waits until the response is completed.
   *
   * @return the maximum time in millisecond an {@link AsyncHttpClient} waits until the response is completed.
   */
  int getRequestTimeout();

  /**
   * Is HTTP redirect enabled
   *
   * @return true if enabled.
   */
  boolean isFollowRedirect();

  /**
   * Get the maximum number of HTTP redirect
   *
   * @return the maximum number of HTTP redirect
   */
  int getMaxRedirects();

  /**
   * Is the {@link ChannelPool} support enabled.
   *
   * @return true if keep-alive is enabled
   */
  boolean isKeepAlive();

  /**
   * Return the USER_AGENT header value
   *
   * @return the USER_AGENT header value
   */
  String getUserAgent();

  /**
   * Is HTTP compression enforced.
   *
   * @return true if compression is enforced
   */
  boolean isCompressionEnforced();

  /**
   * Return the {@link java.util.concurrent.ThreadFactory} an {@link AsyncHttpClient} use for handling asynchronous response.
   *
   * @return the {@link java.util.concurrent.ThreadFactory} an {@link AsyncHttpClient} use for handling asynchronous response. If no {@link ThreadFactory} has been explicitly
   * provided, this method will return <code>null</code>
   */
  ThreadFactory getThreadFactory();

  /**
   * An instance of {@link ProxyServer} used by an {@link AsyncHttpClient}
   *
   * @return instance of {@link ProxyServer}
   */
  ProxyServerSelector getProxyServerSelector();

  /**
   * Return an instance of {@link SslContext} used for SSL connection.
   *
   * @return an instance of {@link SslContext} used for SSL connection.
   */
  SslContext getSslContext();

  /**
   * Return the current {@link Realm}
   *
   * @return the current {@link Realm}
   */
  Realm getRealm();

  /**
   * Return the list of {@link RequestFilter}
   *
   * @return Unmodifiable list of {@link ResponseFilter}
   */
  List<RequestFilter> getRequestFilters();

  /**
   * Return the list of {@link ResponseFilter}
   *
   * @return Unmodifiable list of {@link ResponseFilter}
   */
  List<ResponseFilter> getResponseFilters();

  /**
   * Return the list of {@link java.io.IOException}
   *
   * @return Unmodifiable list of {@link java.io.IOException}
   */
  List<IOExceptionFilter> getIoExceptionFilters();

  /**
   * Return cookie store that is used to store and retrieve cookies
   *
   * @return {@link CookieStore} object
   */
  CookieStore getCookieStore();

  /**
   * Return the number of time the library will retry when an {@link java.io.IOException} is throw by the remote server
   *
   * @return the number of time the library will retry when an {@link java.io.IOException} is throw by the remote server
   */
  int getMaxRequestRetry();

  /**
   * @return the disableUrlEncodingForBoundRequests
   */
  boolean isDisableUrlEncodingForBoundRequests();

  /**
   * @return true if AHC is to use a LAX cookie encoder, eg accept illegal chars in cookie value
   */
  boolean isUseLaxCookieEncoder();

  /**
   * In the case of a POST/Redirect/Get scenario where the server uses a 302 for the redirect, should AHC respond to the redirect with a GET or whatever the original method was.
   * Unless configured otherwise, for a 302, AHC, will use a GET for this case.
   *
   * @return <code>true</code> if strict 302 handling is to be used, otherwise <code>false</code>.
   */
  boolean isStrict302Handling();

  /**
   * @return the maximum time in millisecond an {@link AsyncHttpClient} will keep connection in the pool, or -1 to keep connection while possible.
   */
  int getConnectionTtl();

  boolean isUseOpenSsl();

  boolean isUseInsecureTrustManager();

  /**
   * @return true to disable all HTTPS behaviors AT ONCE, such as hostname verification and SNI
   */
  boolean isDisableHttpsEndpointIdentificationAlgorithm();

  /**
   * @return the array of enabled protocols
   */
  String[] getEnabledProtocols();

  /**
   * @return the array of enabled cipher suites
   */
  String[] getEnabledCipherSuites();

  /**
   * @return the size of the SSL session cache, 0 means using the default value
   */
  int getSslSessionCacheSize();

  /**
   * @return the SSL session timeout in seconds, 0 means using the default value
   */
  int getSslSessionTimeout();

  //......
}  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214

AsyncHttpClientConfig接口定义了一系列获取配置的接口,比如getMaxConnections、getConnectTimeout、getReadTimeout、getPooledConnectionIdleTimeout等

DefaultAsyncHttpClientConfig

org/asynchttpclient/DefaultAsyncHttpClientConfig.java

public class DefaultAsyncHttpClientConfig implements AsyncHttpClientConfig {

  //......

  public static class Builder {
  	// timeouts

  	// keep-alive

  	// ssl

  	// cookie store

  	// tuning

  	// internals
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

DefaultAsyncHttpClientConfig实现了AsyncHttpClientConfig接口,它还定义了一个Builder来用于创建AsyncHttpClient,其配置主要分为timeouts、keep-alive、ssl、cookie store、tuning、internals几个部分,它们的默认值读取的是jar包的ahc-default.properties的配置

timeouts

	// org.asynchttpclient.connectTimeout=5000
    private int connectTimeout = defaultConnectTimeout();

    // org.asynchttpclient.requestTimeout=60000
    private int requestTimeout = defaultRequestTimeout();

    // org.asynchttpclient.readTimeout=60000
    private int readTimeout = defaultReadTimeout();

    // org.asynchttpclient.shutdownQuietPeriod=2000
    private int shutdownQuietPeriod = defaultShutdownQuietPeriod();

    // org.asynchttpclient.shutdownTimeout=15000
    private int shutdownTimeout = defaultShutdownTimeout();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

connectTimeout默认为5s、requestTimeout默认为60s、readTimeout默认为60s、shutdownQuietPeriod默认为2s、shutdownTimeout默认为15s

keep-alive

	// org.asynchttpclient.keepAlive=true
    private boolean keepAlive = defaultKeepAlive();

    // org.asynchttpclient.pooledConnectionIdleTimeout=60000
    private int pooledConnectionIdleTimeout = defaultPooledConnectionIdleTimeout();

    // org.asynchttpclient.connectionPoolCleanerPeriod=1000
    private int connectionPoolCleanerPeriod = defaultConnectionPoolCleanerPeriod();

    // org.asynchttpclient.connectionTtl=-1
    private int connectionTtl = defaultConnectionTtl();

    // org.asynchttpclient.maxConnections=-1
    private int maxConnections = defaultMaxConnections();

    // org.asynchttpclient.maxConnectionsPerHost=-1
    private int maxConnectionsPerHost = defaultMaxConnectionsPerHost();

    private ChannelPool channelPool;

    private KeepAliveStrategy keepAliveStrategy = new DefaultKeepAliveStrategy();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

默认开启keepAlive、pooledConnectionIdleTimeout默认为60s、connectionPoolCleanerPeriod默认为1s、connectionTtl默认为-1、maxConnectionsPerHost默认为-1

tuning

	// org.asynchttpclient.tcpNoDelay=true
    private boolean tcpNoDelay = defaultTcpNoDelay();

    // org.asynchttpclient.soReuseAddress=false
    private boolean soReuseAddress = defaultSoReuseAddress();

    // org.asynchttpclient.soLinger=-1
    private int soLinger = defaultSoLinger();

    // org.asynchttpclient.soSndBuf=-1
    private int soSndBuf = defaultSoSndBuf();

    // org.asynchttpclient.soRcvBuf=-1
    private int soRcvBuf = defaultSoRcvBuf();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

tcpNoDelay默认为true、soReuseAddress默认为false、soLinger、soSndBuf、soRcvBuf默认为-1

internals

	// org.asynchttpclient.threadPoolName=AsyncHttpClient
    private String threadPoolName = defaultThreadPoolName();

    // org.asynchttpclient.httpClientCodecMaxInitialLineLength=4096
    private int httpClientCodecMaxInitialLineLength = defaultHttpClientCodecMaxInitialLineLength();

    // org.asynchttpclient.httpClientCodecMaxHeaderSize=8192
    private int httpClientCodecMaxHeaderSize = defaultHttpClientCodecMaxHeaderSize();

    // org.asynchttpclient.httpClientCodecMaxChunkSize=8192
    private int httpClientCodecMaxChunkSize = defaultHttpClientCodecMaxChunkSize();

    // org.asynchttpclient.httpClientCodecInitialBufferSize=128
    private int httpClientCodecInitialBufferSize = defaultHttpClientCodecInitialBufferSize();

    // org.asynchttpclient.chunkedFileChunkSize=8192
    private int chunkedFileChunkSize = defaultChunkedFileChunkSize();

    // org.asynchttpclient.useNativeTransport=false
    private boolean useNativeTransport = defaultUseNativeTransport();
    private ByteBufAllocator allocator;
    private Map<ChannelOption<Object>, Object> channelOptions = new HashMap<>();
    private EventLoopGroup eventLoopGroup;
    private Timer nettyTimer;
    private ThreadFactory threadFactory;
    private Consumer<Channel> httpAdditionalChannelInitializer;
    private Consumer<Channel> wsAdditionalChannelInitializer;
    private ResponseBodyPartFactory responseBodyPartFactory = ResponseBodyPartFactory.EAGER;

    // org.asynchttpclient.ioThreadsCount=0
    private int ioThreadsCount = defaultIoThreadsCount();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

threadPoolName默认为AsyncHttpClient、httpClientCodecMaxInitialLineLength默认为4096、httpClientCodecMaxHeaderSize默认为8192、httpClientCodecMaxChunkSize默认为8192、httpClientCodecInitialBufferSize默认为128、chunkedFileChunkSize默认为8192、ioThreadsCount默认为0

小结

DefaultAsyncHttpClientConfig实现了AsyncHttpClientConfig接口,它还定义了一个Builder来用于创建AsyncHttpClient,其配置主要分为timeouts、keep-alive、ssl、cookie store、tuning、internals几个部分,它们的默认值读取的是jar包的ahc-default.properties的配置。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/413617
推荐阅读
相关标签
  

闽ICP备14008679号