remove VarMemo
This commit is contained in:
parent
4248e92819
commit
19c052c172
|
@ -1,37 +0,0 @@
|
|||
package lila.memo
|
||||
|
||||
import scala.concurrent.{ Future, Await }
|
||||
import scala.concurrent.duration._
|
||||
import scala.reflect._
|
||||
import akka.actor._
|
||||
import akka.pattern.ask
|
||||
|
||||
import play.api.libs.concurrent.Execution.Implicits._
|
||||
import play.api.libs.concurrent.Akka
|
||||
import play.api.Play.current
|
||||
|
||||
final class VarMemo[A : ClassTag](load: Fu[A], atMost: FiniteDuration = 10.seconds) {
|
||||
|
||||
private case object Get
|
||||
private implicit val timeout = makeTimeout(atMost)
|
||||
|
||||
def get: Fu[A] = (actor ? Get).mapTo[A]
|
||||
|
||||
def reload(fa: Fu[A]): Funit = fuccess(actor ! fa)
|
||||
|
||||
private val actor = Akka.system.actorOf(Props(new Actor {
|
||||
|
||||
private var value: Option[A] = none
|
||||
private def reset(a: A) { value = a.some }
|
||||
|
||||
def receive = {
|
||||
|
||||
case Get ⇒ value match {
|
||||
case None ⇒ sender ! (load.await ~ reset)
|
||||
case Some(v) ⇒ sender ! v
|
||||
}
|
||||
|
||||
case fa: Fu[A] ⇒ reset(fa.await)
|
||||
}
|
||||
}))
|
||||
}
|
|
@ -20,6 +20,7 @@ final class Env(
|
|||
val FirewallCookieName = config getString "firewall.cookie.name"
|
||||
val FirewallCookieEnabled = config getBoolean "firewall.cookie.enabled"
|
||||
val FirewallCollectionFirewall = config getString "firewall.collection.firewall"
|
||||
val FirewallCachedIpsTtl = config duration "firewall.cached.ips.ttl"
|
||||
val FloodDuration = config duration "flood.duration"
|
||||
}
|
||||
import settings._
|
||||
|
@ -30,7 +31,8 @@ final class Env(
|
|||
|
||||
lazy val firewall = new Firewall(
|
||||
cookieName = FirewallCookieName.some filter (_ ⇒ FirewallCookieEnabled),
|
||||
enabled = FirewallEnabled)(db(FirewallCollectionFirewall))
|
||||
enabled = FirewallEnabled,
|
||||
cachedIpsTtl = FirewallCachedIpsTtl)(db(FirewallCollectionFirewall))
|
||||
|
||||
lazy val flood = new Flood(FloodDuration)
|
||||
|
||||
|
|
|
@ -2,26 +2,23 @@ package lila.security
|
|||
|
||||
import lila.common.PimpedJson._
|
||||
import lila.http.LilaCookie
|
||||
import lila.memo.VarMemo
|
||||
import lila.db.Types.Coll
|
||||
import lila.db.Types._
|
||||
import lila.db.api._
|
||||
|
||||
import scala.concurrent.duration._
|
||||
import akka.util.Timeout
|
||||
|
||||
import play.api.mvc.{ RequestHeader, Handler, Action, Cookies }
|
||||
import play.api.mvc.Results.Redirect
|
||||
import play.api.libs.json._
|
||||
import play.api.libs.concurrent.Execution.Implicits._
|
||||
|
||||
import play.modules.reactivemongo.Implicits._
|
||||
|
||||
import spray.caching.{ LruCache, Cache }
|
||||
import org.joda.time.DateTime
|
||||
import ornicar.scalalib.Random
|
||||
|
||||
final class Firewall(
|
||||
cookieName: Option[String],
|
||||
enabled: Boolean)(implicit coll: Coll) {
|
||||
enabled: Boolean,
|
||||
cachedIpsTtl: Duration)(implicit coll: Coll) {
|
||||
|
||||
val requestHandler: (RequestHeader ⇒ Option[Handler]) =
|
||||
if (enabled)
|
||||
|
@ -45,12 +42,10 @@ final class Firewall(
|
|||
|
||||
def accepts(req: RequestHeader): Boolean = !blocks(req)
|
||||
|
||||
def refresh: Funit = ipsMemo reload fetch
|
||||
|
||||
def blockIp(ip: String): Funit =
|
||||
if (validIp(ip) && !blocksIp(ip)) {
|
||||
log("Block IP: " + ip)
|
||||
coll.insert(Json.obj("_id" -> ip, "date" -> DateTime.now)) >> refresh
|
||||
coll.insert(Json.obj("_id" -> ip, "date" -> DateTime.now)) >> cachedIps.clear
|
||||
}
|
||||
else fuccess(log("Invalid IP block: " + ip))
|
||||
|
||||
|
@ -82,10 +77,13 @@ final class Firewall(
|
|||
private def validIp(ip: String) =
|
||||
(ipRegex matches ip) && ip != "127.0.0.1" && ip != "0.0.0.0"
|
||||
|
||||
private implicit val timeout = Timeout(2.seconds)
|
||||
private val ipsMemo = new VarMemo(fetch, 2.seconds)
|
||||
private lazy val cachedIps = new {
|
||||
private val cache: Cache[Set[String]] = LruCache(timeToLive = cachedIpsTtl)
|
||||
def apply: Fu[Set[String]] = cache.fromFuture(true)(fetch)
|
||||
def clear { cache.clear }
|
||||
}
|
||||
|
||||
private def ips: Set[String] = ipsMemo.get.await
|
||||
private def ips: Set[String] = cachedIps.apply.await
|
||||
|
||||
private def fetch: Fu[Set[String]] =
|
||||
coll.genericQueryBuilder
|
||||
|
|
Loading…
Reference in a new issue