|
| 1 | +package com.ecwid.apiclient.v3.httptransport.impl.client5 |
| 2 | + |
| 3 | +import com.ecwid.apiclient.v3.httptransport.HttpRequest |
| 4 | +import com.ecwid.apiclient.v3.httptransport.HttpResponse |
| 5 | +import com.ecwid.apiclient.v3.httptransport.HttpTransport |
| 6 | +import org.apache.hc.client5.http.classic.HttpClient |
| 7 | +import org.apache.hc.client5.http.config.ConnectionConfig |
| 8 | +import org.apache.hc.client5.http.config.RequestConfig |
| 9 | +import org.apache.hc.client5.http.impl.classic.HttpClients |
| 10 | +import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder |
| 11 | +import org.apache.hc.core5.http.Header |
| 12 | +import java.io.Closeable |
| 13 | +import java.io.IOException |
| 14 | +import java.util.concurrent.TimeUnit |
| 15 | + |
| 16 | +private const val DEFAULT_CONNECTION_TIMEOUT = 10_000L // 10 sec |
| 17 | +private const val DEFAULT_READ_TIMEOUT = 60_000 // 1 min |
| 18 | + |
| 19 | +private const val DEFAULT_MAX_CONNECTIONS = 10 |
| 20 | + |
| 21 | +/** |
| 22 | + * Number of attempts to retry request if server responded with 429 |
| 23 | + */ |
| 24 | +internal const val DEFAULT_RATE_LIMIT_ATTEMPTS = 2 |
| 25 | + |
| 26 | +/** |
| 27 | + * Number of seconds to wait until next attempt, if server didn't send Retry-After header |
| 28 | + */ |
| 29 | +internal const val DEFAULT_RATE_LIMIT_RETRY_INTERVAL_SECONDS = 10L |
| 30 | + |
| 31 | +/** |
| 32 | + * Maximal delay in seconds before next attempt |
| 33 | + */ |
| 34 | +internal const val MAX_RATE_LIMIT_RETRY_INTERVAL_SECONDS = 60L |
| 35 | + |
| 36 | +val EMPTY_WAITING_REACTION: (Long) -> Unit = { } |
| 37 | +val EMPTY_BEFORE_REQUEST_ACTION: () -> Unit = { } |
| 38 | + |
| 39 | +open class ApacheCommonsHttpClient5Transport( |
| 40 | + private val httpClient: HttpClient = buildHttpClient(), |
| 41 | + private val rateLimitRetryStrategy: RateLimitRetryStrategy = SleepForRetryAfterRateLimitRetryStrategy(), |
| 42 | +) : HttpTransport { |
| 43 | + |
| 44 | + constructor( |
| 45 | + httpClient: HttpClient, |
| 46 | + defaultRateLimitAttempts: Int = DEFAULT_RATE_LIMIT_ATTEMPTS, |
| 47 | + defaultRateLimitRetryInterval: Long = DEFAULT_RATE_LIMIT_RETRY_INTERVAL_SECONDS, |
| 48 | + maxRateLimitRetryInterval: Long = MAX_RATE_LIMIT_RETRY_INTERVAL_SECONDS, |
| 49 | + onEverySecondOfWaiting: (Long) -> Unit = EMPTY_WAITING_REACTION, |
| 50 | + beforeEachRequestAttempt: () -> Unit = EMPTY_BEFORE_REQUEST_ACTION, |
| 51 | + ) : this( |
| 52 | + httpClient, |
| 53 | + rateLimitRetryStrategy = SleepForRetryAfterRateLimitRetryStrategy( |
| 54 | + defaultRateLimitAttempts = defaultRateLimitAttempts, |
| 55 | + defaultRateLimitRetryInterval = defaultRateLimitRetryInterval, |
| 56 | + maxRateLimitRetryInterval = maxRateLimitRetryInterval, |
| 57 | + onEverySecondOfWaiting = onEverySecondOfWaiting, |
| 58 | + beforeEachRequestAttempt = beforeEachRequestAttempt |
| 59 | + ) |
| 60 | + ) |
| 61 | + |
| 62 | + constructor( |
| 63 | + defaultConnectionTimeout: Long = DEFAULT_CONNECTION_TIMEOUT, |
| 64 | + defaultReadTimeout: Int = DEFAULT_READ_TIMEOUT, |
| 65 | + defaultMaxConnections: Int = DEFAULT_MAX_CONNECTIONS, |
| 66 | + defaultHeaders: List<Header> = emptyList(), |
| 67 | + rateLimitRetryStrategy: RateLimitRetryStrategy |
| 68 | + ) : this( |
| 69 | + httpClient = buildHttpClient( |
| 70 | + defaultConnectionTimeout = defaultConnectionTimeout, |
| 71 | + defaultReadTimeout = defaultReadTimeout, |
| 72 | + defaultMaxConnections = defaultMaxConnections, |
| 73 | + defaultHeaders = defaultHeaders |
| 74 | + ), |
| 75 | + rateLimitRetryStrategy = rateLimitRetryStrategy |
| 76 | + ) |
| 77 | + |
| 78 | + constructor( |
| 79 | + defaultConnectionTimeout: Long = DEFAULT_CONNECTION_TIMEOUT, |
| 80 | + defaultReadTimeout: Int = DEFAULT_READ_TIMEOUT, |
| 81 | + defaultMaxConnections: Int = DEFAULT_MAX_CONNECTIONS, |
| 82 | + defaultRateLimitAttempts: Int = DEFAULT_RATE_LIMIT_ATTEMPTS, |
| 83 | + defaultRateLimitRetryInterval: Long = DEFAULT_RATE_LIMIT_RETRY_INTERVAL_SECONDS, |
| 84 | + maxRateLimitRetryInterval: Long = MAX_RATE_LIMIT_RETRY_INTERVAL_SECONDS, |
| 85 | + defaultHeaders: List<Header> = emptyList(), |
| 86 | + onEverySecondOfWaiting: (Long) -> Unit = EMPTY_WAITING_REACTION, |
| 87 | + beforeEachRequestAttempt: () -> Unit = EMPTY_BEFORE_REQUEST_ACTION, |
| 88 | + ) : this( |
| 89 | + httpClient = buildHttpClient( |
| 90 | + defaultConnectionTimeout = defaultConnectionTimeout, |
| 91 | + defaultReadTimeout = defaultReadTimeout, |
| 92 | + defaultMaxConnections = defaultMaxConnections, |
| 93 | + defaultHeaders = defaultHeaders |
| 94 | + ), |
| 95 | + rateLimitRetryStrategy = SleepForRetryAfterRateLimitRetryStrategy( |
| 96 | + defaultRateLimitAttempts = defaultRateLimitAttempts, |
| 97 | + defaultRateLimitRetryInterval = defaultRateLimitRetryInterval, |
| 98 | + maxRateLimitRetryInterval = maxRateLimitRetryInterval, |
| 99 | + onEverySecondOfWaiting = onEverySecondOfWaiting, |
| 100 | + beforeEachRequestAttempt = beforeEachRequestAttempt, |
| 101 | + ) |
| 102 | + ) |
| 103 | + |
| 104 | + override fun makeHttpRequest(httpRequest: HttpRequest): HttpResponse { |
| 105 | + return try { |
| 106 | + rateLimitRetryStrategy.makeHttpRequest(httpClient, httpRequest) |
| 107 | + } catch (e: IOException) { |
| 108 | + HttpResponse.TransportError(e) |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + override fun close() { |
| 113 | + if (httpClient is Closeable) { |
| 114 | + httpClient.close() |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + companion object { |
| 119 | + |
| 120 | + private fun buildHttpClient( |
| 121 | + defaultConnectionTimeout: Long = DEFAULT_CONNECTION_TIMEOUT, |
| 122 | + defaultReadTimeout: Int = DEFAULT_READ_TIMEOUT, |
| 123 | + defaultMaxConnections: Int = DEFAULT_MAX_CONNECTIONS, |
| 124 | + defaultHeaders: List<Header> = emptyList(), |
| 125 | + ): HttpClient { |
| 126 | + val connectionConfig = ConnectionConfig.custom() |
| 127 | + .setConnectTimeout(defaultConnectionTimeout, TimeUnit.SECONDS) |
| 128 | + .setSocketTimeout(defaultReadTimeout, TimeUnit.SECONDS) |
| 129 | + .build() |
| 130 | + |
| 131 | + val connectionManager = PoolingHttpClientConnectionManagerBuilder.create() |
| 132 | + .setMaxConnTotal(defaultMaxConnections) |
| 133 | + .setMaxConnPerRoute(defaultMaxConnections) |
| 134 | + .setDefaultConnectionConfig(connectionConfig) |
| 135 | + .build() |
| 136 | + |
| 137 | + val requestConfig = RequestConfig.custom() |
| 138 | + .setConnectionRequestTimeout(defaultConnectionTimeout, TimeUnit.SECONDS) |
| 139 | + .build() |
| 140 | + |
| 141 | + val httpClientBuilder = HttpClients.custom() |
| 142 | + .setConnectionManager(connectionManager) |
| 143 | + .setDefaultRequestConfig(requestConfig) |
| 144 | + .setRedirectStrategy(RemoveDisallowedHeadersRedirectStrategy()) |
| 145 | + // TODO .setRetryHandler() |
| 146 | + // TODO .setServiceUnavailableRetryStrategy() |
| 147 | + if (defaultHeaders.isNotEmpty()) { |
| 148 | + httpClientBuilder.setDefaultHeaders(defaultHeaders) |
| 149 | + } |
| 150 | + return httpClientBuilder.build() |
| 151 | + } |
| 152 | + } |
| 153 | +} |
| 154 | + |
0 commit comments