Skip to content

Commit 43713cc

Browse files
committed
Merge pull request #178 from longbai/enlarge_pool_size
enlarge_pool_size
2 parents 8bc453a + b2f5e5c commit 43713cc

File tree

13 files changed

+262
-32
lines changed

13 files changed

+262
-32
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
#Changelog
22

3+
## 7.0.4.3 (2015-10-08)
4+
5+
### 修正
6+
* 调大连接池,提高并发性能
7+
8+
### 增加
9+
* 增加data的异步上传
10+
311
## 7.0.4.2 (2015-09-22)
412

513
### 修正

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ dependencies {
1616
compile group:'com.google.code.gson', name:'gson', version:'2.3.1'
1717
testCompile group: 'junit', name: 'junit', version: '4.11'
1818
//okhttp jdk 6 version, only for jdk6 test
19+
testCompile fileTree(dir: 'libs', include: '*.jar')
1920
testRuntime fileTree(dir: 'libs', include: '*.jar')
2021
}
2122

src/main/java/com/qiniu/common/Config.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
public final class Config {
88

9-
public static final String VERSION = "7.0.4.1";
9+
public static final String VERSION = "7.0.4.3";
1010
/**
1111
* 断点上传时的分块大小(默认的分块大小, 不允许改变)
1212
*/
@@ -38,13 +38,13 @@ public final class Config {
3838
*/
3939
public static int PUT_THRESHOLD = BLOCK_SIZE;
4040
/**
41-
* 连接超时时间(默认10s)
41+
* 连接超时时间 单位秒(默认10s)
4242
*/
43-
public static int CONNECT_TIMEOUT = 10 * 1000;
43+
public static int CONNECT_TIMEOUT = 10;
4444
/**
45-
* 回复超时时间(默认30s)
45+
* 回复超时时间 单位秒(默认30s)
4646
*/
47-
public static int RESPONSE_TIMEOUT = 30 * 1000;
47+
public static int RESPONSE_TIMEOUT = 30;
4848
/**
4949
* 上传失败重试次数
5050
*/
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.qiniu.http;
2+
3+
/**
4+
* Created by bailong on 15/10/8.
5+
*/
6+
public interface AsyncCallback {
7+
void complete(Response r);
8+
}

src/main/java/com/qiniu/http/Client.java

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ public final class Client {
2222
private final OkHttpClient httpClient;
2323

2424
public Client() {
25+
Dispatcher dispatcher = new Dispatcher();
26+
dispatcher.setMaxRequests(64);
27+
dispatcher.setMaxRequestsPerHost(16);
28+
ConnectionPool connectionPool = new ConnectionPool(32, 5 * 60 * 1000);
2529
httpClient = new OkHttpClient();
30+
httpClient.setDispatcher(dispatcher);
31+
httpClient.setConnectionPool(connectionPool);
2632
httpClient.networkInterceptors().add(new Interceptor() {
2733
@Override
2834
public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
@@ -198,6 +204,95 @@ public void accept(String key, Object value) {
198204
return r;
199205
}
200206

207+
public void asyncSend(final Request.Builder requestBuilder, StringMap headers, final AsyncCallback cb) {
208+
if (headers != null) {
209+
headers.forEach(new StringMap.Consumer() {
210+
@Override
211+
public void accept(String key, Object value) {
212+
requestBuilder.header(key, value.toString());
213+
}
214+
});
215+
}
216+
217+
requestBuilder.header("User-Agent", userAgent());
218+
final long start = System.currentTimeMillis();
219+
IpTag tag = new IpTag();
220+
httpClient.newCall(requestBuilder.tag(tag).build()).enqueue(new Callback() {
221+
@Override
222+
public void onFailure(Request request, IOException e) {
223+
e.printStackTrace();
224+
long duration = (System.currentTimeMillis() - start) / 1000;
225+
cb.complete(Response.createError(null, "", duration, e.getMessage()));
226+
}
227+
228+
@Override
229+
public void onResponse(com.squareup.okhttp.Response response) throws IOException {
230+
long duration = (System.currentTimeMillis() - start) / 1000;
231+
cb.complete(Response.create(response, "", duration));
232+
}
233+
});
234+
}
235+
236+
public void asyncPost(String url, byte[] body, int offset, int size,
237+
StringMap headers, String contentType, AsyncCallback cb) {
238+
RequestBody rbody;
239+
if (body != null && body.length > 0) {
240+
MediaType t = MediaType.parse(contentType);
241+
rbody = create(t, body, offset, size);
242+
} else {
243+
rbody = RequestBody.create(null, new byte[0]);
244+
}
245+
246+
Request.Builder requestBuilder = new Request.Builder().url(url).post(rbody);
247+
asyncSend(requestBuilder, headers, cb);
248+
}
249+
250+
public void asyncMultipartPost(String url,
251+
StringMap fields,
252+
String name,
253+
String fileName,
254+
byte[] fileBody,
255+
String mimeType,
256+
StringMap headers,
257+
AsyncCallback cb) {
258+
RequestBody file = RequestBody.create(MediaType.parse(mimeType), fileBody);
259+
asyncMultipartPost(url, fields, name, fileName, file, headers, cb);
260+
}
261+
262+
public void asyncMultipartPost(String url,
263+
StringMap fields,
264+
String name,
265+
String fileName,
266+
File fileBody,
267+
String mimeType,
268+
StringMap headers,
269+
AsyncCallback cb) throws QiniuException {
270+
RequestBody file = RequestBody.create(MediaType.parse(mimeType), fileBody);
271+
asyncMultipartPost(url, fields, name, fileName, file, headers, cb);
272+
}
273+
274+
private void asyncMultipartPost(String url,
275+
StringMap fields,
276+
String name,
277+
String fileName,
278+
RequestBody file,
279+
StringMap headers,
280+
AsyncCallback cb) {
281+
final MultipartBuilder mb = new MultipartBuilder();
282+
mb.addFormDataPart(name, fileName, file);
283+
284+
fields.forEach(new StringMap.Consumer() {
285+
@Override
286+
public void accept(String key, Object value) {
287+
mb.addFormDataPart(key, value.toString());
288+
}
289+
});
290+
mb.type(MediaType.parse("multipart/form-data"));
291+
RequestBody body = mb.build();
292+
Request.Builder requestBuilder = new Request.Builder().url(url).post(body);
293+
asyncSend(requestBuilder, headers, cb);
294+
}
295+
201296
private static class IpTag {
202297
public String ip = null;
203298
}

src/main/java/com/qiniu/http/Response.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,33 @@ static Response create(com.squareup.okhttp.Response response, String address, do
8888
address, duration, error, body);
8989
}
9090

91+
static Response createError(com.squareup.okhttp.Response response, String address, double duration, String error) {
92+
if (response == null) {
93+
return new Response(null, -1, "", "", "", "", duration, error, null);
94+
}
95+
int code = response.code();
96+
String reqId = null;
97+
98+
byte[] body = null;
99+
if (ctype(response).equals(Client.JsonMime)) {
100+
reqId = response.header("X-Reqid");
101+
reqId = (reqId == null) ? null : reqId.trim();
102+
try {
103+
body = response.body().bytes();
104+
if (response.code() >= 400 && !StringUtils.isNullOrEmpty(reqId) && body != null) {
105+
ErrorBody errorBody = Json.decode(new String(body), ErrorBody.class);
106+
error = errorBody.error;
107+
}
108+
} catch (Exception e) {
109+
if (response.code() < 300) {
110+
error = e.getMessage();
111+
}
112+
}
113+
}
114+
return new Response(response, code, reqId, response.header("X-Log"), via(response),
115+
address, duration, error, body);
116+
}
117+
91118

92119
private static String via(com.squareup.okhttp.Response response) {
93120
String via;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.qiniu.storage;
2+
3+
/**
4+
* Created by bailong on 15/10/8.
5+
*/
6+
public class AsyncResumeUploader {
7+
}

src/main/java/com/qiniu/storage/FormUploader.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.qiniu.common.Config;
44
import com.qiniu.common.QiniuException;
5+
import com.qiniu.http.AsyncCallback;
56
import com.qiniu.http.Client;
67
import com.qiniu.http.Response;
78
import com.qiniu.util.Crc32;
@@ -53,6 +54,27 @@ Response upload() throws QiniuException {
5354
return client.multipartPost(Config.zone.upHost, params, "file", fileName, file, mime, new StringMap());
5455
}
5556

57+
void asyncUpload(final UpCompletionHandler handler) throws IOException {
58+
buildParams();
59+
if (data != null) {
60+
client.asyncMultipartPost(Config.zone.upHost, params, "file", fileName,
61+
data, mime, new StringMap(), new AsyncCallback() {
62+
@Override
63+
public void complete(Response r) {
64+
handler.complete(key, r);
65+
}
66+
});
67+
return;
68+
}
69+
client.asyncMultipartPost(Config.zone.upHost, params, "file", fileName,
70+
file, mime, new StringMap(), new AsyncCallback() {
71+
@Override
72+
public void complete(Response r) {
73+
handler.complete(key, r);
74+
}
75+
});
76+
}
77+
5678
private void buildParams() throws QiniuException {
5779
params.put("token", token);
5880
if (key == null) {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.qiniu.storage;
2+
3+
import com.qiniu.http.Response;
4+
5+
/**
6+
* Created by bailong on 15/10/8.
7+
*/
8+
public interface UpCompletionHandler {
9+
void complete(String key, Response r);
10+
}

src/main/java/com/qiniu/storage/UploadManager.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.qiniu.util.StringMap;
88

99
import java.io.File;
10+
import java.io.IOException;
1011

1112
/**
1213
* 七牛文件上传管理器
@@ -191,4 +192,15 @@ public Response put(File file, String key, String token, StringMap params,
191192
params, mime, recorder, recorderKey);
192193
return uploader.upload();
193194
}
195+
196+
197+
public void asyncPut(final byte[] data, final String key, final String token, StringMap params,
198+
String mime, boolean checkCrc, UpCompletionHandler handler) throws IOException {
199+
checkArgs(key, data, null, token);
200+
if (mime == null) {
201+
mime = Client.DefaultMime;
202+
}
203+
params = filterParam(params);
204+
new FormUploader(client, token, key, data, params, mime, checkCrc).asyncUpload(handler);
205+
}
194206
}

src/test/java/com/qiniu/processing/PfopTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.qiniu.http.Response;
66
import com.qiniu.util.Auth;
77
import com.qiniu.util.StringMap;
8-
98
import com.qiniu.util.UrlSafeBase64;
109
import org.junit.Test;
1110

src/test/java/com/qiniu/storage/FormUploadTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
import java.io.File;
1111
import java.io.IOException;
12+
import java.util.concurrent.CountDownLatch;
13+
import java.util.concurrent.TimeUnit;
1214

1315
import static org.junit.Assert.*;
1416

@@ -128,4 +130,41 @@ public void testFile() {
128130
}
129131
TempFile.remove(f);
130132
}
133+
134+
@Test
135+
public void testAsync() {
136+
final String expectKey = "你好?&=\r\n";
137+
StringMap params = new StringMap().put("x:foo", "foo_val");
138+
139+
String token = TestConfig.testAuth.uploadToken(TestConfig.bucket, expectKey);
140+
final CountDownLatch signal = new CountDownLatch(1);
141+
Response r = null;
142+
try {
143+
uploadManager.asyncPut("hello".getBytes(), expectKey, token, params,
144+
null, false, new UpCompletionHandler() {
145+
@Override
146+
public void complete(String key, Response r) {
147+
signal.countDown();
148+
StringMap map = null;
149+
try {
150+
map = r.jsonToMap();
151+
} catch (QiniuException e) {
152+
e.printStackTrace();
153+
fail();
154+
}
155+
assertEquals(200, r.statusCode);
156+
assert map != null;
157+
assertEquals("Fqr0xh3cxeii2r7eDztILNmuqUNN", map.get("hash"));
158+
assertEquals(expectKey, map.get("key"));
159+
}
160+
});
161+
} catch (IOException e) {
162+
fail();
163+
}
164+
try {
165+
signal.await(120, TimeUnit.SECONDS); // wait for callback
166+
} catch (InterruptedException e) {
167+
e.printStackTrace();
168+
}
169+
}
131170
}

0 commit comments

Comments
 (0)