Skip to content

Commit 499934a

Browse files
committed
add getConnectionPoolStats API.
1 parent cb54c93 commit 499934a

File tree

5 files changed

+53
-4
lines changed

5 files changed

+53
-4
lines changed

src/main/java/com/aliyun/oss/OSS.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ public interface OSS {
6868
*/
6969
public void shutdown();
7070

71+
/**
72+
* Get the statistics of the connection pool.
73+
*/
74+
public String getConnectionPoolStats();
75+
7176
/**
7277
* Creates {@link Bucket} instance. The bucket name specified must be
7378
* globally unique and follow the naming rules from

src/main/java/com/aliyun/oss/OSSClient.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,4 +1779,12 @@ public void shutdown() {
17791779
}
17801780
}
17811781

1782+
@Override
1783+
public String getConnectionPoolStats() {
1784+
try {
1785+
return serviceClient.getConnectionPoolStats();
1786+
} catch (Exception e) {
1787+
}
1788+
return "";
1789+
}
17821790
}

src/main/java/com/aliyun/oss/common/comm/DefaultServiceClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ public void shutdown() {
322322
}
323323

324324
@Override
325-
protected String getConnectionPoolStats() {
325+
public String getConnectionPoolStats() {
326326
if (connectionManager != null && connectionManager instanceof PoolingHttpClientConnectionManager) {
327327
PoolingHttpClientConnectionManager conn = (PoolingHttpClientConnectionManager)connectionManager;
328328
return conn.getTotalStats().toString();

src/main/java/com/aliyun/oss/common/comm/ServiceClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ private String formatSlowRequestLog(RequestMessage request, ResponseMessage resp
320320

321321
public abstract void shutdown();
322322

323-
protected String getConnectionPoolStats() {
323+
public String getConnectionPoolStats() {
324324
return "";
325325
};
326326

src/test/java/com/aliyun/oss/integrationtests/ClientBuilderTest.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ public void testClientBuilderWithInvalidEndpoint() {
242242
public void testClientBuilderLogConnectionPoolStats() {
243243
OSSClient client = null;
244244
ClientBuilderConfiguration config = new ClientBuilderConfiguration();
245+
config.setMaxConnections(10);
245246
Assert.assertEquals(config.isLogConnectionPoolStatsEnable(), false);
246247
config.setLogConnectionPoolStats(true);
247248
Assert.assertEquals(config.isLogConnectionPoolStatsEnable(), true);
@@ -251,8 +252,43 @@ public void testClientBuilderLogConnectionPoolStats() {
251252
new DefaultCredentialProvider(TestConfig.OSS_TEST_ACCESS_KEY_ID,
252253
TestConfig.OSS_TEST_ACCESS_KEY_SECRET),
253254
config);
254-
BucketInfo info = client.getBucketInfo(bucketName);
255-
Assert.assertEquals(info.getBucket().getName(), bucketName);
255+
256+
final OSSClient innerClient = client;
257+
258+
int threadCount = 20;
259+
Thread[] ts = new Thread[threadCount];
260+
for (int i = 0; i < threadCount; i++) {
261+
final int seqNum = i;
262+
Runnable r = new Runnable() {
263+
264+
@Override
265+
public void run() {
266+
try {
267+
BucketInfo info = innerClient.getBucketInfo(bucketName);
268+
Assert.assertEquals(info.getBucket().getName(), bucketName);
269+
} catch (Exception e) {
270+
System.out.println(e.getMessage());
271+
}
272+
}
273+
};
274+
275+
ts[i] = new Thread(r);
276+
}
277+
278+
for (int i = 0; i < threadCount; i++) {
279+
ts[i].start();
280+
}
281+
282+
for (int i = 0; i < threadCount; i++) {
283+
ts[i].join();
284+
}
285+
286+
String str = client.getConnectionPoolStats();
287+
Assert.assertTrue(str.indexOf("leased: 0") != -1);
288+
Assert.assertTrue(str.indexOf("pending: 0") != -1);
289+
Assert.assertTrue(str.indexOf("available: 10") != -1);
290+
Assert.assertTrue(str.indexOf("max: 10") != -1);
291+
256292
} catch (Exception e) {
257293
Assert.assertTrue(false);
258294
} finally {

0 commit comments

Comments
 (0)