Skip to content

Commit

Permalink
fix batch 1
Browse files Browse the repository at this point in the history
  • Loading branch information
wzy1935 committed Jul 25, 2024
1 parent 0f14fde commit 14f0d8b
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 29 deletions.
6 changes: 3 additions & 3 deletions src/main/java/examples/HttpProxyExamples.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void origin(Vertx vertx) {
public void proxy(Vertx vertx) {
HttpClient proxyClient = vertx.createHttpClient();

HttpProxy proxy = HttpProxy.reverseProxy(proxyClient);
HttpProxy proxy = HttpProxy.reverseProxy(proxyClient, vertx);
proxy.origin(7070, "origin");

HttpServer proxyServer = vertx.createHttpServer();
Expand Down Expand Up @@ -165,7 +165,7 @@ private JsonObject removeSomeFields(JsonObject o) {
;

public void more(Vertx vertx, HttpClient proxyClient) {
HttpProxy proxy = HttpProxy.reverseProxy(proxyClient).originSelector(
HttpProxy proxy = HttpProxy.reverseProxy(proxyClient, vertx).originSelector(
address -> Future.succeededFuture(SocketAddress.inetSocketAddress(7070, "origin"))
);
}
Expand Down Expand Up @@ -204,6 +204,6 @@ public Future<ProxyResponse> handleProxyRequest(ProxyContext context) {
}

public void cacheConfig(Vertx vertx, HttpClient proxyClient) {
HttpProxy proxy = HttpProxy.reverseProxy(new ProxyOptions().setCacheOptions(new CacheOptions()), proxyClient);
HttpProxy proxy = HttpProxy.reverseProxy(new ProxyOptions().setCacheOptions(new CacheOptions()), proxyClient, vertx);
}
}
9 changes: 5 additions & 4 deletions src/main/java/io/vertx/httpproxy/HttpProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.vertx.codegen.annotations.VertxGen;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.http.HttpServerRequest;
Expand All @@ -39,8 +40,8 @@ public interface HttpProxy extends Handler<HttpServerRequest> {
* @param client the {@code HttpClient} that forwards <i><b>outbound</b></i> requests to the <i><b>origin</b></i>.
* @return a reference to this, so the API can be used fluently.
*/
static HttpProxy reverseProxy(HttpClient client) {
return new ReverseProxy(new ProxyOptions(), client);
static HttpProxy reverseProxy(HttpClient client, Vertx vertx) {
return new ReverseProxy(new ProxyOptions(), client, vertx);
}

/**
Expand All @@ -49,8 +50,8 @@ static HttpProxy reverseProxy(HttpClient client) {
* @param client the {@code HttpClient} that forwards <i><b>outbound</b></i> requests to the <i><b>origin</b></i>.
* @return a reference to this, so the API can be used fluently.
*/
static HttpProxy reverseProxy(ProxyOptions options, HttpClient client) {
return new ReverseProxy(options, client);
static HttpProxy reverseProxy(ProxyOptions options, HttpClient client, Vertx vertx) {
return new ReverseProxy(options, client, vertx);
}

/**
Expand Down
42 changes: 41 additions & 1 deletion src/main/java/io/vertx/httpproxy/cache/CacheOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

import io.vertx.codegen.annotations.DataObject;
import io.vertx.codegen.json.annotations.JsonGen;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpClientAgent;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.internal.CloseFuture;
import io.vertx.core.internal.VertxInternal;
import io.vertx.core.json.JsonObject;
import io.vertx.httpproxy.impl.CacheImpl;
import io.vertx.httpproxy.spi.cache.Cache;

import java.util.Objects;

/**
* Cache options.
*/
Expand All @@ -14,8 +21,12 @@
public class CacheOptions {

public static final int DEFAULT_MAX_SIZE = 1000;
public static final String DEFAULT_NAME = "__vertx.DEFAULT";
public static final boolean DEFAULT_SHARED = false;

private int maxSize = DEFAULT_MAX_SIZE;
private String name = DEFAULT_NAME;
private boolean shared = DEFAULT_SHARED;

public CacheOptions() {
}
Expand Down Expand Up @@ -45,7 +56,36 @@ public CacheOptions setMaxSize(int maxSize) {
return this;
}

public Cache newCache() {
public String getName() {
return this.name;
}

public CacheOptions setName(String name) {
Objects.requireNonNull(name, "Client name cannot be null");
this.name = name;
return this;
}

public boolean getShared() {
return shared;
}

public CacheOptions setShared(boolean shared) {
this.shared = shared;
return this;
}

public Cache newCache(Vertx vertx) {
if (shared) {
CloseFuture closeFuture = new CloseFuture();
return ((VertxInternal) vertx).createSharedResource("__vertx.shared.proxyCache", name, closeFuture, (cf_) -> {
Cache cache = new CacheImpl(this);
cf_.add(completion -> {
cache.close().onComplete(completion);
});
return cache;
});
}
return new CacheImpl(this);
}

Expand Down
24 changes: 13 additions & 11 deletions src/main/java/io/vertx/httpproxy/impl/CacheImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.vertx.httpproxy.impl;

import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.httpproxy.cache.CacheOptions;
import io.vertx.httpproxy.spi.cache.Cache;
import io.vertx.httpproxy.spi.cache.Resource;
Expand All @@ -16,25 +17,22 @@
public class CacheImpl implements Cache {

private final int maxSize;
private final Map<String, Resource> data;
private final LinkedList<String> records;
private final LinkedHashMap<String, Resource> data;

public CacheImpl(CacheOptions options) {
this.maxSize = options.getMaxSize();
this.data = new HashMap<>();
this.records = new LinkedList<>();
this.data = new LinkedHashMap<>() {
@Override
protected boolean removeEldestEntry(Map.Entry<String, Resource> eldest) {
return size() > maxSize;
}
};
}


@Override
public Future<Void> put(String key, Resource value) {
while (records.size() >= maxSize) {
String toRemove = records.removeLast();
data.remove(toRemove);
}

data.put(key, value);
records.addFirst(key);
return Future.succeededFuture();
}

Expand All @@ -45,8 +43,12 @@ public Future<Resource> get(String key) {

@Override
public Future<Void> remove(String key) {
records.remove(key);
data.remove(key);
return Future.succeededFuture();
}

@Override
public Future<Void> close() {
return Future.succeededFuture();
}
}
5 changes: 3 additions & 2 deletions src/main/java/io/vertx/httpproxy/impl/ReverseProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package io.vertx.httpproxy.impl;

import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.http.*;
import io.vertx.core.internal.logging.Logger;
import io.vertx.core.internal.logging.LoggerFactory;
Expand All @@ -30,10 +31,10 @@ public class ReverseProxy implements HttpProxy {
private BiFunction<HttpServerRequest, HttpClient, Future<HttpClientRequest>> selector = (req, client) -> Future.failedFuture("No origin available");
private final List<ProxyInterceptor> interceptors = new ArrayList<>();

public ReverseProxy(ProxyOptions options, HttpClient client) {
public ReverseProxy(ProxyOptions options, HttpClient client, Vertx vertx) {
CacheOptions cacheOptions = options.getCacheOptions();
if (cacheOptions != null) {
Cache cache = cacheOptions.newCache();
Cache cache = cacheOptions.newCache(vertx);
addInterceptor(new CachingFilter(cache));
}
this.client = client;
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/io/vertx/httpproxy/spi/cache/Cache.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.vertx.httpproxy.spi.cache;

import io.vertx.core.Future;
import io.vertx.core.dns.SrvRecord;


/**
Expand All @@ -9,7 +10,9 @@
public interface Cache {

/**
* Being called when the cache attempts to add a new cache item.
* Being called when the proxy attempts to add a new cache item.
* The cache can only store up to maxSize of the latest items based
* on CacheOptions.
*
* @param key the URI of the resource
* @param value the cached response
Expand All @@ -18,20 +21,27 @@ public interface Cache {
Future<Void> put(String key, Resource value);

/**
* Being called when the cache attempts to fetch a cache item.
* Being called when the proxy attempts to fetch a cache item.
*
* @param key the URI of the resource
* @return the cached response, null if not exist
*/
Future<Resource> get(String key);

/**
* Being called when the cache attempts to delete a cache item,
* Being called when the proxy attempts to delete a cache item,
* typically caused by invalidating an existing item. Do nothing
* if not exist.
*
* @param key the URI of the resource
* @return a succeed void future
*/
Future<Void> remove(String key);

/**
* Being called when need to close the cache.
*
* @return a succeed void future
*/
Future<Void> close();
}
6 changes: 4 additions & 2 deletions src/main/java/io/vertx/httpproxy/spi/cache/Resource.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
import io.vertx.httpproxy.ProxyResponse;
import io.vertx.httpproxy.impl.ParseUtils;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.Instant;

/**
* The cached object.
*/
public class Resource implements ClusterSerializable {

private static final String UTF_8 = "utf-8";
private static final Charset UTF_8 = StandardCharsets.UTF_8;

private String absoluteUri;
private int statusCode;
Expand Down Expand Up @@ -148,7 +150,7 @@ private static Buffer readBuffer(Buffer buffer, Cursor cursor) {
}

private static void appendString(Buffer buffer, String string) {
appendBuffer(buffer, string == null ? null : Buffer.buffer(string, UTF_8));
appendBuffer(buffer, string == null ? null : Buffer.buffer(string.getBytes(UTF_8)));
}

private static String readString(Buffer buffer, Cursor cursor) {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/io/vertx/tests/ProxyRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ public void testRequestFilter(TestContext ctx) {
Async async = ctx.async();
backendClient = vertx.createHttpClient(new HttpClientOptions(clientOptions));
httpClient = vertx.createHttpClient();
HttpProxy proxy = HttpProxy.reverseProxy(backendClient);
HttpProxy proxy = HttpProxy.reverseProxy(backendClient, vertx);
proxy.origin(backend);
proxy.addInterceptor(new ProxyInterceptor() {
@Override
Expand Down Expand Up @@ -421,7 +421,7 @@ public void testResponseFilter(TestContext ctx) {
});
});
backendClient = vertx.createHttpClient(new HttpClientOptions(clientOptions));
HttpProxy proxy = HttpProxy.reverseProxy(backendClient);
HttpProxy proxy = HttpProxy.reverseProxy(backendClient, vertx);
proxy.origin(backend);
proxy.addInterceptor(new ProxyInterceptor() {
@Override
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/io/vertx/tests/TestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected Closeable startProxy(Consumer<HttpProxy> config) {
public void start(Promise<Void> startFuture) {
proxyClient = vertx.createHttpClient(new HttpClientOptions(clientOptions));
proxyServer = vertx.createHttpServer(new HttpServerOptions(serverOptions));
proxy = HttpProxy.reverseProxy(proxyOptions, proxyClient);
proxy = HttpProxy.reverseProxy(proxyOptions, proxyClient, vertx);
config.accept(proxy);
proxyServer.requestHandler(proxy);
proxyServer.listen().onComplete(ar -> startFuture.handle(ar.mapEmpty()));
Expand Down
Loading

0 comments on commit 14f0d8b

Please sign in to comment.