Created
March 22, 2017 12:26
-
-
Save ponkotuy/ac1c5c73d1362d22551eeb112339ed9d to your computer and use it in GitHub Desktop.
Parallel Downloader per hostname
This file contains hidden or 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
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