Skip to content
Permalink
Browse files
[3.0 Triple] Optimize Connection (#9954)
* opt code of Connection

* add multi connect test case

* opt code of Connection

Co-authored-by: 呈铭 <beck.wcm@antgroup.com>
  • Loading branch information
wangchengming666 and 呈铭 committed May 16, 2022
1 parent 7e8c7aa commit 96cff83d0e36fbcd7564b4c15b0c2453af50124a
Showing 2 changed files with 37 additions and 5 deletions.
@@ -175,7 +175,7 @@ public void onConnected(Channel channel) {
}
this.channel.set(channel);
// This indicates that the connection is available.
if (this.connectingPromise != null ) {
if (this.connectingPromise != null) {
this.connectingPromise.trySuccess(CONNECTED_OBJECT);
}
channel.attr(CONNECTION).set(this);
@@ -192,10 +192,8 @@ public boolean isAvailable() {
if (channel != null && channel.isActive()) {
return true;
}
synchronized (this) {
if (init.compareAndSet(false, true)) {
connect();
}
if (init.compareAndSet(false, true)) {
connect();
}

this.createConnectingPromise();
@@ -23,6 +23,8 @@
import org.junit.jupiter.api.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class ConnectionTest {

@@ -88,4 +90,36 @@ public void connectSyncTest() throws Throwable {


}

@Test
public void testMultiConnect() throws Throwable {
int port = NetUtils.getAvailablePort();
URL url = URL.valueOf("empty://127.0.0.1:" + port + "?foo=bar");
PortUnificationServer server = null;
try {
server = new PortUnificationServer(url);
server.close();

Connection connection = new Connection(url);
ExecutorService service = Executors.newFixedThreadPool(10);
final CountDownLatch latch = new CountDownLatch(10);
for (int i = 0; i < 10; i++) {
Runnable runnable = () -> {
try {
Assertions.assertTrue(connection.isAvailable());
latch.countDown();
} catch (Exception e) {
// ignore
}
};
service.execute(runnable);
}
} finally {
try {
server.close();
} catch (Throwable e) {
// ignored
}
}
}
}

0 comments on commit 96cff83

Please sign in to comment.