Skip to content

Commit

Permalink
✨ Use virtual threads for static and proxy resources
Browse files Browse the repository at this point in the history
  • Loading branch information
ujibang committed May 20, 2024
1 parent 388e599 commit d165988
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions core/src/main/java/org/restheart/Bootstrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import org.restheart.handlers.RequestLogger;
import org.restheart.handlers.RequestNotManagedHandler;
import org.restheart.handlers.TracingInstrumentationHandler;
import org.restheart.handlers.WorkingThreadsPoolDispatcher;
import org.restheart.handlers.injectors.AuthHeadersRemover;
import org.restheart.handlers.injectors.ConduitInjector;
import org.restheart.handlers.injectors.PipelineInfoInjector;
Expand Down Expand Up @@ -115,13 +116,12 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xnio.OptionMap;
import org.xnio.Options;
import org.xnio.SslClientAuthMode;
import org.xnio.Xnio;

import static io.undertow.Handlers.resource;
import io.undertow.Undertow;
import io.undertow.UndertowOptions;
import io.undertow.protocols.ssl.UndertowXnioSsl;
import io.undertow.server.handlers.AllowedMethodsHandler;
import io.undertow.server.handlers.GracefulShutdownHandler;
import io.undertow.server.handlers.HttpContinueAcceptingHandler;
Expand Down Expand Up @@ -767,10 +767,6 @@ private static void plugProxies(final Configuration conf, final Set<PluginRecord
return;
}

final var xnio = Xnio.getInstance();

final var optionMap = OptionMap.create(Options.SSL_CLIENT_AUTH_MODE, SslClientAuthMode.REQUIRED, Options.SSL_STARTTLS, true);

var proxyClient = new LoadBalancingProxyClient()
.setConnectionsPerThread(proxy.connectionPerThread())
.setSoftMaxConnectionsPerThread(proxy.softMaxConnectionsPerThread())
Expand All @@ -780,8 +776,14 @@ private static void plugProxies(final Configuration conf, final Set<PluginRecord

proxy.proxyPass().stream().forEach(pp -> {
try {
var byteBufferPool = new ThreadAwareByteBufferPool(
configuration.coreModule().directBuffers(),
configuration.coreModule().bufferSize(),
configuration.coreModule().buffersPooling());

var xnioSsl = new UndertowXnioSsl(Xnio.getInstance(), OptionMap.EMPTY, byteBufferPool);
var uri = new URI(pp);
proxyClient.addHost(uri, xnio.getSslProvider(optionMap));
proxyClient.addHost(uri, xnioSsl);
} catch(URISyntaxException t) {
LOGGER.warn("Invalid location URI {}, resource {} not bound ", proxy.location(), pp);
} catch (GeneralSecurityException ex) {
Expand All @@ -795,6 +797,7 @@ private static void plugProxies(final Configuration conf, final Set<PluginRecord
.build();

var rhProxy = pipe(
new WorkingThreadsPoolDispatcher(),
new PipelineInfoInjector(),
new TracingInstrumentationHandler(),
new RequestLogger(),
Expand Down Expand Up @@ -886,7 +889,7 @@ private static void plugStaticResourcesHandlers(final Configuration conf) {
if (file.exists()) {
var handler = resource(new FileResourceManager(file, 3)).addWelcomeFiles(sr.welcomeFile()).setDirectoryListingEnabled(false);

var ph = PipelinedHandler.pipe(new PipelineInfoInjector(), new RequestLogger(), PipelinedWrappingHandler.wrap(handler));
var ph = PipelinedHandler.pipe(new PipelineInfoInjector(), new RequestLogger(), new WorkingThreadsPoolDispatcher(PipelinedWrappingHandler.wrap(handler)));

PluginsRegistryImpl.getInstance().plugPipeline(sr.where(), ph, new PipelineInfo(STATIC_RESOURCE, sr.where(), sr.what()));

Expand Down

0 comments on commit d165988

Please sign in to comment.