cache world map network IP localizations
This commit is contained in:
parent
f57efd3bd3
commit
62b2b2b524
|
@ -2,13 +2,12 @@ package lila.security
|
|||
|
||||
import com.sanoma.cda.geoip.{ MaxMindIpGeo, IpLocation }
|
||||
|
||||
import lila.memo.Builder
|
||||
import scala.concurrent.duration._
|
||||
|
||||
final class GeoIP(file: String, cacheTtl: Duration) {
|
||||
|
||||
private val geoIp = MaxMindIpGeo(file, 0)
|
||||
private val cache = Builder.cache(cacheTtl, compute)
|
||||
private val cache = lila.memo.Builder.cache(cacheTtl, compute)
|
||||
|
||||
private def compute(ip: String): Option[Location] =
|
||||
geoIp getLocation ip map Location.apply
|
||||
|
|
|
@ -6,11 +6,11 @@ import com.sanoma.cda.geoip.MaxMindIpGeo
|
|||
import lila.common.PimpedConfig._
|
||||
|
||||
final class Env(
|
||||
system: akka.actor.ActorSystem,
|
||||
config: Config) {
|
||||
system: akka.actor.ActorSystem,
|
||||
config: Config) {
|
||||
|
||||
private val GeoIPFile = config getString "geoip.file"
|
||||
private val GeoIPCacheSize = config getInt "geoip.cache_size"
|
||||
private val GeoIPCacheTtl = config duration "geoip.cache_ttl"
|
||||
private val PlayersCacheSize = config getInt "players.cache_size"
|
||||
|
||||
lazy val players = new Players(PlayersCacheSize)
|
||||
|
@ -18,7 +18,8 @@ final class Env(
|
|||
lazy val stream = new Stream(
|
||||
system = system,
|
||||
players = players,
|
||||
geoIp = MaxMindIpGeo(GeoIPFile, GeoIPCacheSize))
|
||||
geoIp = MaxMindIpGeo(GeoIPFile, 0),
|
||||
geoIpCacheTtl = GeoIPCacheTtl)
|
||||
}
|
||||
|
||||
object Env {
|
||||
|
|
|
@ -7,16 +7,22 @@ import java.io.File
|
|||
import lila.hub.actorApi.round.MoveEvent
|
||||
import play.api.libs.iteratee._
|
||||
import play.api.libs.json._
|
||||
import scala.concurrent.duration._
|
||||
|
||||
final class Stream(
|
||||
system: ActorSystem,
|
||||
players: Players,
|
||||
geoIp: MaxMindIpGeo) {
|
||||
geoIp: MaxMindIpGeo,
|
||||
geoIpCacheTtl: Duration) {
|
||||
|
||||
private val (enumerator, channel) = Concurrent.broadcast[MoveEvent]
|
||||
|
||||
private val ipCache = lila.memo.Builder.cache(geoIpCacheTtl, localizeIp)
|
||||
private def localizeIp(ip: String): Option[Location] =
|
||||
geoIp getLocation ip flatMap Location.apply
|
||||
|
||||
def processMove(move: MoveEvent) =
|
||||
geoIp getLocation move.ip flatMap Location.apply match {
|
||||
ipCache get move.ip match {
|
||||
case None => Input.Empty
|
||||
case Some(loc) =>
|
||||
val opponentLoc = players.getOpponentLocation(move.gameId, loc)
|
||||
|
|
Loading…
Reference in a new issue