From 62b2b2b524b966318956226f82107be7ba020f7d Mon Sep 17 00:00:00 2001 From: Thibault Duplessis Date: Mon, 23 Mar 2015 01:57:28 +0100 Subject: [PATCH] cache world map network IP localizations --- modules/security/src/main/GeoIP.scala | 3 +-- modules/worldMap/src/main/Env.scala | 9 +++++---- modules/worldMap/src/main/Stream.scala | 10 ++++++++-- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/modules/security/src/main/GeoIP.scala b/modules/security/src/main/GeoIP.scala index 9413e0a823..68b38a1ba4 100644 --- a/modules/security/src/main/GeoIP.scala +++ b/modules/security/src/main/GeoIP.scala @@ -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 diff --git a/modules/worldMap/src/main/Env.scala b/modules/worldMap/src/main/Env.scala index 7e17a4ccc8..5c62b43e67 100644 --- a/modules/worldMap/src/main/Env.scala +++ b/modules/worldMap/src/main/Env.scala @@ -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 { diff --git a/modules/worldMap/src/main/Stream.scala b/modules/worldMap/src/main/Stream.scala index b392b734ee..c2b8feba39 100644 --- a/modules/worldMap/src/main/Stream.scala +++ b/modules/worldMap/src/main/Stream.scala @@ -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)