Skip to content

Commit

Permalink
Merge pull request #5 from Kerosene-Labs/feat/4
Browse files Browse the repository at this point in the history
#4 adding worker thread name generator
  • Loading branch information
hlafaille authored Aug 9, 2024
2 parents 70661e4 + 94db1aa commit ab262b2
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 9 deletions.
3 changes: 1 addition & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,5 @@
},
"forwardPorts": [
8443
],
"postAttachCommand": "gradle"
]
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>io.kerosenelabs</groupId>
<artifactId>kindling</artifactId>
<packaging>jar</packaging>
<version>0.1.1</version>
<version>0.1.2</version>
<name>kindling</name>
<url>http://maven.apache.org</url>
<dependencies>
Expand Down
35 changes: 32 additions & 3 deletions src/main/java/io/kerosenelabs/kindling/KindlingServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;

import javax.net.ssl.SSLServerSocket;
Expand All @@ -25,6 +26,12 @@
public class KindlingServer {
private static KindlingServer instance = null;
private List<RequestHandler> requestHandlers = new ArrayList<>();
private WorkerThreadNameGenerator workerThreadNameGenerator = new WorkerThreadNameGenerator() {
@Override
public String generate() {
return "req:" + UUID.randomUUID().toString();
}
};

private KindlingServer() {
}
Expand All @@ -36,6 +43,23 @@ public static KindlingServer getInstance() {
return instance;
}

/**
* Set the worker thread name generator. By default, this defaults to a
* anonymous class that generates a random UUID.
*
* @param workerThreadNameGeneratorClass
* @throws KindlingException
*/
public void setWorkerThreadNameGenerator(Class<WorkerThreadNameGenerator> workerThreadNameGeneratorClass)
throws KindlingException {
try {
workerThreadNameGenerator = workerThreadNameGeneratorClass.getDeclaredConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException e) {
throw new KindlingException(e);
}
}

/**
* Install a {@link RequestHandler}.
*
Expand Down Expand Up @@ -99,7 +123,8 @@ public void serve(int port, Path keystorePath, String keyStorePassword) throws K
* @param sslSocket
*/
private void dispatchWorker(SSLSocket sslSocket) {
Thread.startVirtualThread(() -> {
String workerThreadName = workerThreadNameGenerator.generate();
Thread.ofVirtual().name(workerThreadName).start(() -> {
try (
InputStream inputStream = sslSocket.getInputStream();
InputStreamReader inputStreamReder = new InputStreamReader(inputStream);
Expand All @@ -113,7 +138,11 @@ private void dispatchWorker(SSLSocket sslSocket) {
HttpResponse response = null;
for (RequestHandler requestHandler : requestHandlers) {
if (requestHandler.accepts(httpRequest.getHttpMethod(), httpRequest.getResource())) {
response = requestHandler.handle(httpRequest);
try {
response = requestHandler.handle(httpRequest);
} catch (Exception e) {
response = requestHandler.handleError(e);
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/kerosenelabs/kindling/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public HttpResponse handle(HttpRequest httpRequest) throws KindlingException {
.status(HttpStatus.OK)
.headers(new HashMap<>() {
{
put("Content-Type", "text/html");
put("Content-Type", "application/json");
}
})
.content("<h1>Hello from Kindling!</h1>")
.content("{\"key\": \"value\"}")
.build();
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.kerosenelabs.kindling;

public interface WorkerThreadNameGenerator {
public String generate();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ public abstract class RequestHandler {
* Called from {@link io.kerosenelabs.kindling.Server} if an error occurs during
* {@link RequestHandler#handle(HttpRequest)}
*
* @param The throwable that occurred
* @return
* @throws KindlingException
*/
public HttpResponse doError() {
public HttpResponse handleError(Throwable t) {
return new HttpResponse.Builder()
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.content("Internal Server Error")
Expand Down

0 comments on commit ab262b2

Please sign in to comment.