<commit_before>package com.ldaniels528.broadway.core.actors

import akka.actor.Actor
import com.ldaniels528.broadway.core.actors.Actors.BWxActorRef
import com.ldaniels528.broadway.core.actors.Actors.Implicits._
import com.ldaniels528.broadway.server.ServerConfig
import org.slf4j.LoggerFactory

import scala.concurrent.ExecutionContext
import scala.language.postfixOps

/**
 * Throttling Actor
 * @author <NAME> <<EMAIL>>
 */
class ThrottlingActor(config: ServerConfig, actor: BWxActorRef, rateLimit: Double)(implicit ec: ExecutionContext) extends Actor {
  private lazy val logger = LoggerFactory.getLogger(getClass)
  private val throttlePerMessage = Math.max(1000d / rateLimit, 1).toLong
  private var lastUpdate = System.currentTimeMillis()
  private var lastLogUpdate = System.currentTimeMillis()
  private var lastMessageCount = 0
  private var messageCount = 0
  private var rate: Double = 0

  override def receive = {
    case message =>
      actor ! message
      messageCount += 1
      computeRate()
      Thread.sleep(throttlePerMessage)
  }

  private def computeRate(): Double = {
    val deltaTime = (System.currentTimeMillis() - lastUpdate).toDouble / 1000d
    if (deltaTime >= 1d) {
      val count = messageCount - lastMessageCount
      rate = count.toDouble / deltaTime
      lastMessageCount = messageCount
      lastUpdate = System.currentTimeMillis()

      if(System.currentTimeMillis() - lastLogUpdate >= 15000) {
        logger.info(f"Throughput rate is $rate%.1f")
        lastLogUpdate = System.currentTimeMillis()
      }
    }
    rate
  }

}<commit_msg>introduced an actor to throttle the output of a host actor<commit_after>package com.ldaniels528.broadway.core.actors

import akka.actor.Actor
import com.ldaniels528.broadway.core.actors.Actors.BWxActorRef
import com.ldaniels528.broadway.core.actors.Actors.Implicits._

import scala.concurrent.ExecutionContext
import scala.language.postfixOps

/**
 * Throttling Actor
 * @author <NAME> <<EMAIL>>
 */
class ThrottlingActor(actor: BWxActorRef, rateLimit: Double)(implicit ec: ExecutionContext) extends Actor {
  private val throttlePerMessage = Math.max(1000d / rateLimit, 1).toLong

  override def receive = {
    case message =>
      Thread.sleep(throttlePerMessage)
      actor ! message
  }

}
{'fraction_non_alphanumeric': 0.09329829172141918, 'fraction_numerical': 0.02058694699956198, 'mean_word_length': 4.098214285714286, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '831550', 'n_tokens_mistral': 739, 'n_tokens_neox': 689, 'n_words': 158}