-
Notifications
You must be signed in to change notification settings - Fork 4
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
add initial message delivery implementation #151
Changes from 3 commits
1b1c617
26ab682
9f56510
eb30569
005107b
b154c1a
9f552e2
b465499
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.flipkart.varadhi.consumer.delivery; | ||
|
||
import com.flipkart.varadhi.entities.Endpoint; | ||
|
||
public record DeliveryResponse(int statusCode, Endpoint.Protocol protocol, byte[] body) { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.flipkart.varadhi.consumer.delivery; | ||
|
||
import com.flipkart.varadhi.entities.Endpoint; | ||
import com.flipkart.varadhi.entities.Message; | ||
import com.flipkart.varadhi.exceptions.NotImplementedException; | ||
import com.google.common.collect.Multimap; | ||
import org.apache.commons.lang3.ArrayUtils; | ||
|
||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.time.Duration; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
public interface MessageDelivery { | ||
static MessageDelivery of(Endpoint endpoint) { | ||
return switch (endpoint.getProtocol()) { | ||
case HTTP1_1 -> new HttpMessageDelivery(endpoint); | ||
case HTTP2 -> throw new NotImplementedException("HTTP2 is not supported yet"); | ||
default -> throw new IllegalArgumentException("Unsupported protocol: " + endpoint.getProtocol()); | ||
}; | ||
} | ||
|
||
CompletableFuture<DeliveryResponse> deliver(Message message) | ||
throws Exception; | ||
|
||
class HttpMessageDelivery implements MessageDelivery { | ||
private final Endpoint.HttpEndpoint endpoint; | ||
private final HttpClient httpClient; | ||
|
||
public HttpMessageDelivery(Endpoint endpoint) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The constructor should take HttpEndpoint as param. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
this.endpoint = (Endpoint.HttpEndpoint) endpoint; | ||
this.httpClient = HttpClient.newBuilder() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We will likely not create a client per endpoint. There will be a single client used for all endpoints in an app. Assume a ConsumerContext { And expect the context to be passed to
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, can I model it as a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. supplier works |
||
.version(this.endpoint.isHttp2Supported() ? HttpClient.Version.HTTP_2 : HttpClient.Version.HTTP_1_1) | ||
.connectTimeout(Duration.ofMillis(this.endpoint.getConnectTimeoutMs())) | ||
.followRedirects(HttpClient.Redirect.NORMAL) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<DeliveryResponse> deliver(Message message) throws Exception { | ||
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder() | ||
.uri(endpoint.getUrl().toURI()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The fact that we are forced to do this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure, I can make this change |
||
.timeout(Duration.ofMillis(endpoint.getRequestTimeoutMs())) | ||
.header("Content-Type", endpoint.getContentType()) | ||
.method( | ||
endpoint.getMethod(), | ||
ArrayUtils.isEmpty(message.getPayload()) ? HttpRequest.BodyPublishers.noBody() : | ||
HttpRequest.BodyPublishers.ofByteArray(message.getPayload()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thing to note here: If that is what we ned in future, then we will want to look into using netty directly or an http client that does support offheap buffers. I wonder if vertx's http client supports it or not!! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
); | ||
|
||
// apply request headers from message | ||
Multimap<String, String> requestHeaders = message.getRequestHeaders(); | ||
if (requestHeaders != null) { | ||
requestHeaders.entries().forEach(entry -> requestBuilder.header(entry.getKey(), entry.getValue())); | ||
} | ||
|
||
HttpRequest request = requestBuilder.build(); | ||
|
||
return httpClient.sendAsync( | ||
request, HttpResponse.BodyHandlers.ofByteArray()) | ||
.thenApply(response -> new DeliveryResponse(response.statusCode(), endpoint.getProtocol(), | ||
response.body() | ||
)) | ||
.exceptionally(e -> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed it, had added for testing |
||
// log error | ||
return new DeliveryResponse(500, endpoint.getProtocol(), e.getMessage().getBytes()); | ||
}); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package com.flipkart.varadhi.entities; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.fasterxml.jackson.annotation.JsonSubTypes; | ||
import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
import lombok.Data; | ||
|
@@ -16,7 +17,8 @@ | |
}) | ||
public abstract sealed class Endpoint { | ||
|
||
abstract Protocol getProtocol(); | ||
@JsonIgnore | ||
public abstract Protocol getProtocol(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of of using raw type name, we can actually use protocol string only in the JsonSubTypes annotation. It will make for nice json. Eg: @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "protocol")
@JsonSubTypes({
@JsonSubTypes.Type(value = HttpEndpoint.class, name = "HTTP1_1"),
@JsonSubTypes.Type(value = HttpEndpoint.class, name = "HTTP2"), See if this works and would it result in a better serialized form? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes it works and this is the serialized json:
|
||
|
||
public enum Protocol { | ||
HTTP1_1, | ||
|
@@ -35,7 +37,7 @@ public static final class HttpEndpoint extends Endpoint { | |
private final boolean http2Supported; | ||
|
||
@Override | ||
Protocol getProtocol() { | ||
public Protocol getProtocol() { | ||
return http2Supported ? Protocol.HTTP2 : Protocol.HTTP1_1; | ||
} | ||
} | ||
|
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.
can we keep the Endpoint object itself here?
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.
not sure if it would be helpful since the caller will already have the endpoint object.
might as well remove the protocol field itself
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.
hmm. sure if that is the case