Skip to content

Commit

Permalink
add remote address even socket timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
sxci committed Nov 13, 2020
1 parent 3be435d commit ecc295f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/main/java/com/qiniu/http/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.UnknownHostException;
import java.util.List;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -69,17 +71,34 @@ public Client(final Dns dns, final boolean hostFirst, final ProxyConfiguration p

builder.dispatcher(dispatcher);
builder.connectionPool(connectionPool);
builder.addNetworkInterceptor(new Interceptor() {
builder.eventListener(new EventListener() {
@Override
public void connectStart(Call call, InetSocketAddress inetSocketAddress, Proxy proxy) {
Request req = call.request();
if (req != null) {
IpTag tag = (IpTag) req.tag();
tag.ip = inetSocketAddress + "";
}
}
});
builder.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request();

okhttp3.Response response = chain.proceed(request);
IpTag tag = (IpTag) request.tag();
okhttp3.Response response = null;
IOException ex = null;
try {
tag.ip = chain.connection().socket().getRemoteSocketAddress().toString();
} catch (Exception e) {
tag.ip = "";
response = chain.proceed(request);
} catch (IOException e) {
if (e instanceof java.net.ConnectException) {
ex = e;
} else {
IpTag tag = (IpTag) request.tag();
ex = new IOException(e + " on " + tag.ip, e);
}
}
if (ex != null) {
throw ex;
}
return response;
}
Expand Down Expand Up @@ -445,6 +464,6 @@ public void accept(String key, Object value) {
}

private static class IpTag {
public String ip = null;
public String ip = "";
}
}
55 changes: 55 additions & 0 deletions src/test/java/test/com/qiniu/HttpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
import com.qiniu.http.Client;
import com.qiniu.http.ProxyConfiguration;
import com.qiniu.http.Response;
import okhttp3.OkHttpClient;
import org.junit.Assert;
import org.junit.Test;

import java.lang.reflect.Field;
import java.util.concurrent.TimeUnit;

public class HttpTest {
private Client httpManager = new Client();
Expand Down Expand Up @@ -93,4 +98,54 @@ public void testProxy() {
Assert.assertEquals(e.response.statusCode, 400);
}
}

@Test
public void testTimeout() throws NoSuchFieldException, IllegalAccessException {
try {
Response res = new Client().get("https://www.qiniu.com/?v=12345");
String r = res.toString();
System.out.println(r);
Assert.assertTrue("https, must have port 443", r.indexOf(":443") > 0);
} catch (QiniuException e) {
e.printStackTrace();
Assert.fail("should be ok");
}

Client client = new Client();
Field field = client.getClass().getDeclaredField("httpClient");
field.setAccessible(true);
OkHttpClient okHttpClient = (OkHttpClient) field.get(client);
okHttpClient = okHttpClient.newBuilder().connectTimeout(3, TimeUnit.MILLISECONDS).build();
field.set(client, okHttpClient);

try {
client.get("http://rs.qbox.me/?v=12");
Assert.fail("should be timeout");
} catch (QiniuException e) {
e.printStackTrace();
Assert.assertTrue("http, must have port 80", e.getMessage().indexOf(":80") > 10);
}
try {
client.get("https://rs.qbox.me/?v=we");
Assert.fail("should be timeout");
} catch (QiniuException e) {
e.printStackTrace();
Assert.assertTrue("https, must have port 443", e.getMessage().indexOf(":443") > 10);
}

try {
client.get("http://www.qiniu.com/?v=543");
Assert.fail("should be timeout");
} catch (QiniuException e) {
e.printStackTrace();
Assert.assertTrue("http, must have port 80", e.getMessage().indexOf(":80") > 10);
}
try {
client.get("https://www.qiniu.com/?v=kgd");
Assert.fail("should be timeout");
} catch (QiniuException e) {
e.printStackTrace();
Assert.assertTrue("https, must have port 443", e.getMessage().indexOf(":443") > 10);
}
}
}
1 change: 1 addition & 0 deletions src/test/java/test/com/qiniu/storage/BucketTest2.java
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,7 @@ public void testBatch() {
bucketManager.copy(bucket, key, bucket, moveFromKey);
bucketManager.copy(bucket, key, bucket, moveFromKey2);
} catch (QiniuException e) {
e.printStackTrace();
Assert.fail(e.response.toString());
}

Expand Down

0 comments on commit ecc295f

Please sign in to comment.