Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

Commit

Permalink
be more defensive when reading env vars for throttling
Browse files Browse the repository at this point in the history
  • Loading branch information
mavilein committed Feb 14, 2018
1 parent 3952ddc commit 289ae58
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
10 changes: 6 additions & 4 deletions server/api/src/main/scala/com/prisma/api/server/ApiServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ import com.prisma.metrics.extensions.TimeResponseDirectiveImpl
import com.prisma.shared.models.{ProjectId, ProjectWithClientId}
import com.prisma.logging.{LogData, LogKey}
import com.prisma.logging.LogDataWrites.logDataWrites
import com.prisma.util.env.EnvUtils
import play.api.libs.json.Json
import spray.json._

import scala.concurrent.Future
import scala.language.postfixOps
import scala.util.Try

case class ApiServer(
schemaBuilder: SchemaBuilder,
Expand All @@ -50,14 +52,14 @@ case class ApiServer(

lazy val throttler: Option[Throttler[ProjectId]] = {
for {
throttlingRate <- sys.env.get("THROTTLING_RATE")
maxCallsInFlights <- sys.env.get("THROTTLING_MAX_CALLS_IN_FLIGHT")
throttlingRate <- EnvUtils.asInt("THROTTLING_RATE")
maxCallsInFlights <- EnvUtils.asInt("THROTTLING_MAX_CALLS_IN_FLIGHT")
} yield {
val per = sys.env.getOrElse("THROTTLING_RATE_PER_SECONDS", "1")
val per = EnvUtils.asInt("THROTTLING_RATE_PER_SECONDS").getOrElse(1)
Throttler[ProjectId](
groupBy = pid => pid.name + "_" + pid.stage,
amount = throttlingRate.toInt,
per = per.toInt.seconds,
per = per.seconds,
timeout = 25.seconds,
maxCallsInFlight = maxCallsInFlights.toInt
)
Expand Down
13 changes: 13 additions & 0 deletions server/api/src/main/scala/com/prisma/util/env/EnvUtils.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.prisma.util.env

import scala.util.Try

object EnvUtils {
def asInt(name: String): Option[Int] = {
for {
value <- sys.env.get(name)
converted <- Try(value.toInt).toOption
} yield converted
}

}

0 comments on commit 289ae58

Please sign in to comment.