-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement support for NDJSON streaming in vertex-web
- Loading branch information
Showing
5 changed files
with
193 additions
and
8 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
90 changes: 90 additions & 0 deletions
90
...ions/vertx-web/runtime/src/main/java/io/quarkus/vertx/web/runtime/MultiNdjsonSupport.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,90 @@ | ||
package io.quarkus.vertx.web.runtime; | ||
|
||
import org.reactivestreams.Subscriber; | ||
import org.reactivestreams.Subscription; | ||
|
||
import io.smallrye.mutiny.Multi; | ||
import io.vertx.core.AsyncResult; | ||
import io.vertx.core.MultiMap; | ||
import io.vertx.core.buffer.Buffer; | ||
import io.vertx.core.http.HttpHeaders; | ||
import io.vertx.core.http.HttpServerResponse; | ||
import io.vertx.core.json.Json; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
@SuppressWarnings("ReactiveStreamsSubscriberImplementation") | ||
public class MultiNdjsonSupport { | ||
|
||
private MultiNdjsonSupport() { | ||
// Avoid direct instantiation. | ||
} | ||
|
||
private static void initialize(HttpServerResponse response, RoutingContext rc) { | ||
if (response.bytesWritten() == 0) { | ||
MultiMap headers = response.headers(); | ||
if (rc.getAcceptableContentType() == null) { | ||
headers.set(HttpHeaders.CONTENT_TYPE, "application/x-ndjson"); | ||
} | ||
response.setChunked(true); | ||
} | ||
} | ||
|
||
public static void subscribeString(Multi<String> multi, RoutingContext rc) { | ||
write(multi.map(s -> Buffer.buffer("\"" + s + "\"")), rc); | ||
} | ||
|
||
public static void subscribeObject(Multi<Object> multi, RoutingContext rc) { | ||
write(multi.map(o -> Buffer.buffer(Json.encode(o) + "\n")), rc); | ||
} | ||
|
||
private static void onWriteDone(Subscription subscription, AsyncResult<Void> ar, RoutingContext rc) { | ||
if (ar.failed()) { | ||
rc.fail(ar.cause()); | ||
} else { | ||
subscription.request(1); | ||
} | ||
} | ||
|
||
public static void write(Multi<Buffer> multi, RoutingContext rc) { | ||
HttpServerResponse response = rc.response(); | ||
multi.subscribe().withSubscriber(new Subscriber<Buffer>() { | ||
Subscription upstream; | ||
|
||
@Override | ||
public void onSubscribe(Subscription subscription) { | ||
this.upstream = subscription; | ||
this.upstream.request(1); | ||
} | ||
|
||
@Override | ||
public void onNext(Buffer item) { | ||
initialize(response, rc); | ||
response.write(item, ar -> onWriteDone(upstream, ar, rc)); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable throwable) { | ||
rc.fail(throwable); | ||
} | ||
|
||
@Override | ||
public void onComplete() { | ||
endOfStream(response, rc); | ||
} | ||
}); | ||
} | ||
|
||
private static void endOfStream(HttpServerResponse response, RoutingContext rc) { | ||
if (response.bytesWritten() == 0) { // No item | ||
MultiMap headers = response.headers(); | ||
if (rc.getAcceptableContentType() == null) { | ||
headers.set(HttpHeaders.CONTENT_TYPE, "application/x-ndjson"); | ||
} | ||
} | ||
response.end(); | ||
} | ||
|
||
public static boolean isNdjson(Multi<?> multi) { | ||
return multi instanceof NdjsonMulti; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
extensions/vertx-web/runtime/src/main/java/io/quarkus/vertx/web/runtime/NdjsonMulti.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,25 @@ | ||
package io.quarkus.vertx.web.runtime; | ||
|
||
import io.smallrye.mutiny.Multi; | ||
import io.smallrye.mutiny.infrastructure.Infrastructure; | ||
import io.smallrye.mutiny.operators.AbstractMulti; | ||
import io.smallrye.mutiny.subscription.MultiSubscriber; | ||
|
||
/** | ||
* Just a wrapped to capture the fact that the items must be written as SSE. | ||
* | ||
* @param <T> the type of item. | ||
*/ | ||
public class NdjsonMulti<T> extends AbstractMulti<T> { | ||
|
||
private final Multi<T> multi; | ||
|
||
public NdjsonMulti(Multi<T> multi) { | ||
this.multi = multi; | ||
} | ||
|
||
@Override | ||
public void subscribe(MultiSubscriber<? super T> subscriber) { | ||
multi.subscribe(Infrastructure.onMultiSubscription(multi, subscriber)); | ||
} | ||
} |