This repository has been archived by the owner on May 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Http.scala
56 lines (45 loc) · 1.47 KB
/
Http.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package org.w3.vs.http
import akka.actor._
import org.w3.util._
import play.Logger
import org.w3.vs.model._
import org.w3.vs.VSConfiguration
import scalaz.Scalaz._
import org.w3.util.akkaext._
import AuthorityManager.encode
case class Fetch(url: URL, action: HttpAction, runId: RunId, jobId: JobId)
case class SetSleepTime(value: Long)
case object HowManyPendingRequests
/**
* This is an actor which encapsulates the AsyncHttpClient library.
*/
class Http()(implicit configuration: VSConfiguration) extends Actor with PathAwareActor {
val httpClient = configuration.httpClient
// TODO really???
val logger = Logger.of(classOf[Http])
def getAuthorityManagerRefOrCreate(authority: Authority): ActorRef = {
val encoded = encode(authority)
try {
context.children.find(_.path.name === encoded) getOrElse {
context.actorOf(Props(new AuthorityManager(authority)), name = encoded)
}
} catch {
case iane: InvalidActorNameException => context.actorFor(self.path / encoded)
}
}
def receive = {
case Tell(Child(name), msg) => {
val authorityManagerRef = getAuthorityManagerRefOrCreate(name)
authorityManagerRef forward msg
}
case fetch @ Fetch(url, _, _, _) => {
val authority = url.authority
val authorityManagerRef = getAuthorityManagerRefOrCreate(authority)
authorityManagerRef forward fetch
}
}
override def postStop() = {
logger.debug("closing asyncHttpClient")
httpClient.close()
}
}