Merge pull request #5 from Comcast/develop · hosuaby/feign@f07a2e4 · GitHub
Skip to content

Commit f07a2e4

Browse files
authored
Merge pull request OpenFeign#5 from Comcast/develop
Thanks to fix such big flaw! I will make it available in release 0.1.1
2 parents 91b8fcb + bf8fd61 commit f07a2e4

7 files changed

Lines changed: 553 additions & 3 deletions

File tree

‎pom.xml‎

Lines changed: 38 additions & 1 deletion

‎src/main/java/feign/vertx/VertxHttpClient.java‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
@SuppressWarnings("unused")
3131
public final class VertxHttpClient {
3232
private final Vertx vertx;
33+
34+
private HttpClient client = null;
3335

3436
public VertxHttpClient(final Vertx vertx) {
3537
this.vertx = checkNotNull(vertx, "Argument vertx must not be null");
@@ -47,11 +49,14 @@ public Future<Response> execute(final Request request, final HttpClientOptions o
4749
checkNotNull(request, "Argument request must be not null");
4850
checkNotNull(options, "Argument options must be not null");
4951

50-
final HttpClient client = vertx.createHttpClient(options);
52+
if (this.client == null) {
53+
this.client = vertx.createHttpClient(options);
54+
}
55+
5156
final HttpClientRequest httpClientRequest;
5257

5358
try {
54-
httpClientRequest = makeHttpClientRequest(request, client);
59+
httpClientRequest = makeHttpClientRequest(request, this.client);
5560
} catch (final MalformedURLException unexpectedException) {
5661
return Future.failedFuture(unexpectedException);
5762
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package feign.vertx;
2+
3+
import feign.VertxFeign;
4+
import feign.jackson.JacksonDecoder;
5+
import feign.jackson.JacksonEncoder;
6+
import feign.vertx.testcase.HelloServiceAPI;
7+
import io.vertx.core.Vertx;
8+
import io.vertx.core.http.*;
9+
import io.vertx.ext.unit.Async;
10+
import io.vertx.ext.unit.TestContext;
11+
import io.vertx.ext.unit.junit.VertxUnitRunner;
12+
import org.junit.After;
13+
import org.junit.Rule;
14+
import org.junit.Test;
15+
import org.junit.rules.ExpectedException;
16+
import org.junit.runner.RunWith;
17+
18+
import java.util.HashSet;
19+
20+
/**
21+
* Verify that Feign-Vertx does not leak sockets upon each request
22+
*/
23+
@RunWith(VertxUnitRunner.class)
24+
public class VertxHttp11ClientLeakFixTest {
25+
private Vertx vertx = Vertx.vertx();
26+
private HttpServer httpServer = null;
27+
28+
private HashSet<HttpConnection> connections = new HashSet<>();
29+
30+
@Rule
31+
public ExpectedException expectedException = ExpectedException.none();
32+
33+
@Test
34+
public void testHttpClientIssue(TestContext context) {
35+
36+
Async async = context.async();
37+
38+
HttpServerOptions serverOptions =
39+
new HttpServerOptions()
40+
.setLogActivity(true)
41+
.setPort(8090)
42+
.setSsl(false);
43+
44+
httpServer = this.vertx.createHttpServer(serverOptions);
45+
46+
// add a request handler and save the connections.
47+
httpServer.requestHandler(request -> {
48+
if (request.connection() != null)
49+
{
50+
this.connections.add(request.connection());
51+
}
52+
request.response().end("Hello world");
53+
});
54+
55+
httpServer.listen(res -> {
56+
if (res.failed()) {
57+
context.assertTrue(res.failed(), "Server failed to start");
58+
} else {
59+
// for HTTP 1.1 test, set up the pool size to 3. Then in the server side, there should be only 3 connections created per client.
60+
HttpClientOptions options = new HttpClientOptions();
61+
options.setMaxPoolSize(3);
62+
63+
HelloServiceAPI client = VertxFeign
64+
.builder()
65+
.vertx(this.vertx)
66+
.options(options)
67+
.encoder(new JacksonEncoder())
68+
.decoder(new JacksonDecoder())
69+
.target(HelloServiceAPI.class, "http://localhost:8090");
70+
71+
// run 10 times call to the server, then check if there are 10 connections created in server or not.
72+
for (int numToCall = 10; numToCall > 0; numToCall--) {
73+
74+
/* When */
75+
client.hello().setHandler(response -> {
76+
/* Then */
77+
if (response.succeeded()) {
78+
async.complete();
79+
} else {
80+
context.fail(response.cause());
81+
}
82+
});
83+
}
84+
}
85+
});
86+
}
87+
88+
@After
89+
public void after(TestContext context) {
90+
System.out.println("Connection created in server " + this.connections.size());
91+
92+
// before the fix of HttpClient issue in VertxHttpClient, the server will create 10 connections for 10 requests.
93+
// after the fix of the issue, server should only create 3 connection per same client.
94+
context.assertTrue(this.connections.size() == 3);
95+
96+
// Stop the server
97+
httpServer.close();
98+
}
99+
}
Lines changed: 219 additions & 0 deletions

0 commit comments

Comments
 (0)