Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/main/java/org/tikv/common/util/ChannelFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class ChannelFactory implements AutoCloseable {

private final AtomicReference<SslContextBuilder> sslContextBuilder = new AtomicReference<>();

private final ScheduledExecutorService recycler = Executors.newSingleThreadScheduledExecutor();
private final ScheduledExecutorService recycler;

private final ReadWriteLock lock = new ReentrantReadWriteLock();

Expand Down Expand Up @@ -209,6 +209,7 @@ public ChannelFactory(
this.idleTimeout = idleTimeout;
this.certWatcher = null;
this.certContext = null;
this.recycler = null;
this.connRecycleTime = 0;
}

Expand All @@ -229,6 +230,7 @@ public ChannelFactory(
this.connRecycleTime = connRecycleTime;
this.certContext =
new OpenSslContext(trustCertCollectionFilePath, keyCertChainFilePath, keyFilePath);
this.recycler = Executors.newSingleThreadScheduledExecutor();

File trustCert = new File(trustCertCollectionFilePath);
File keyCert = new File(keyCertChainFilePath);
Expand Down Expand Up @@ -261,6 +263,7 @@ public ChannelFactory(
this.idleTimeout = idleTimeout;
this.connRecycleTime = connRecycleTime;
this.certContext = new JksContext(jksKeyPath, jksKeyPassword, jksTrustPath, jksTrustPassword);
this.recycler = Executors.newSingleThreadScheduledExecutor();

File jksKey = new File(jksKeyPath);
File jksTrust = new File(jksTrustPath);
Expand Down Expand Up @@ -360,11 +363,12 @@ public void close() {
}
connPool.clear();

if (certContext != null) {
if (recycler != null) {
recycler.shutdown();
if (certWatcher != null) {
certWatcher.close();
}
}

if (certWatcher != null) {
certWatcher.close();
}
}
}
26 changes: 15 additions & 11 deletions src/test/java/org/tikv/common/ChannelFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,28 @@ public void testCertWatcher() throws InterruptedException {
File a = new File(caPath);
File b = new File(clientCertPath);
File c = new File(clientKeyPath);
new CertWatcher(2, ImmutableList.of(a, b, c), () -> changed.set(true));
Thread.sleep(5000);
assertTrue(changed.get());
try (CertWatcher certWatcher =
new CertWatcher(2, ImmutableList.of(a, b, c), () -> changed.set(true))) {
Thread.sleep(5000);
assertTrue(changed.get());
}
}

@Test
public void testCertWatcherWithExceptionTask() throws InterruptedException {
AtomicInteger timesOfReloadTask = new AtomicInteger(0);
new CertWatcher(
1,
ImmutableList.of(new File(caPath), new File(clientCertPath), new File(clientKeyPath)),
() -> {
timesOfReloadTask.getAndIncrement();
touchCert();
throw new RuntimeException("Mock exception in reload task");
});
CertWatcher certWatcher =
new CertWatcher(
1,
ImmutableList.of(new File(caPath), new File(clientCertPath), new File(clientKeyPath)),
() -> {
timesOfReloadTask.getAndIncrement();
touchCert();
throw new RuntimeException("Mock exception in reload task");
});

Thread.sleep(5000);
certWatcher.close();
assertTrue(timesOfReloadTask.get() > 1);
}

Expand Down