Skip to content

Commit

Permalink
Add graceful shutdown option.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Jul 18, 2022
1 parent 2a75891 commit cc42960
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 96 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Proteus Changelog.
## Unreleased
### No issue

**Bump version**


[2a75891265dacc6](https://github.com/noboomu/proteus/commit/2a75891265dacc6) noboomu *2022-07-07 01:07:12*

**Fix for typo**


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.sinistral.proteus;

import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.util.concurrent.MoreExecutors;
Expand All @@ -19,14 +18,15 @@
import io.sinistral.proteus.server.handlers.HandlerGenerator;
import io.sinistral.proteus.server.handlers.ServerDefaultHttpHandler;
import io.sinistral.proteus.services.BaseService;
import io.sinistral.proteus.utilities.SecurityOps;
import io.sinistral.proteus.utilities.SecurityUtilities;
import io.sinistral.proteus.utilities.TablePrinter;
import io.undertow.Undertow;
import io.undertow.Undertow.ListenerInfo;
import io.undertow.UndertowOptions;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.RoutingHandler;
import io.undertow.server.handlers.GracefulShutdownHandler;
import io.undertow.server.session.SessionAttachmentHandler;
import io.undertow.util.Headers;
import io.undertow.util.Methods;
Expand All @@ -50,7 +50,6 @@
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -71,6 +70,7 @@
* @author jbauer
*/

@SuppressWarnings("UnusedReturnValue")
public class ProteusApplication {

private static Logger log = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(ProteusApplication.class.getCanonicalName());
Expand Down Expand Up @@ -310,7 +310,7 @@ public void buildServer()

final Instant compilationStartTime = Instant.now();

ExecutorService handlerExecutor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);
ExecutorService handlerCompilationExecutor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

CountDownLatch countDownLatch = new CountDownLatch(registeredControllers.size());

Expand All @@ -321,7 +321,7 @@ public void buildServer()
for (Class<?> controllerClass : registeredControllers)
{

handlerExecutor.submit(() -> {
handlerCompilationExecutor.submit(() -> {

try
{
Expand Down Expand Up @@ -388,7 +388,7 @@ public void buildServer()
sessionAttachmentHandler = injector.getInstance(SessionAttachmentHandler.class);
} catch (Exception e)
{
log.warn("No session attachment handler found.");
log.debug("No session attachment handler found.");
}

if (sessionAttachmentHandler != null)
Expand All @@ -399,6 +399,12 @@ public void buildServer()
handler = sessionAttachmentHandler;
}


if(config.hasPath("undertow.gracefulShutdown") && config.getBoolean("undertow.gracefulShutdown"))
{
handler = new GracefulShutdownHandler(handler);
}

int httpPort = config.getInt("application.ports.http");

if (System.getProperty("http.port") != null)
Expand Down Expand Up @@ -435,10 +441,10 @@ public void buildServer()
httpsPort = Integer.parseInt(System.getProperty("https.port"));
}

KeyStore keyStore = SecurityOps.loadKeyStore(config.getString("undertow.ssl.keystorePath"), config.getString("undertow.ssl.keystorePassword"));
KeyStore trustStore = SecurityOps.loadKeyStore(config.getString("undertow.ssl.truststorePath"), config.getString("undertow.ssl.truststorePassword"));
KeyStore keyStore = SecurityUtilities.loadKeyStore(config.getString("undertow.ssl.keystorePath"), config.getString("undertow.ssl.keystorePassword"));
KeyStore trustStore = SecurityUtilities.loadKeyStore(config.getString("undertow.ssl.truststorePath"), config.getString("undertow.ssl.truststorePassword"));

undertowBuilder.addHttpsListener(httpsPort, config.getString("application.host"), SecurityOps.createSSLContext(keyStore, trustStore, config.getString("undertow.ssl.keystorePassword")));
undertowBuilder.addHttpsListener(httpsPort, config.getString("application.host"), SecurityUtilities.createSSLContext(keyStore, trustStore, config.getString("undertow.ssl.keystorePassword")));

} catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.google.inject.Inject;
import io.sinistral.proteus.server.predicates.ServerPredicates;
import io.sinistral.proteus.utilities.DataOps;
import io.sinistral.proteus.utilities.DataUtilities;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.handlers.form.FormData;
import io.undertow.server.handlers.form.FormDataParser;
Expand Down Expand Up @@ -161,7 +161,7 @@ private static Path formValueFileItemPath(FormData.FileItem fileItem) {

path.toFile().deleteOnExit();

DataOps.writeStreamToPath(fileItem.getInputStream(), path);
DataUtilities.writeStreamToPath(fileItem.getInputStream(), path);

return path;

Expand Down Expand Up @@ -256,7 +256,7 @@ private static java.util.Optional<ByteBuffer> formValueBuffer(final HttpServerEx
{
log.trace("fileItem: {} is in memory {}", fileItem, fileItem.getFileSize());
}
return DataOps.fileItemToBuffer(fileItem);
return DataUtilities.fileItemToBuffer(fileItem);
}

} catch (Exception e)
Expand Down Expand Up @@ -299,7 +299,7 @@ private static <T> T formValueModel(final FormData.FormValue formValue, final Ty
{
try
{
ByteBuffer byteBuffer = DataOps.fileItemToBuffer(formValue.getFileItem());
ByteBuffer byteBuffer = DataUtilities.fileItemToBuffer(formValue.getFileItem());

return parseTypedXML(type, byteBuffer.array());

Expand Down Expand Up @@ -327,7 +327,7 @@ else if (formValue.getHeaders().get(Headers.CONTENT_TYPE) == null || (formValue.
{
try
{
ByteBuffer byteBuffer = DataOps.fileItemToBuffer(formValue.getFileItem());
ByteBuffer byteBuffer = DataUtilities.fileItemToBuffer(formValue.getFileItem());

return parseTypedJson(type, byteBuffer.array());

Expand Down Expand Up @@ -358,7 +358,7 @@ private static <T> T formValueModel(final FormData.FormValue formValue, final Cl
{
try
{
ByteBuffer byteBuffer = DataOps.fileItemToBuffer(formValue.getFileItem());
ByteBuffer byteBuffer = DataUtilities.fileItemToBuffer(formValue.getFileItem());

return parseTypedXML(type, byteBuffer.array());

Expand Down Expand Up @@ -386,7 +386,7 @@ else if (formValue.getHeaders().get(Headers.CONTENT_TYPE) == null || (formValue.
{
try
{
ByteBuffer byteBuffer = DataOps.fileItemToBuffer(formValue.getFileItem());
ByteBuffer byteBuffer = DataUtilities.fileItemToBuffer(formValue.getFileItem());

return parseTypedJson(type, byteBuffer.array());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.sinistral.proteus.utilities;

import com.typesafe.config.Config;
import io.undertow.server.handlers.form.FormData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -13,10 +14,11 @@
import java.nio.channels.WritableByteChannel;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Optional;

public class DataOps {
public class DataUtilities {

private static final Logger logger = LoggerFactory.getLogger(DataOps.class.getName());
private static final Logger logger = LoggerFactory.getLogger(DataUtilities.class.getName());

public static void writeStreamToPath(InputStream inputStream, Path path) throws IOException
{
Expand All @@ -33,12 +35,14 @@ public static void writeStreamToPath(InputStream inputStream, Path path) throws
}

public static ByteBuffer streamToBuffer(InputStream stream) throws IOException {

return ByteBuffer.wrap(stream.readAllBytes());
}

public static ByteBuffer fileItemToBuffer(FormData.FileItem fileItem) throws Exception
{
if(fileItem.isInMemory())

if (fileItem.isInMemory())
{
return ByteBuffer.wrap(fileItem.getInputStream().readAllBytes());
}
Expand All @@ -63,6 +67,7 @@ public static ByteBuffer readAllBytes(Path fp) throws IOException {
}
}


public static void fastChannelCopy(final ReadableByteChannel src, final WritableByteChannel destination) throws IOException {

final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/**
* @author jbauer
*/
public class SecurityOps
public class SecurityUtilities
{
public static SSLContext createSSLContext(final KeyStore keyStore, final KeyStore trustStore, final String password) throws Exception
{
Expand Down Expand Up @@ -47,7 +47,7 @@ public static KeyStore loadKeyStore(String name, String password) throws Excepti
InputStream stream = null;

if (!storeFile.exists()) {
stream = SecurityOps.class.getResourceAsStream("/" + name);
stream = SecurityUtilities.class.getResourceAsStream("/" + name);
} else {
stream = Files.newInputStream(Paths.get(name));
}
Expand Down
Loading

0 comments on commit cc42960

Please sign in to comment.