-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
92 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 0 additions & 33 deletions
33
dht/src/main/scala/com/github/torrentdam/bittorrent/dht/PingRoutine.scala
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
dht/src/main/scala/com/github/torrentdam/bittorrent/dht/RoutingTableRefresh.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.github.torrentdam.bittorrent.dht | ||
|
||
import cats.effect.IO | ||
import cats.syntax.all.* | ||
import cats.effect.cps.{*, given} | ||
import cats.effect.std.Random | ||
import org.legogroup.woof.{Logger, given} | ||
|
||
import scala.concurrent.duration.{DurationInt, FiniteDuration} | ||
|
||
class RoutingTableRefresh(table: RoutingTable[IO], client: Client, discovery: PeerDiscovery)(using logger: Logger[IO], random: Random[IO]): | ||
|
||
def runOnce: IO[Unit] = async[IO]: | ||
val buckets = table.buckets.await | ||
val (fresh, stale) = buckets.toList.partition(_.nodes.values.exists(_.isGood)) | ||
if stale.nonEmpty then | ||
refreshBuckets(stale).await | ||
val nodes = fresh.flatMap(_.nodes.values) | ||
pingNodes(nodes).await | ||
|
||
def runEvery(period: FiniteDuration): IO[Unit] = | ||
IO | ||
.sleep(period) | ||
.productR(runOnce) | ||
.foreverM | ||
.handleErrorWith: e => | ||
logger.error(s"PingRoutine failed: $e") | ||
.foreverM | ||
|
||
private def pingNodes(nodes: List[RoutingTable.Node]) = async[IO]: | ||
logger.info(s"Pinging ${nodes.size} nodes").await | ||
val results = nodes | ||
.parTraverse { node => | ||
client.ping(node.address).timeout(5.seconds).attempt.map(_.bimap(_ => node.id, _ => node.id)) | ||
} | ||
.await | ||
val (bad, good) = results.partitionMap(identity) | ||
logger.info(s"Got ${good.size} good nodes and ${bad.size} bad nodes").await | ||
table.updateGoodness(good.toSet, bad.toSet).await | ||
|
||
private def refreshBuckets(buckets: List[RoutingTable.TreeNode.Bucket]) = async[IO]: | ||
logger.info(s"Found ${buckets.size} stale buckets").await | ||
buckets | ||
.parTraverse: bucket => | ||
val randomId = NodeId.randomInRange(bucket.from, bucket.until).await | ||
discovery.findNodes(randomId).take(32).compile.drain | ||
.await | ||
|
||
|