Skip to content
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

[Refactor] Handling of request size metrics uniformly #3

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 17 additions & 21 deletions RFS/src/main/java/com/rfs/common/RestClient.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.rfs.common;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.function.BiConsumer;

import com.rfs.netty.ReadMeteringHandler;
import com.rfs.netty.WriteMeteringHandler;
import com.rfs.tracing.IRfsContexts;
import io.netty.buffer.Unpooled;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import reactor.core.publisher.Mono;
import reactor.netty.http.client.HttpClient;
import reactor.netty.http.client.HttpClientRequest;
import reactor.netty.ByteBufMono;
import reactor.netty.Connection;

public class RestClient {
public static class Response {
Expand Down Expand Up @@ -47,17 +46,13 @@ public RestClient(ConnectionDetails connectionDetails) {

public Mono<Response> getAsync(String path, IRfsContexts.IRequestContext context) {
return client
.doOnRequest((r, conn) -> {
conn.channel().pipeline().addFirst(new WriteMeteringHandler(context::addBytesSent));
conn.channel().pipeline().addFirst(new ReadMeteringHandler(context::addBytesRead));
})
.doOnRequest(sizeMetricsHandler(context))
.get()
.uri("/" + path)
.responseSingle((response, bytes) -> bytes.asString()
.map(body -> new Response(response.status().code(), body, response.status().reasonPhrase())))
.map(body -> new Response(response.status().code(), body, response.status().reasonPhrase())))
.doOnError(t->context.addTraceException(t, true))
.doFinally(r->context.close())
;
.doFinally(r->context.close());
}

public Response get(String path, IRfsContexts.IRequestContext context) {
Expand All @@ -66,35 +61,36 @@ public Response get(String path, IRfsContexts.IRequestContext context) {

public Mono<Response> postAsync(String path, String body, IRfsContexts.IRequestContext context) {
return client
.doOnRequest((r, conn) -> {
conn.channel().pipeline().addFirst(new WriteMeteringHandler(context::addBytesSent));
conn.channel().pipeline().addFirst(new ReadMeteringHandler(context::addBytesRead));
})
.doOnRequest(sizeMetricsHandler(context))
.post()
.uri("/" + path)
.send(ByteBufMono.fromString(Mono.just(body)))
.responseSingle((response, bytes) -> bytes.asString()
.map(b -> new Response(response.status().code(), b, response.status().reasonPhrase())))
.map(b -> new Response(response.status().code(), b, response.status().reasonPhrase())))
.doOnError(t->context.addTraceException(t, true))
.doFinally(r->context.close());
}

public Mono<Response> putAsync(String path, String body, IRfsContexts.IRequestContext context) {
return client
.doOnRequest((r, conn) -> {
conn.channel().pipeline().addFirst(new WriteMeteringHandler(context::addBytesSent));
conn.channel().pipeline().addFirst(new ReadMeteringHandler(context::addBytesRead));
})
.doOnRequest(sizeMetricsHandler(context))
.put()
.uri("/" + path)
.send(ByteBufMono.fromString(Mono.just(body)))
.responseSingle((response, bytes) -> bytes.asString()
.map(b -> new Response(response.status().code(), b, response.status().reasonPhrase())))
.map(b -> new Response(response.status().code(), b, response.status().reasonPhrase())))
.doOnError(t->context.addTraceException(t, true))
.doFinally(r->context.close());
}

public Response put(String path, String body, IRfsContexts.IRequestContext context) {
return putAsync(path, body, context).block();
}

private BiConsumer<HttpClientRequest, Connection> sizeMetricsHandler(final IRfsContexts.IRequestContext context) {
return (r, conn) -> {
conn.channel().pipeline().addFirst(new WriteMeteringHandler(context::addBytesSent));
conn.channel().pipeline().addFirst(new ReadMeteringHandler(context::addBytesRead));
};
}
}