Skip to content

Commit 14f0d8b

Browse files
committed
fix batch 1
1 parent 0f14fde commit 14f0d8b

File tree

11 files changed

+212
-29
lines changed

11 files changed

+212
-29
lines changed

src/main/java/examples/HttpProxyExamples.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void origin(Vertx vertx) {
3939
public void proxy(Vertx vertx) {
4040
HttpClient proxyClient = vertx.createHttpClient();
4141

42-
HttpProxy proxy = HttpProxy.reverseProxy(proxyClient);
42+
HttpProxy proxy = HttpProxy.reverseProxy(proxyClient, vertx);
4343
proxy.origin(7070, "origin");
4444

4545
HttpServer proxyServer = vertx.createHttpServer();
@@ -165,7 +165,7 @@ private JsonObject removeSomeFields(JsonObject o) {
165165
;
166166

167167
public void more(Vertx vertx, HttpClient proxyClient) {
168-
HttpProxy proxy = HttpProxy.reverseProxy(proxyClient).originSelector(
168+
HttpProxy proxy = HttpProxy.reverseProxy(proxyClient, vertx).originSelector(
169169
address -> Future.succeededFuture(SocketAddress.inetSocketAddress(7070, "origin"))
170170
);
171171
}
@@ -204,6 +204,6 @@ public Future<ProxyResponse> handleProxyRequest(ProxyContext context) {
204204
}
205205

206206
public void cacheConfig(Vertx vertx, HttpClient proxyClient) {
207-
HttpProxy proxy = HttpProxy.reverseProxy(new ProxyOptions().setCacheOptions(new CacheOptions()), proxyClient);
207+
HttpProxy proxy = HttpProxy.reverseProxy(new ProxyOptions().setCacheOptions(new CacheOptions()), proxyClient, vertx);
208208
}
209209
}

src/main/java/io/vertx/httpproxy/HttpProxy.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import io.vertx.codegen.annotations.VertxGen;
1616
import io.vertx.core.Future;
1717
import io.vertx.core.Handler;
18+
import io.vertx.core.Vertx;
1819
import io.vertx.core.http.HttpClient;
1920
import io.vertx.core.http.HttpClientRequest;
2021
import io.vertx.core.http.HttpServerRequest;
@@ -39,8 +40,8 @@ public interface HttpProxy extends Handler<HttpServerRequest> {
3940
* @param client the {@code HttpClient} that forwards <i><b>outbound</b></i> requests to the <i><b>origin</b></i>.
4041
* @return a reference to this, so the API can be used fluently.
4142
*/
42-
static HttpProxy reverseProxy(HttpClient client) {
43-
return new ReverseProxy(new ProxyOptions(), client);
43+
static HttpProxy reverseProxy(HttpClient client, Vertx vertx) {
44+
return new ReverseProxy(new ProxyOptions(), client, vertx);
4445
}
4546

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

5657
/**

src/main/java/io/vertx/httpproxy/cache/CacheOptions.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@
22

33
import io.vertx.codegen.annotations.DataObject;
44
import io.vertx.codegen.json.annotations.JsonGen;
5+
import io.vertx.core.Vertx;
6+
import io.vertx.core.http.HttpClientAgent;
7+
import io.vertx.core.http.HttpClientOptions;
8+
import io.vertx.core.internal.CloseFuture;
9+
import io.vertx.core.internal.VertxInternal;
510
import io.vertx.core.json.JsonObject;
611
import io.vertx.httpproxy.impl.CacheImpl;
712
import io.vertx.httpproxy.spi.cache.Cache;
813

14+
import java.util.Objects;
15+
916
/**
1017
* Cache options.
1118
*/
@@ -14,8 +21,12 @@
1421
public class CacheOptions {
1522

1623
public static final int DEFAULT_MAX_SIZE = 1000;
24+
public static final String DEFAULT_NAME = "__vertx.DEFAULT";
25+
public static final boolean DEFAULT_SHARED = false;
1726

1827
private int maxSize = DEFAULT_MAX_SIZE;
28+
private String name = DEFAULT_NAME;
29+
private boolean shared = DEFAULT_SHARED;
1930

2031
public CacheOptions() {
2132
}
@@ -45,7 +56,36 @@ public CacheOptions setMaxSize(int maxSize) {
4556
return this;
4657
}
4758

48-
public Cache newCache() {
59+
public String getName() {
60+
return this.name;
61+
}
62+
63+
public CacheOptions setName(String name) {
64+
Objects.requireNonNull(name, "Client name cannot be null");
65+
this.name = name;
66+
return this;
67+
}
68+
69+
public boolean getShared() {
70+
return shared;
71+
}
72+
73+
public CacheOptions setShared(boolean shared) {
74+
this.shared = shared;
75+
return this;
76+
}
77+
78+
public Cache newCache(Vertx vertx) {
79+
if (shared) {
80+
CloseFuture closeFuture = new CloseFuture();
81+
return ((VertxInternal) vertx).createSharedResource("__vertx.shared.proxyCache", name, closeFuture, (cf_) -> {
82+
Cache cache = new CacheImpl(this);
83+
cf_.add(completion -> {
84+
cache.close().onComplete(completion);
85+
});
86+
return cache;
87+
});
88+
}
4989
return new CacheImpl(this);
5090
}
5191

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.vertx.httpproxy.impl;
22

33
import io.vertx.core.Future;
4+
import io.vertx.core.Vertx;
45
import io.vertx.httpproxy.cache.CacheOptions;
56
import io.vertx.httpproxy.spi.cache.Cache;
67
import io.vertx.httpproxy.spi.cache.Resource;
@@ -16,25 +17,22 @@
1617
public class CacheImpl implements Cache {
1718

1819
private final int maxSize;
19-
private final Map<String, Resource> data;
20-
private final LinkedList<String> records;
20+
private final LinkedHashMap<String, Resource> data;
2121

2222
public CacheImpl(CacheOptions options) {
2323
this.maxSize = options.getMaxSize();
24-
this.data = new HashMap<>();
25-
this.records = new LinkedList<>();
24+
this.data = new LinkedHashMap<>() {
25+
@Override
26+
protected boolean removeEldestEntry(Map.Entry<String, Resource> eldest) {
27+
return size() > maxSize;
28+
}
29+
};
2630
}
2731

2832

2933
@Override
3034
public Future<Void> put(String key, Resource value) {
31-
while (records.size() >= maxSize) {
32-
String toRemove = records.removeLast();
33-
data.remove(toRemove);
34-
}
35-
3635
data.put(key, value);
37-
records.addFirst(key);
3836
return Future.succeededFuture();
3937
}
4038

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

4644
@Override
4745
public Future<Void> remove(String key) {
48-
records.remove(key);
4946
data.remove(key);
5047
return Future.succeededFuture();
5148
}
49+
50+
@Override
51+
public Future<Void> close() {
52+
return Future.succeededFuture();
53+
}
5254
}

src/main/java/io/vertx/httpproxy/impl/ReverseProxy.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
package io.vertx.httpproxy.impl;
1212

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

33-
public ReverseProxy(ProxyOptions options, HttpClient client) {
34+
public ReverseProxy(ProxyOptions options, HttpClient client, Vertx vertx) {
3435
CacheOptions cacheOptions = options.getCacheOptions();
3536
if (cacheOptions != null) {
36-
Cache cache = cacheOptions.newCache();
37+
Cache cache = cacheOptions.newCache(vertx);
3738
addInterceptor(new CachingFilter(cache));
3839
}
3940
this.client = client;
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.vertx.httpproxy.spi.cache;
22

33
import io.vertx.core.Future;
4+
import io.vertx.core.dns.SrvRecord;
45

56

67
/**
@@ -9,7 +10,9 @@
910
public interface Cache {
1011

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

2023
/**
21-
* Being called when the cache attempts to fetch a cache item.
24+
* Being called when the proxy attempts to fetch a cache item.
2225
*
2326
* @param key the URI of the resource
2427
* @return the cached response, null if not exist
2528
*/
2629
Future<Resource> get(String key);
2730

2831
/**
29-
* Being called when the cache attempts to delete a cache item,
32+
* Being called when the proxy attempts to delete a cache item,
3033
* typically caused by invalidating an existing item. Do nothing
3134
* if not exist.
3235
*
3336
* @param key the URI of the resource
3437
* @return a succeed void future
3538
*/
3639
Future<Void> remove(String key);
40+
41+
/**
42+
* Being called when need to close the cache.
43+
*
44+
* @return a succeed void future
45+
*/
46+
Future<Void> close();
3747
}

src/main/java/io/vertx/httpproxy/spi/cache/Resource.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@
1818
import io.vertx.httpproxy.ProxyResponse;
1919
import io.vertx.httpproxy.impl.ParseUtils;
2020

21+
import java.nio.charset.Charset;
22+
import java.nio.charset.StandardCharsets;
2123
import java.time.Instant;
2224

2325
/**
2426
* The cached object.
2527
*/
2628
public class Resource implements ClusterSerializable {
2729

28-
private static final String UTF_8 = "utf-8";
30+
private static final Charset UTF_8 = StandardCharsets.UTF_8;
2931

3032
private String absoluteUri;
3133
private int statusCode;
@@ -148,7 +150,7 @@ private static Buffer readBuffer(Buffer buffer, Cursor cursor) {
148150
}
149151

150152
private static void appendString(Buffer buffer, String string) {
151-
appendBuffer(buffer, string == null ? null : Buffer.buffer(string, UTF_8));
153+
appendBuffer(buffer, string == null ? null : Buffer.buffer(string.getBytes(UTF_8)));
152154
}
153155

154156
private static String readString(Buffer buffer, Cursor cursor) {

src/test/java/io/vertx/tests/ProxyRequestTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ public void testRequestFilter(TestContext ctx) {
369369
Async async = ctx.async();
370370
backendClient = vertx.createHttpClient(new HttpClientOptions(clientOptions));
371371
httpClient = vertx.createHttpClient();
372-
HttpProxy proxy = HttpProxy.reverseProxy(backendClient);
372+
HttpProxy proxy = HttpProxy.reverseProxy(backendClient, vertx);
373373
proxy.origin(backend);
374374
proxy.addInterceptor(new ProxyInterceptor() {
375375
@Override
@@ -421,7 +421,7 @@ public void testResponseFilter(TestContext ctx) {
421421
});
422422
});
423423
backendClient = vertx.createHttpClient(new HttpClientOptions(clientOptions));
424-
HttpProxy proxy = HttpProxy.reverseProxy(backendClient);
424+
HttpProxy proxy = HttpProxy.reverseProxy(backendClient, vertx);
425425
proxy.origin(backend);
426426
proxy.addInterceptor(new ProxyInterceptor() {
427427
@Override

src/test/java/io/vertx/tests/TestBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ protected Closeable startProxy(Consumer<HttpProxy> config) {
8181
public void start(Promise<Void> startFuture) {
8282
proxyClient = vertx.createHttpClient(new HttpClientOptions(clientOptions));
8383
proxyServer = vertx.createHttpServer(new HttpServerOptions(serverOptions));
84-
proxy = HttpProxy.reverseProxy(proxyOptions, proxyClient);
84+
proxy = HttpProxy.reverseProxy(proxyOptions, proxyClient, vertx);
8585
config.accept(proxy);
8686
proxyServer.requestHandler(proxy);
8787
proxyServer.listen().onComplete(ar -> startFuture.handle(ar.mapEmpty()));

0 commit comments

Comments
 (0)