forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure large payloads don't affect the invoking thread of a Resource …
…Method Fixes: quarkusio#31606
- Loading branch information
Showing
5 changed files
with
138 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...e/src/main/java/org/jboss/resteasy/reactive/server/handlers/SwitchToEventLoopHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.jboss.resteasy.reactive.server.handlers; | ||
|
||
import java.util.concurrent.Executor; | ||
import java.util.function.Supplier; | ||
|
||
import org.jboss.resteasy.reactive.server.core.BlockingOperationSupport; | ||
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext; | ||
import org.jboss.resteasy.reactive.server.spi.ServerRestHandler; | ||
|
||
/** | ||
* A special handler whose only purpose is to switch execution from a worker thread to the event loop | ||
* This can be needed when for example a large payload is read but the JAX-RS Resource method | ||
* is intended to be run on the event loop. | ||
*/ | ||
public class SwitchToEventLoopHandler implements ServerRestHandler { | ||
|
||
private final Supplier<Executor> workerExecutorSupplier; | ||
|
||
public SwitchToEventLoopHandler(Supplier<Executor> workerExecutorSupplier) { | ||
this.workerExecutorSupplier = workerExecutorSupplier; | ||
} | ||
|
||
@Override | ||
public void handle(ResteasyReactiveRequestContext requestContext) throws Exception { | ||
if (BlockingOperationSupport.isBlockingAllowed()) { // we are on a worker thread | ||
requestContext.suspend(); | ||
workerExecutorSupplier.get().execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
requestContext.resumeOnEventLoop(); | ||
} | ||
}); | ||
} | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
...st/java/org/jboss/resteasy/reactive/server/vertx/test/BodyPayloadBlockingAllowedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package org.jboss.resteasy.reactive.server.vertx.test; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
|
||
import java.util.Base64; | ||
|
||
import jakarta.ws.rs.Consumes; | ||
import jakarta.ws.rs.POST; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.core.MediaType; | ||
|
||
import org.jboss.resteasy.reactive.server.core.BlockingOperationSupport; | ||
import org.jboss.resteasy.reactive.server.vertx.test.framework.ResteasyReactiveUnitTest; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.smallrye.mutiny.Uni; | ||
|
||
public class BodyPayloadBlockingAllowedTest { | ||
|
||
@RegisterExtension | ||
static ResteasyReactiveUnitTest test = new ResteasyReactiveUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(TestResource.class)); | ||
|
||
@Test | ||
void testSmallRequestForNonBlocking() { | ||
doTest(5_000, "non-blocking", false); | ||
} | ||
|
||
@Test | ||
void testLargeRequestForNonBlocking() { | ||
doTest(5_000_000, "non-blocking", false); | ||
} | ||
|
||
@Test | ||
void testSmallRequestForBlocking() { | ||
doTest(5_000, "blocking", true); | ||
} | ||
|
||
@Test | ||
void testLargeRequestForBlocking() { | ||
doTest(5_000_000, "blocking", true); | ||
} | ||
|
||
private static void doTest(int size, String path, boolean blockingAllowed) { | ||
given() // | ||
.body(String.format("{\"data\":\"%s\"}", getBase64String(size))) | ||
.header("Content-Type", MediaType.TEXT_PLAIN) | ||
.when().post("/test/" + path) | ||
.then() | ||
.statusCode(200) | ||
.body(equalTo("" + blockingAllowed)); | ||
} | ||
|
||
private static String getBase64String(int size) { | ||
return Base64.getEncoder().encodeToString(new byte[size]); | ||
} | ||
|
||
@Path("test") | ||
public static class TestResource { | ||
|
||
@Path("non-blocking") | ||
@POST | ||
@Consumes(MediaType.TEXT_PLAIN) | ||
@Produces(MediaType.TEXT_PLAIN) | ||
public Uni<Boolean> nonBlocking(String request) { | ||
return Uni.createFrom().item(BlockingOperationSupport::isBlockingAllowed); | ||
} | ||
|
||
@Path("blocking") | ||
@POST | ||
@Consumes(MediaType.TEXT_PLAIN) | ||
@Produces(MediaType.TEXT_PLAIN) | ||
public Boolean blocking(String request) { | ||
return BlockingOperationSupport.isBlockingAllowed(); | ||
} | ||
|
||
} | ||
} |