use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project build-info by JFrogDev.
the class PreemptiveHttpClient method createHttpClientBuilder.
private HttpClientBuilder createHttpClientBuilder(String userName, String password, int timeout, int connectionRetries) {
this.connectionRetries = connectionRetries;
int timeoutMilliSeconds = timeout * 1000;
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeoutMilliSeconds).setConnectTimeout(timeoutMilliSeconds).setCircularRedirectsAllowed(true).build();
HttpClientBuilder builder = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig);
if (StringUtils.isEmpty(userName)) {
userName = "anonymous";
password = "";
}
basicCredentialsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(userName, password));
localContext.setCredentialsProvider(basicCredentialsProvider);
// Add as the first request interceptor
builder.addInterceptorFirst(new PreemptiveAuth());
int retryCount = connectionRetries < 0 ? ArtifactoryHttpClient.DEFAULT_CONNECTION_RETRY : connectionRetries;
builder.setRetryHandler(new PreemptiveRetryHandler(retryCount));
builder.setServiceUnavailableRetryStrategy(new PreemptiveRetryStrategy());
builder.setRedirectStrategy(new PreemptiveRedirectStrategy());
// set the following user agent with each request
String userAgent = "ArtifactoryBuildClient/" + CLIENT_VERSION;
builder.setUserAgent(userAgent);
return builder;
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project epp.mpc by eclipse.
the class HttpClientFactory method setClientTimeouts.
private static void setClientTimeouts(HttpClientBuilder builder) {
@SuppressWarnings("restriction") int connectTimeoutUssDefault = StorageProperties.getProperty(StorageProperties.CONNECT_TIMEOUT, HttpClientTransport.DEFAULT_CONNECT_TIMEOUT);
@SuppressWarnings("restriction") int readTimeoutUssDefault = StorageProperties.getProperty(StorageProperties.SOCKET_TIMEOUT, HttpClientTransport.DEFAULT_READ_TIMEOUT);
int connectTimeout = getTimeoutValue(HttpClientTransport.CONNECT_TIMEOUT_PROPERTY, connectTimeoutUssDefault);
int readTimeout = getTimeoutValue(HttpClientTransport.READ_TIMEOUT_PROPERTY, readTimeoutUssDefault);
int connectionRequestTimeout = getTimeoutValue(HttpClientTransport.CONNECTION_REQUEST_TIMEOUT_PROPERTY, HttpClientTransport.DEFAULT_CONNECTION_REQUEST_TIMEOUT);
SocketConfig defaultSocketConfig = SocketConfig.copy(SocketConfig.DEFAULT).setSoTimeout(readTimeout).setTcpNoDelay(// Disable Nagle - see https://en.wikipedia.org/wiki/Nagle%27s_algorithm#Negative_effect_on_larger_writes
true).build();
RequestConfig defaultRequestConfig = RequestConfig.copy(RequestConfig.DEFAULT).setSocketTimeout(readTimeout).setConnectTimeout(connectTimeout).setConnectionRequestTimeout(connectionRequestTimeout).build();
builder.setDefaultSocketConfig(defaultSocketConfig);
builder.setDefaultRequestConfig(defaultRequestConfig);
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project gateway-dubbox by zhuzhong.
the class OpenApiHttpClientServiceImpl method initHttpClient.
private void initHttpClient() {
// 注册访问协议相关的socket工厂
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", SSLConnectionSocketFactory.getSystemSocketFactory()).build();
// httpclient 工厂
HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory = new ManagedHttpClientConnectionFactory(DefaultHttpRequestWriterFactory.INSTANCE, DefaultHttpResponseParserFactory.INSTANCE);
// dns解析器
DnsResolver dnsResolver = SystemDefaultDnsResolver.INSTANCE;
// 创建池化连接管理器
manager = new PoolingHttpClientConnectionManager(socketFactoryRegistry, connFactory, dnsResolver);
// 默认socket配置
SocketConfig defaultSocketConfig = SocketConfig.custom().setTcpNoDelay(true).build();
manager.setDefaultSocketConfig(defaultSocketConfig);
// 设置整个连接池的最大连接数
manager.setMaxTotal(this.maxTotal);
// 每个路由最大连接数
manager.setDefaultMaxPerRoute(this.defaultMaxPerRoute);
manager.setValidateAfterInactivity(this.validateAfterInactivity);
RequestConfig defaultRequestConfig = RequestConfig.custom().setConnectTimeout(this.connectionTimeout).setSocketTimeout(this.socketTimeout).setConnectionRequestTimeout(this.connectionRequestTimeout).build();
httpClient = HttpClients.custom().setConnectionManager(manager).setConnectionManagerShared(false).evictIdleConnections(60, TimeUnit.SECONDS).evictExpiredConnections().setConnectionTimeToLive(60, TimeUnit.SECONDS).setDefaultRequestConfig(defaultRequestConfig).setConnectionReuseStrategy(DefaultConnectionReuseStrategy.INSTANCE).setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE).setRetryHandler(new DefaultHttpRequestRetryHandler(0, false)).build();
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try {
httpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project vespa by vespa-engine.
the class ConfigServerRestExecutorImpl method proxyCall.
private Optional<ProxyResponse> proxyCall(URI uri, ProxyRequest proxyRequest, StringBuilder errorBuilder) throws ProxyException {
String fullUri = uri.toString() + removeFirstSlashIfAny(proxyRequest.getConfigServerRequest());
final HttpRequestBase requestBase = createHttpBaseRequest(proxyRequest.getMethod(), fullUri, proxyRequest.getData());
// Empty list of headers to copy for now, add headers when needed, or rewrite logic.
copyHeaders(proxyRequest.getHeaders(), requestBase);
RequestConfig config = RequestConfig.custom().setConnectTimeout((int) PROXY_REQUEST_TIMEOUT.toMillis()).setConnectionRequestTimeout((int) PROXY_REQUEST_TIMEOUT.toMillis()).setSocketTimeout((int) PROXY_REQUEST_TIMEOUT.toMillis()).build();
try (CloseableHttpClient client = createHttpClient(config, sslContextProvider, zoneRegistry, proxyRequest);
CloseableHttpResponse response = client.execute(requestBase)) {
String content = getContent(response);
int status = response.getStatusLine().getStatusCode();
if (status / 100 == 5) {
errorBuilder.append("Talking to server ").append(uri.getHost());
errorBuilder.append(", got ").append(status).append(" ").append(content).append("\n");
log.log(LogLevel.DEBUG, () -> String.format("Got response from %s with status code %d and content:\n %s", uri.getHost(), status, content));
return Optional.empty();
}
final Header contentHeader = response.getLastHeader("Content-Type");
final String contentType;
if (contentHeader != null && contentHeader.getValue() != null && !contentHeader.getValue().isEmpty()) {
contentType = contentHeader.getValue().replace("; charset=UTF-8", "");
} else {
contentType = "application/json";
}
// Send response back
return Optional.of(new ProxyResponse(proxyRequest, content, status, Optional.of(uri), contentType));
} catch (Exception e) {
errorBuilder.append("Talking to server ").append(uri.getHost());
errorBuilder.append(" got exception ").append(e.getMessage());
log.log(LogLevel.DEBUG, e, () -> "Got exception while sending request to " + uri.getHost());
return Optional.empty();
}
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project vespa by vespa-engine.
the class ConfigServerRestExecutorImpl method queueFirstServerIfDown.
/**
* During upgrade, one server can be down, this is normal. Therefor we do a quick ping on the first server,
* if it is not responding, we try the other servers first. False positive/negatives are not critical,
* but will increase latency to some extent.
*/
private boolean queueFirstServerIfDown(List<URI> allServers, ProxyRequest proxyRequest) {
if (allServers.size() < 2) {
return false;
}
URI uri = allServers.get(0);
HttpGet httpget = new HttpGet(uri);
int timeout = 500;
RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).build();
try (CloseableHttpClient client = createHttpClient(config, sslContextProvider, zoneRegistry, proxyRequest);
CloseableHttpResponse response = client.execute(httpget)) {
if (response.getStatusLine().getStatusCode() == 200) {
return false;
}
} catch (IOException e) {
// We ignore this, if server is restarting this might happen.
}
// Some error happened, move this server to the back. The other servers should be running.
allServers.remove(0);
allServers.add(uri);
return true;
}
Aggregations