<commit_before>package mesosphere.util

import mesosphere.marathon.state.{ Timestamp, AppDefinition, PathId }

import scala.concurrent.duration._
import scala.util.Try

import org.apache.log4j.Logger

class RateLimiter {

  private val log = Logger.getLogger(getClass.getName)

  protected case class Delay(
    current: Deadline,
    future: Iterator[FiniteDuration])

  protected[this] val maxLaunchDelay = 1.hour

  protected[this] var taskLaunchDelays = Map[(PathId, Timestamp), Delay]()

  def getDelay(app: AppDefinition): Deadline =
    taskLaunchDelays.get(app.id -> app.version).map(_.current) getOrElse Deadline.now

  def addDelay(app: AppDefinition): Unit = {
    val newDelay = taskLaunchDelays.get(app.id -> app.version) match {
      case Some(Delay(current, future)) => Delay(future.next().fromNow, future)
      case None => Delay(
        app.backoff.fromNow,
        durations(app.backoff, app.backoffFactor)
      )
    }

    log.info(s"Task launch delay for [${app.id}] is now [${newDelay.current}]")

    taskLaunchDelays += ((app.id, app.version) -> newDelay)
  }

  def resetDelay(app: AppDefinition): Unit = {
    if (taskLaunchDelays contains (app.id -> app.version))
      log.info(s"Task launch delay for [${app.id} - ${app.version}}] reset to zero")
    taskLaunchDelays = taskLaunchDelays - (app.id -> app.version)
  }

  /**
    * Returns an infinite lazy stream of exponentially increasing durations.
    *
    * @param initial  the length of the first duration in the resulting stream
    * @param factor   the multiplier used to compute each successive<commit_msg>In task launch delay log output time left, not absolute time<commit_after>package mesosphere.util

import mesosphere.marathon.state.{ Timestamp, AppDefinition, PathId }

import scala.concurrent.duration._
import scala.util.Try

import org.apache.log4j.Logger

class RateLimiter {

  private val log = Logger.getLogger(getClass.getName)

  protected case class Delay(
    current: Deadline,
    future: Iterator[FiniteDuration])

  protected[this] val maxLaunchDelay = 1.hour

  protected[this] var taskLaunchDelays = Map[(PathId, Timestamp), Delay]()

  def getDelay(app: AppDefinition): Deadline =
    taskLaunchDelays.get(app.id -> app.version).map(_.current) getOrElse Deadline.now

  def addDelay(app: AppDefinition): Unit = {
    val newDelay = taskLaunchDelays.get(app.id -> app.version) match {
      case Some(Delay(current, future)) => Delay(future.next().fromNow, future)
      case None => Delay(
        app.backoff.fromNow,
        durations(app.backoff, app.backoffFactor)
      )
    }

    log.info(s"Task launch delay for [${app.id}] is now [${newDelay.current.timeLeft}]")

    taskLaunchDelays += ((app.id, app.version) -> newDelay)
  }

  def resetDelay(app: AppDefinition): Unit = {
    if (taskLaunchDelays contains (app.id -> app.version))
      log.info(s"Task launch delay for [${app.id} - ${app.version}}] reset to zero")
    taskLaunchDelays = taskLaunchDelays - (app.id -> app.version)
  }

  /**
    * Returns an infinite lazy stream of exponentially increasing durations.
    *
    * @param initial  the length of the first duration in the resulting stream
    * @param factor   the multiplier used to compute each successive
{'fraction_non_alphanumeric': 0.11432083589428396, 'fraction_numerical': 0.001229256299938537, 'mean_word_length': 3.703757225433526, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6980026', 'n_tokens_mistral': 1011, 'n_tokens_neox': 976, 'n_words': 267}