Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Parallel Downloader per hostname
package actors
import java.net.URI
import akka.actor.{Actor, ActorRef, Props}
import akka.pattern.{ask, pipe}
import akka.util.Timeout
import skinny.http.HTTP
import scala.collection.mutable
import scala.concurrent.ExecutionContext
import scala.concurrent.duration._
class Downloader(ec: ExecutionContext) extends Actor {
implicit val timeout = Timeout(30.second)
implicit val _ec: ExecutionContext = ec
val downloadActor: mutable.Map[String, ActorRef] = mutable.Map()
override def receive = {
case method: Method =>
val host = new URI(method.url).getHost
val actor = downloadActor.getOrElseUpdate(host, createActor(host))
println("receive")
(actor ? method).pipeTo(sender())
}
def createActor(host: String): ActorRef =
context.actorOf(Props(new DownloadHost(host)))
}
class DownloadHost(host: String) extends Actor {
import Method._
val http = new HTTP
http.defaultConnectTimeoutMillis = 30.seconds.toMillis.toInt
override def receive = {
case Get(url: String) =>
sender ! http.get(url)
}
}
sealed abstract class Method {
def url: String
}
object Method {
case class Get(url: String) extends Method
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.