Skip to content

Instantly share code, notes, and snippets.

@ponkotuy
Created March 22, 2017 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ponkotuy/ac1c5c73d1362d22551eeb112339ed9d to your computer and use it in GitHub Desktop.
Save ponkotuy/ac1c5c73d1362d22551eeb112339ed9d to your computer and use it in GitHub Desktop.
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