-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move body writing logic into http-server #11342
Open
yawkat
wants to merge
5
commits into
4.8.x
Choose a base branch
from
abstract-body-writing
base: 4.8.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
175 changes: 175 additions & 0 deletions
175
core-reactive/src/main/java/io/micronaut/core/async/subscriber/LazySendingSubscriber.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,175 @@ | ||
/* | ||
* Copyright 2017-2024 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.core.async.subscriber; | ||
|
||
import io.micronaut.core.annotation.Internal; | ||
import io.micronaut.core.annotation.NonNull; | ||
import io.micronaut.core.execution.DelayedExecutionFlow; | ||
import io.micronaut.core.execution.ExecutionFlow; | ||
import org.reactivestreams.Publisher; | ||
import org.reactivestreams.Subscriber; | ||
import org.reactivestreams.Subscription; | ||
import reactor.core.CorePublisher; | ||
import reactor.core.CoreSubscriber; | ||
import reactor.core.publisher.Operators; | ||
import reactor.core.publisher.Signal; | ||
import reactor.util.context.Context; | ||
|
||
/** | ||
* This class waits for the first item of a publisher before completing an ExecutionFlow with a | ||
* publisher containing the same items. | ||
* | ||
* @param <T> The publisher item type | ||
* @since 4.8.0 | ||
* @author Jonas Konrad | ||
*/ | ||
@Internal | ||
public final class LazySendingSubscriber<T> implements CoreSubscriber<T>, CorePublisher<T>, Subscription { | ||
private final DelayedExecutionFlow<Publisher<T>> result = DelayedExecutionFlow.create(); | ||
private boolean receivedFirst = false; | ||
private volatile boolean sentFirst = false; | ||
private boolean sendingFirst = false; | ||
private T first; | ||
private Subscription upstream; | ||
private volatile CoreSubscriber<? super T> downstream; | ||
private Signal<? extends T> heldBackSignal; | ||
|
||
private LazySendingSubscriber() { | ||
} | ||
|
||
/** | ||
* Create an {@link ExecutionFlow} that waits for the first item of the given publisher. If | ||
* there is an error before the first item, the flow will fail. If there is no error, the flow | ||
* will complete with a publisher containing all items, including the first one. | ||
* | ||
* @param input The input stream | ||
* @return A flow that will complete with the same stream | ||
* @param <T> The item type | ||
*/ | ||
@NonNull | ||
public static <T> ExecutionFlow<Publisher<T>> create(@NonNull Publisher<T> input) { | ||
LazySendingSubscriber<T> subscriber = new LazySendingSubscriber<>(); | ||
input.subscribe(subscriber); | ||
return subscriber.result; | ||
} | ||
|
||
@Override | ||
public Context currentContext() { | ||
return downstream == null ? Context.empty() : downstream.currentContext(); | ||
} | ||
|
||
@Override | ||
public void onSubscribe(Subscription s) { | ||
upstream = s; | ||
s.request(1); | ||
} | ||
|
||
@Override | ||
public void onNext(T t) { | ||
if (!receivedFirst) { | ||
receivedFirst = true; | ||
first = t; | ||
result.complete(this); | ||
} else { | ||
downstream.onNext(t); | ||
} | ||
} | ||
|
||
@Override | ||
public void onError(Throwable t) { | ||
if (receivedFirst) { | ||
Subscriber<? super T> d; | ||
synchronized (this) { | ||
d = downstream; | ||
if (d == null || !sentFirst) { | ||
heldBackSignal = Signal.error(t); | ||
return; | ||
} | ||
} | ||
d.onError(t); | ||
} else { | ||
receivedFirst = true; | ||
result.completeExceptionally(t); | ||
} | ||
} | ||
|
||
@Override | ||
public void onComplete() { | ||
if (!receivedFirst) { | ||
onNext(null); | ||
} | ||
|
||
Subscriber<? super T> d; | ||
synchronized (this) { | ||
d = downstream; | ||
if (d == null || !sentFirst) { | ||
heldBackSignal = Signal.complete(); | ||
return; | ||
} | ||
} | ||
d.onComplete(); | ||
} | ||
|
||
@Override | ||
public void subscribe(CoreSubscriber<? super T> subscriber) { | ||
synchronized (this) { | ||
downstream = subscriber; | ||
} | ||
subscriber.onSubscribe(this); | ||
} | ||
|
||
@Override | ||
public void subscribe(Subscriber<? super T> s) { | ||
subscribe(Operators.toCoreSubscriber(s)); | ||
} | ||
|
||
@Override | ||
public void request(long n) { | ||
if (!sentFirst && !sendingFirst) { | ||
sendingFirst = true; | ||
if (first != null) { | ||
downstream.onNext(first); // note: this can trigger reentrancy! | ||
first = null; | ||
} | ||
Signal<? extends T> heldBackSignal; | ||
synchronized (this) { | ||
sentFirst = true; | ||
heldBackSignal = this.heldBackSignal; | ||
} | ||
if (heldBackSignal != null) { | ||
heldBackSignal.accept(downstream); | ||
return; | ||
} | ||
n--; | ||
if (n <= 0) { | ||
return; | ||
} | ||
} | ||
|
||
upstream.request(n); | ||
} | ||
|
||
@Override | ||
public void cancel() { | ||
if (!sentFirst) { | ||
sentFirst = true; | ||
T t = first; | ||
first = null; | ||
Operators.onNextDropped(t, currentContext()); | ||
} | ||
upstream.cancel(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
core/src/main/java/io/micronaut/core/util/functional/ThrowingConsumer.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 @@ | ||
/* | ||
* Copyright 2017-2024 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.core.util.functional; | ||
|
||
/** | ||
* Consumer with a generic exception. | ||
* | ||
* @param <T> the type accepted by this consumer | ||
* @param <E> the type of exception thrown from the supplier | ||
* @author Jonas Konrad | ||
* @since 4.8.0 | ||
*/ | ||
@FunctionalInterface | ||
public interface ThrowingConsumer<T, E extends Throwable> { | ||
/** | ||
* Consume the value. | ||
* | ||
* @param t The value | ||
* @throws E The generic exception | ||
*/ | ||
void accept(T t) throws E; | ||
} |
135 changes: 135 additions & 0 deletions
135
http-client-jdk/src/main/java/io/micronaut/http/client/jdk/ByteBodySubscriber.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,135 @@ | ||
/* | ||
* Copyright 2017-2024 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.client.jdk; | ||
|
||
import io.micronaut.core.annotation.Internal; | ||
import io.micronaut.http.body.CloseableByteBody; | ||
import io.micronaut.http.body.ReactiveByteBufferByteBody; | ||
import io.micronaut.http.body.stream.BodySizeLimits; | ||
import io.micronaut.http.body.stream.BufferConsumer; | ||
|
||
import java.net.http.HttpResponse; | ||
import java.nio.ByteBuffer; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionStage; | ||
import java.util.concurrent.Flow; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
/** | ||
* {@link HttpResponse.BodySubscriber} implementation that pushes data into a | ||
* {@link ReactiveByteBufferByteBody.SharedBuffer}. | ||
* | ||
* @since 4.8.0 | ||
* @author Jonas Konrad | ||
*/ | ||
@Internal | ||
final class ByteBodySubscriber implements HttpResponse.BodySubscriber<CloseableByteBody>, BufferConsumer.Upstream { | ||
private final ReactiveByteBufferByteBody.SharedBuffer sharedBuffer; | ||
private final CloseableByteBody root; | ||
private final AtomicLong demand = new AtomicLong(0); | ||
private Flow.Subscription subscription; | ||
private boolean cancelled; | ||
private volatile boolean disregardBackpressure; | ||
|
||
public ByteBodySubscriber(BodySizeLimits limits) { | ||
sharedBuffer = new ReactiveByteBufferByteBody.SharedBuffer(limits, this); | ||
root = new ReactiveByteBufferByteBody(sharedBuffer); | ||
} | ||
|
||
@Override | ||
public CompletionStage<CloseableByteBody> getBody() { | ||
return CompletableFuture.completedFuture(root); | ||
} | ||
|
||
@Override | ||
public void onSubscribe(Flow.Subscription subscription) { | ||
boolean initialDemand; | ||
boolean cancelled; | ||
synchronized (this) { | ||
this.subscription = subscription; | ||
cancelled = this.cancelled; | ||
initialDemand = demand.get() > 0; | ||
} | ||
if (cancelled) { | ||
subscription.cancel(); | ||
} else if (initialDemand) { | ||
subscription.request(disregardBackpressure ? Long.MAX_VALUE : 1); | ||
} | ||
} | ||
|
||
@Override | ||
public void onNext(List<ByteBuffer> item) { | ||
for (ByteBuffer buffer : item) { | ||
int n = buffer.remaining(); | ||
demand.addAndGet(-n); | ||
sharedBuffer.add(buffer); | ||
} | ||
if (demand.get() > 0) { | ||
subscription.request(1); | ||
} | ||
} | ||
|
||
@Override | ||
public void onError(Throwable throwable) { | ||
sharedBuffer.error(throwable); | ||
} | ||
|
||
@Override | ||
public void onComplete() { | ||
sharedBuffer.complete(); | ||
} | ||
|
||
@Override | ||
public void start() { | ||
Flow.Subscription initialDemand; | ||
synchronized (this) { | ||
initialDemand = subscription; | ||
demand.set(1); | ||
} | ||
if (initialDemand != null) { | ||
initialDemand.request(1); | ||
} | ||
} | ||
|
||
@Override | ||
public void onBytesConsumed(long bytesConsumed) { | ||
long prev = demand.getAndAdd(bytesConsumed); | ||
if (prev <= 0 && prev + bytesConsumed > 0) { | ||
subscription.request(1); | ||
} | ||
} | ||
|
||
@Override | ||
public void allowDiscard() { | ||
Flow.Subscription subscription; | ||
synchronized (this) { | ||
cancelled = true; | ||
subscription = this.subscription; | ||
} | ||
if (subscription != null) { | ||
subscription.cancel(); | ||
} | ||
} | ||
|
||
@Override | ||
public void disregardBackpressure() { | ||
disregardBackpressure = true; | ||
if (subscription != null) { | ||
subscription.request(Long.MAX_VALUE); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI: this has simply been moved from ReactiveByteBufferByteBody where it was an inner class previously, it's not new code.