-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is a refactor of route execution, excluding filters (those will follow in #8422). The goals are: * Moving away from reactive APIs where possible. * Streamlining route execution so that it will be possible to have pre-route filters, and filters that read the request body. This initial patch factors most of the routing code out of RoutingInboundHandler (the response writing code remains), and moves away from reactive for HttpContentProcessor and the route parameter fulfillment code (now called BaseRouteCompleter and FormRouteCompleter). The goal of this PR is to get to a point where I am confident that it will be easy to delay filter execution until the body is available. Ideally the full request lifecycle, from before route resolution, through filter execution, to route execution, will be contained in the RouteRunner class.
- Loading branch information
Showing
31 changed files
with
2,372 additions
and
1,602 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
113 changes: 0 additions & 113 deletions
113
...y/src/main/java/io/micronaut/http/server/netty/AbstractBufferingHttpContentProcessor.java
This file was deleted.
Oops, something went wrong.
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
107 changes: 107 additions & 0 deletions
107
http-server-netty/src/main/java/io/micronaut/http/server/netty/BaseRouteCompleter.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,107 @@ | ||
/* | ||
* Copyright 2017-2022 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.http.server.netty; | ||
|
||
import io.micronaut.core.annotation.Internal; | ||
import io.micronaut.core.annotation.Nullable; | ||
import io.micronaut.core.io.buffer.ReferenceCounted; | ||
import io.micronaut.http.server.netty.multipart.NettyCompletedFileUpload; | ||
import io.micronaut.web.router.RouteMatch; | ||
import io.netty.buffer.ByteBufHolder; | ||
import io.netty.util.ReferenceCountUtil; | ||
|
||
/** | ||
* This class consumes objects produced by a {@link HttpContentProcessor}. Normally it just adds | ||
* the data to the {@link NettyHttpRequest}. For multipart data, there is additional logic in | ||
* {@link FormRouteCompleter} that also dynamically binds parameters, though usually this is done | ||
* by the {@link io.micronaut.http.server.binding.RequestArgumentSatisfier}. | ||
* | ||
* @since 4.0.0 | ||
* @author Jonas Konrad | ||
*/ | ||
@Internal | ||
class BaseRouteCompleter { | ||
final NettyHttpRequest<?> request; | ||
volatile boolean needsInput = true; | ||
/** | ||
* Optional runnable that may be called from other threads (i.e. downstream subscribers) to | ||
* notify that {@link #needsInput} may have changed. | ||
*/ | ||
@Nullable | ||
volatile Runnable checkDemand; | ||
RouteMatch<?> routeMatch; | ||
boolean execute = false; | ||
|
||
public BaseRouteCompleter(NettyHttpRequest<?> request, RouteMatch<?> routeMatch) { | ||
this.request = request; | ||
this.routeMatch = routeMatch; | ||
} | ||
|
||
final void add(Object message) throws Throwable { | ||
try { | ||
if (request.destroyed) { | ||
// we don't want this message anymore | ||
ReferenceCountUtil.release(message); | ||
return; | ||
} | ||
|
||
if (message instanceof ByteBufHolder bbh) { | ||
addHolder(bbh); | ||
} else { | ||
((NettyHttpRequest) request).setBody(message); | ||
needsInput = true; | ||
} | ||
|
||
// now, a pseudo try-finally with addSuppressed. | ||
} catch (Throwable t) { | ||
try { | ||
ReferenceCountUtil.release(message); | ||
} catch (Throwable u) { | ||
t.addSuppressed(u); | ||
} | ||
throw t; | ||
} | ||
|
||
// the upstream processor gives us ownership of the message, so we need to release it. | ||
ReferenceCountUtil.release(message); | ||
} | ||
|
||
protected void addHolder(ByteBufHolder holder) { | ||
request.addContent(holder); | ||
needsInput = true; | ||
} | ||
|
||
void completeSuccess() { | ||
execute = true; | ||
} | ||
|
||
void completeFailure(Throwable failure) { | ||
if (!execute) { | ||
// discard parameters that have already been bound | ||
for (Object toDiscard : routeMatch.getVariableValues().values()) { | ||
if (toDiscard instanceof ReferenceCounted rc) { | ||
rc.release(); | ||
} | ||
if (toDiscard instanceof io.netty.util.ReferenceCounted rc) { | ||
rc.release(); | ||
} | ||
if (toDiscard instanceof NettyCompletedFileUpload fu) { | ||
fu.discard(); | ||
} | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.