use of okhttp3.mockwebserver.Dispatcher in project buck by facebook.
the class ArtifactCaches method createHttpArtifactCache.
private static ArtifactCache createHttpArtifactCache(HttpCacheEntry cacheDescription, final String hostToReportToRemote, final BuckEventBus buckEventBus, ProjectFilesystem projectFilesystem, ListeningExecutorService httpWriteExecutorService, ArtifactCacheBuckConfig config, NetworkCacheFactory factory, boolean distributedBuildModeEnabled) {
// Setup the default client to use.
OkHttpClient.Builder storeClientBuilder = new OkHttpClient.Builder();
storeClientBuilder.networkInterceptors().add(chain -> chain.proceed(chain.request().newBuilder().addHeader("X-BuckCache-User", stripNonAscii(System.getProperty("user.name", "<unknown>"))).addHeader("X-BuckCache-Host", stripNonAscii(hostToReportToRemote)).build()));
int timeoutSeconds = cacheDescription.getTimeoutSeconds();
setTimeouts(storeClientBuilder, timeoutSeconds);
storeClientBuilder.connectionPool(new ConnectionPool(/* maxIdleConnections */
(int) config.getThreadPoolSize(), /* keepAliveDurationMs */
config.getThreadPoolKeepAliveDurationMillis(), TimeUnit.MILLISECONDS));
// The artifact cache effectively only connects to a single host at a time. We should allow as
// many concurrent connections to that host as we allow threads.
Dispatcher dispatcher = new Dispatcher();
dispatcher.setMaxRequestsPerHost((int) config.getThreadPoolSize());
storeClientBuilder.dispatcher(dispatcher);
final ImmutableMap<String, String> readHeaders = cacheDescription.getReadHeaders();
final ImmutableMap<String, String> writeHeaders = cacheDescription.getWriteHeaders();
// If write headers are specified, add them to every default client request.
if (!writeHeaders.isEmpty()) {
storeClientBuilder.networkInterceptors().add(chain -> chain.proceed(addHeadersToBuilder(chain.request().newBuilder(), writeHeaders).build()));
}
OkHttpClient storeClient = storeClientBuilder.build();
// For fetches, use a client with a read timeout.
OkHttpClient.Builder fetchClientBuilder = storeClient.newBuilder();
setTimeouts(fetchClientBuilder, timeoutSeconds);
// If read headers are specified, add them to every read client request.
if (!readHeaders.isEmpty()) {
fetchClientBuilder.networkInterceptors().add(chain -> chain.proceed(addHeadersToBuilder(chain.request().newBuilder(), readHeaders).build()));
}
fetchClientBuilder.networkInterceptors().add((chain -> {
Response originalResponse = chain.proceed(chain.request());
return originalResponse.newBuilder().body(new ProgressResponseBody(originalResponse.body(), buckEventBus)).build();
}));
OkHttpClient fetchClient = fetchClientBuilder.build();
HttpService fetchService;
HttpService storeService;
switch(config.getLoadBalancingType()) {
case CLIENT_SLB:
HttpLoadBalancer clientSideSlb = config.getSlbConfig().createClientSideSlb(new DefaultClock(), buckEventBus, new CommandThreadFactory("ArtifactCaches.HttpLoadBalancer", SLB_THREAD_PRIORITY));
fetchService = new RetryingHttpService(buckEventBus, new LoadBalancedService(clientSideSlb, fetchClient, buckEventBus), config.getMaxFetchRetries());
storeService = new LoadBalancedService(clientSideSlb, storeClient, buckEventBus);
break;
case SINGLE_SERVER:
URI url = cacheDescription.getUrl();
fetchService = new SingleUriService(url, fetchClient);
storeService = new SingleUriService(url, storeClient);
break;
default:
throw new IllegalArgumentException("Unknown HttpLoadBalancer type: " + config.getLoadBalancingType());
}
String cacheName = cacheDescription.getName().map(input -> "http-" + input).orElse("http");
boolean doStore = cacheDescription.getCacheReadMode().isDoStore();
return factory.newInstance(NetworkCacheArgs.builder().setThriftEndpointPath(config.getHybridThriftEndpoint()).setCacheName(cacheName).setRepository(config.getRepository()).setScheduleType(config.getScheduleType()).setFetchClient(fetchService).setStoreClient(storeService).setDoStore(doStore).setProjectFilesystem(projectFilesystem).setBuckEventBus(buckEventBus).setHttpWriteExecutorService(httpWriteExecutorService).setErrorTextTemplate(cacheDescription.getErrorMessageFormat()).setDistributedBuildModeEnabled(distributedBuildModeEnabled).build());
}
use of okhttp3.mockwebserver.Dispatcher in project zipkin by openzipkin.
the class TraceZipkinElasticsearchHttpStorageAutoConfiguration method elasticsearchOkHttpClientBuilder.
@Bean
@Qualifier("zipkinElasticsearchHttp")
@ConditionalOnMissingBean
OkHttpClient.Builder elasticsearchOkHttpClientBuilder() {
// have to indirect to unwind a circular dependency
Interceptor tracingInterceptor = new Interceptor() {
Interceptor delegate = BraveTracingInterceptor.builder(brave).serverName("elasticsearch").build();
@Override
public Response intercept(Chain chain) throws IOException {
// Only join traces, don't start them. This prevents LocalCollector's thread from amplifying.
if (brave.serverSpanThreadBinder().getCurrentServerSpan() != null && brave.serverSpanThreadBinder().getCurrentServerSpan().getSpan() != null) {
return delegate.intercept(chain);
}
return chain.proceed(chain.request());
}
};
BraveExecutorService tracePropagatingExecutor = BraveExecutorService.wrap(new Dispatcher().executorService(), brave);
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(tracingInterceptor);
builder.addNetworkInterceptor(tracingInterceptor);
builder.dispatcher(new Dispatcher(tracePropagatingExecutor));
return builder;
}
use of okhttp3.mockwebserver.Dispatcher in project okhttp by square.
the class InterceptorTest method applicationInterceptorReturnsNull.
@Test
public void applicationInterceptorReturnsNull() throws Exception {
server.enqueue(new MockResponse());
Interceptor interceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
chain.proceed(chain.request());
return null;
}
};
client = client.newBuilder().addInterceptor(interceptor).build();
ExceptionCatchingExecutor executor = new ExceptionCatchingExecutor();
client = client.newBuilder().dispatcher(new Dispatcher(executor)).build();
Request request = new Request.Builder().url(server.url("/")).build();
try {
client.newCall(request).execute();
fail();
} catch (NullPointerException expected) {
assertEquals("interceptor " + interceptor + " returned null", expected.getMessage());
}
}
use of okhttp3.mockwebserver.Dispatcher in project okhttp by square.
the class OkHttpAsync method prepare.
@Override
public void prepare(final Benchmark benchmark) {
concurrencyLevel = benchmark.concurrencyLevel;
targetBacklog = benchmark.targetBacklog;
client = new OkHttpClient.Builder().protocols(benchmark.protocols).dispatcher(new Dispatcher(new ThreadPoolExecutor(benchmark.concurrencyLevel, benchmark.concurrencyLevel, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()))).build();
if (benchmark.tls) {
SslClient sslClient = SslClient.localhost();
SSLSocketFactory socketFactory = sslClient.socketFactory;
HostnameVerifier hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String s, SSLSession session) {
return true;
}
};
client = client.newBuilder().sslSocketFactory(socketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build();
}
callback = new Callback() {
@Override
public void onFailure(Call call, IOException e) {
System.out.println("Failed: " + e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
ResponseBody body = response.body();
long total = SynchronousHttpClient.readAllAndClose(body.byteStream());
long finish = System.nanoTime();
if (VERBOSE) {
long start = (Long) response.request().tag();
System.out.printf("Transferred % 8d bytes in %4d ms%n", total, TimeUnit.NANOSECONDS.toMillis(finish - start));
}
requestsInFlight.decrementAndGet();
}
};
}
use of okhttp3.mockwebserver.Dispatcher in project okhttp by square.
the class HttpOverHttp2Test method concurrentHttp2ConnectionsDeduplicated.
/**
* We don't know if the connection will support HTTP/2 until after we've connected. When multiple
* connections are requested concurrently OkHttp will pessimistically connect multiple times, then
* close any unnecessary connections. This test confirms that behavior works as intended.
*
* <p>This test uses proxy tunnels to get a hook while a connection is being established.
*/
@Test
public void concurrentHttp2ConnectionsDeduplicated() throws Exception {
server.useHttps(sslClient.socketFactory, true);
// Force a fresh connection pool for the test.
client.connectionPool().evictAll();
final QueueDispatcher queueDispatcher = new QueueDispatcher();
queueDispatcher.enqueueResponse(new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END).clearHeaders());
queueDispatcher.enqueueResponse(new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END).clearHeaders());
queueDispatcher.enqueueResponse(new MockResponse().setBody("call2 response"));
queueDispatcher.enqueueResponse(new MockResponse().setBody("call1 response"));
// We use a re-entrant dispatcher to initiate one HTTPS connection while the other is in flight.
server.setDispatcher(new Dispatcher() {
int requestCount;
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
MockResponse result = queueDispatcher.dispatch(request);
requestCount++;
if (requestCount == 1) {
// Before handling call1's CONNECT we do all of call2. This part re-entrant!
try {
Call call2 = client.newCall(new Request.Builder().url("https://android.com/call2").build());
Response response2 = call2.execute();
assertEquals("call2 response", response2.body().string());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return result;
}
@Override
public MockResponse peek() {
return queueDispatcher.peek();
}
@Override
public void shutdown() {
queueDispatcher.shutdown();
}
});
client = client.newBuilder().proxy(server.toProxyAddress()).build();
Call call1 = client.newCall(new Request.Builder().url("https://android.com/call1").build());
Response response2 = call1.execute();
assertEquals("call1 response", response2.body().string());
RecordedRequest call1Connect = server.takeRequest();
assertEquals("CONNECT", call1Connect.getMethod());
assertEquals(0, call1Connect.getSequenceNumber());
RecordedRequest call2Connect = server.takeRequest();
assertEquals("CONNECT", call2Connect.getMethod());
assertEquals(0, call2Connect.getSequenceNumber());
RecordedRequest call2Get = server.takeRequest();
assertEquals("GET", call2Get.getMethod());
assertEquals("/call2", call2Get.getPath());
assertEquals(0, call2Get.getSequenceNumber());
RecordedRequest call1Get = server.takeRequest();
assertEquals("GET", call1Get.getMethod());
assertEquals("/call1", call1Get.getPath());
assertEquals(1, call1Get.getSequenceNumber());
assertEquals(1, client.connectionPool().connectionCount());
}
Aggregations