-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathHttpRequestFlowAdapter.java
More file actions
111 lines (94 loc) · 3.15 KB
/
Copy pathHttpRequestFlowAdapter.java
File metadata and controls
111 lines (94 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH
//
// This file is part of the Restate Java SDK,
// which is released under the MIT license.
//
// You can find a copy of the license in file LICENSE in the root
// directory of this repository or package, or at
// https://github.com/restatedev/sdk-java/blob/main/LICENSE
package dev.restate.sdk.http.vertx;
import dev.restate.common.Slice;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpServerRequest;
import java.nio.ByteBuffer;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.concurrent.Flow;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
class HttpRequestFlowAdapter implements Flow.Publisher<Slice> {
private static final Logger LOG = LogManager.getLogger(HttpRequestFlowAdapter.class);
private final HttpServerRequest httpServerRequest;
private Flow.Subscriber<? super Slice> inputMessagesSubscriber;
private long subscriberRequest = 0;
private final Queue<ByteBuffer> buffers;
HttpRequestFlowAdapter(HttpServerRequest httpServerRequest) {
this.httpServerRequest = httpServerRequest;
this.buffers = new ArrayDeque<>();
}
@Override
public void subscribe(Flow.Subscriber<? super Slice> subscriber) {
this.inputMessagesSubscriber = subscriber;
this.inputMessagesSubscriber.onSubscribe(
new Flow.Subscription() {
@Override
public void request(long l) {
handleSubscriptionRequest(l);
}
@Override
public void cancel() {
closeRequest();
}
});
// Set request handlers
this.httpServerRequest.handler(this::handleIncomingBuffer);
this.httpServerRequest.exceptionHandler(this::handleRequestFailure);
this.httpServerRequest.endHandler(this::handleRequestEnd);
}
private void closeRequest() {
if (!this.httpServerRequest.isEnded()) {
this.httpServerRequest.end();
}
}
private void handleSubscriptionRequest(long l) {
if (l == Long.MAX_VALUE) {
this.subscriberRequest = l;
} else {
this.subscriberRequest += l;
// Overflow check
if (this.subscriberRequest < 0) {
this.subscriberRequest = Long.MAX_VALUE;
}
}
tryProgress();
}
private void handleIncomingBuffer(Buffer buffer) {
// Fast path
if (this.buffers.isEmpty() && this.subscriberRequest > 0) {
this.inputMessagesSubscriber.onNext(Slice.wrap(buffer.getByteBuf().nioBuffer()));
this.subscriberRequest--;
return;
}
this.buffers.add(buffer.getByteBuf().nioBuffer());
tryProgress();
}
private void handleRequestFailure(Throwable e) {
LOG.trace("Request error", e);
this.inputMessagesSubscriber.onError(e);
}
private void handleRequestEnd(Void v) {
LOG.trace("Request end");
this.inputMessagesSubscriber.onComplete();
this.inputMessagesSubscriber = null;
}
private void tryProgress() {
while (this.subscriberRequest > 0) {
ByteBuffer input = this.buffers.poll();
if (input == null) {
return;
}
this.subscriberRequest--;
inputMessagesSubscriber.onNext(Slice.wrap(input));
}
}
}