-
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
5 changed files
with
128 additions
and
72 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
28 changes: 28 additions & 0 deletions
28
dht/src/main/scala/com/github/torrentdam/bittorrent/dht/PingRoutine.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,28 @@ | ||
package com.github.torrentdam.bittorrent.dht | ||
|
||
import cats.effect.IO | ||
import cats.syntax.all.* | ||
import cats.effect.cps.{*, given} | ||
import org.legogroup.woof.{Logger, given} | ||
|
||
import scala.concurrent.duration.DurationInt | ||
|
||
class PingRoutine(table: RoutingTable[IO], client: Client[IO])(using logger: Logger[IO]): | ||
|
||
def run: IO[Unit] = async[IO]: | ||
val nodes = table.allNodes.await | ||
logger.info(s"Pinging ${nodes.size} nodes").await | ||
val queries = nodes.map { node => | ||
client.ping(node.address).timeout(5.seconds).attempt.map(_.bimap(_ => node.id, _ => node.id)) | ||
} | ||
val results = queries.parSequence.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) | ||
|
||
def runForever: IO[Unit] = | ||
run | ||
.handleErrorWith: e => | ||
logger.error(s"PingRoutine failed: $e") | ||
.productR(IO.sleep(10.minutes)) | ||
.foreverM |
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