Skip to content

Commit

Permalink
Stop kademlia bootstrap thread when node is stopped
Browse files Browse the repository at this point in the history
  • Loading branch information
ianopolous committed Mar 13, 2024
1 parent 7bc1975 commit e7452b8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/main/java/org/peergos/EmbeddedIpfs.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ public CompletableFuture<Void> stop() throws Exception {
records.close();
}
blockProvider.ifPresent(b -> b.stop());
dht.stopBootstrapThread();
return node != null ? node.stop() : CompletableFuture.completedFuture(null);
}

Expand Down
9 changes: 8 additions & 1 deletion src/main/java/org/peergos/protocol/dht/Kademlia.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.time.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.function.*;
import java.util.logging.*;
import java.util.stream.*;
Expand Down Expand Up @@ -73,9 +74,11 @@ public int bootstrapRoutingTable(Host host, List<MultiAddress> addrs, Predicate<
return successes;
}

private AtomicBoolean running = new AtomicBoolean(false);
public void startBootstrapThread(Host us) {
running.set(true);
new Thread(() -> {
while (true) {
while (running.get()) {
try {
bootstrap(us);
Thread.sleep(BOOTSTRAP_PERIOD_MILLIS);
Expand All @@ -86,6 +89,10 @@ public void startBootstrapThread(Host us) {
}, "Kademlia bootstrap").start();
}

public void stopBootstrapThread() {
running.set(false);
}

private boolean connectTo(Host us, PeerAddresses peer) {
try {
new Identify().dial(us, PeerId.fromBase58(peer.peerId.toBase58()), getPublic(peer)).getController().join().id().join();
Expand Down

0 comments on commit e7452b8

Please sign in to comment.