Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ public static class Builder {
private InetAddress inetAddress = null;
private InetSocketAddress inetSocketAddress = null;
private HttpServer httpServer = null;
private ExecutorService executorService = null;
private CollectorRegistry registry = CollectorRegistry.defaultRegistry;
private boolean daemon = false;
private Predicate<String> sampleNameFilter;
Expand Down Expand Up @@ -248,13 +249,26 @@ public Builder withInetSocketAddress(InetSocketAddress address) {
/**
* Use this httpServer. The {@code httpServer} is expected to already be bound to an address.
* Must not be called together with {@link #withPort(int)}, or {@link #withHostname(String)},
* or {@link #withInetAddress(InetAddress)}, or {@link #withInetSocketAddress(InetSocketAddress)}.
* or {@link #withInetAddress(InetAddress)}, or {@link #withInetSocketAddress(InetSocketAddress)},
* or {@link #withExecutorService(ExecutorService)}.
*/
public Builder withHttpServer(HttpServer httpServer) {
this.httpServer = httpServer;
return this;
}

/**
* Optional: ExecutorService used by the {@code httpServer}.
* Must not be called together with the {@link #withHttpServer(HttpServer)}.
*
* @param executorService
* @return
*/
public Builder withExecutorService(ExecutorService executorService) {
this.executorService = executorService;
return this;
}

/**
* By default, the {@link HTTPServer} uses non-daemon threads. Set this to {@code true} to
* run the {@link HTTPServer} with daemon threads.
Expand Down Expand Up @@ -323,12 +337,13 @@ public HTTPServer build() throws IOException {
}

if (httpServer != null) {
assertNull(executorService, "cannot configure 'httpServer' and `executorService'");
assertZero(port, "cannot configure 'httpServer' and 'port' at the same time");
assertNull(hostname, "cannot configure 'httpServer' and 'hostname' at the same time");
assertNull(inetAddress, "cannot configure 'httpServer' and 'inetAddress' at the same time");
assertNull(inetSocketAddress, "cannot configure 'httpServer' and 'inetSocketAddress' at the same time");
assertNull(httpsConfigurator, "cannot configure 'httpServer' and 'httpsConfigurator' at the same time");
return new HTTPServer(httpServer, registry, daemon, sampleNameFilterSupplier, authenticator);
return new HTTPServer(executorService, httpServer, registry, daemon, sampleNameFilterSupplier, authenticator);
} else if (inetSocketAddress != null) {
assertZero(port, "cannot configure 'inetSocketAddress' and 'port' at the same time");
assertNull(hostname, "cannot configure 'inetSocketAddress' and 'hostname' at the same time");
Expand All @@ -350,7 +365,7 @@ public HTTPServer build() throws IOException {
httpServer = HttpServer.create(inetSocketAddress, 3);
}

return new HTTPServer(httpServer, registry, daemon, sampleNameFilterSupplier, authenticator);
return new HTTPServer(executorService, httpServer, registry, daemon, sampleNameFilterSupplier, authenticator);
}

private void assertNull(Object o, String msg) {
Expand All @@ -371,7 +386,7 @@ private void assertZero(int i, String msg) {
* The {@code httpServer} is expected to already be bound to an address
*/
public HTTPServer(HttpServer httpServer, CollectorRegistry registry, boolean daemon) throws IOException {
this(httpServer, registry, daemon, null, null);
this(null, httpServer, registry, daemon, null, null);
}

/**
Expand Down Expand Up @@ -416,7 +431,7 @@ public HTTPServer(String host, int port) throws IOException {
this(new InetSocketAddress(host, port), CollectorRegistry.defaultRegistry, false);
}

private HTTPServer(HttpServer httpServer, CollectorRegistry registry, boolean daemon, Supplier<Predicate<String>> sampleNameFilterSupplier, Authenticator authenticator) {
private HTTPServer(ExecutorService executorService, HttpServer httpServer, CollectorRegistry registry, boolean daemon, Supplier<Predicate<String>> sampleNameFilterSupplier, Authenticator authenticator) {
if (httpServer.getAddress() == null)
throw new IllegalArgumentException("HttpServer hasn't been bound to an address");

Expand All @@ -434,8 +449,12 @@ private HTTPServer(HttpServer httpServer, CollectorRegistry registry, boolean da
if (authenticator != null) {
mContext.setAuthenticator(authenticator);
}
executorService = Executors.newFixedThreadPool(5, NamedDaemonThreadFactory.defaultThreadFactory(daemon));
server.setExecutor(executorService);
if (executorService != null) {
this.executorService = executorService;
} else {
this.executorService = Executors.newFixedThreadPool(5, NamedDaemonThreadFactory.defaultThreadFactory(daemon));
}
server.setExecutor(this.executorService);
start(daemon);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.cert.X509Certificate;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static org.assertj.core.api.Java6Assertions.assertThat;

Expand Down Expand Up @@ -451,6 +453,53 @@ public void testHEADRequestWithSSLAndBasicAuthWrongCredentials() throws GeneralS
}
}

@Test
public void testExecutorService() throws IOException {
ExecutorService executorService = Executors.newFixedThreadPool(20);

HTTPServer httpServer = new HTTPServer.Builder()
.withExecutorService(executorService)
.withRegistry(registry)
.build();

Assert.assertEquals(httpServer.executorService, executorService);

try {
String body = createHttpRequestBuilder(httpServer, "/metrics").build().execute().getBody();
assertThat(body).contains("a 0.0");
assertThat(body).contains("b 0.0");
assertThat(body).contains("c 0.0");
} finally {
httpServer.close();
}
}

@Test(expected = IllegalStateException.class)
public void testExecutorServiceWithHttpServer() throws IOException {
InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 0);

HttpServer externalHttpServer = HttpServer.create(inetSocketAddress, 0);
externalHttpServer.createContext("/metrics", new HTTPServer.HTTPMetricHandler(registry));
externalHttpServer.start();

ExecutorService executorService = Executors.newFixedThreadPool(20);

HTTPServer httpServer = new HTTPServer.Builder()
.withExecutorService(executorService)
.withHttpServer(externalHttpServer)
.withRegistry(registry)
.build();

try {
String body = createHttpRequestBuilder(httpServer, "/metrics").build().execute().getBody();
assertThat(body).contains("a 0.0");
assertThat(body).contains("b 0.0");
assertThat(body).contains("c 0.0");
} finally {
httpServer.close();
}
}

/**
* Encodes authorization credentials
*
Expand Down