Skip to content

Commit

Permalink
#164 Add second, minut and hour unit for rate limiting
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Jan 18, 2020
1 parent 0165b5d commit 2c4f448
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
18 changes: 9 additions & 9 deletions app/org/thp/cortex/models/Worker.scala
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package org.thp.cortex.models

import javax.inject.{Inject, Singleton}

import scala.concurrent.Future
import scala.util.Try

import play.api.libs.json.{JsObject, JsString, Json}

import org.elastic4play.models.JsonFormat.enumFormat
import org.elastic4play.models.{AttributeDef, BaseEntity, ChildModelDef, EntityDef, HiveEnumeration, AttributeFormat F, AttributeOption O}
import org.elastic4play.utils.Hasher

import org.thp.cortex.models.JsonFormat.workerTypeFormat
import play.api.libs.json.{JsObject, JsString, Json}

import scala.concurrent.Future
import scala.util.Try

object RateUnit extends Enumeration with HiveEnumeration {
type Type = Value
val Day = Value(1)
val Month = Value(30)
val Second = Value(1)
val Minute = Value(60)
val Hour = Value(60 * 60)
val Day = Value(60 * 60 * 24)
val Month = Value(60 * 60 * 24 * 30)
implicit val reads = enumFormat(this)
}

Expand Down
2 changes: 1 addition & 1 deletion app/org/thp/cortex/services/JobSrv.scala
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ class JobSrv(
import org.elastic4play.services.QueryDSL._
val now = new Date().getTime
logger.info(s"Checking rate limit on worker ${worker.name()} from ${new Date(now - rateUnit.id.toLong * 24 * 60 * 60 * 1000)}")
stats(and("createdAt" ~>= (now - rateUnit.id.toLong * 24 * 60 * 60 * 1000), "workerId" ~= worker.id), Seq(selectCount)).map { s
stats(and("createdAt" ~>= (now - rateUnit.id.toLong * 1000), "workerId" ~= worker.id), Seq(selectCount)).map { s
val count = (s \ "count").as[Long]
logger.info(s"$count analysis found (limit is $rate)")
count < rate
Expand Down

0 comments on commit 2c4f448

Please sign in to comment.