<commit_before>package lila.hub

import scala.concurrent.duration._
import scala.concurrent.stm._

import lila.base.LilaException

/*
 * Sequential like an actor, but for async functions,
 * and using an STM backend instead of akka actor.
 */
trait Duct {

  type ReceiveAsync = PartialFunction[Any, Fu[Any]]

  // implement async behaviour here
  protected val process: PartialFunction[Any, Fu[Any]]

  def !(msg: Any): Unit = atomic { implicit txn =>
    if (isBusy()) queue addLast msg
    else {
      isBusy() = true
      doProcess(msg)
    }
  }

  private[this] val isBusy = Ref(false)

  private[this] val queue = new java.util.ArrayDeque[Any]

  private[this] def doProcess(msg: Any): Fu[Any] =
    process.applyOrElse(msg, Duct.fallback) addEffectAnyway postProcess

  private[this] def postProcess = queue.poll match {
    case null => atomic { implicit txn =>
      isBusy() = false
    }
    case queuedMsg => doProcess(queuedMsg)
  }
}

object Duct {

  private val fallback = { msg: Any =>
    lila.log("Duct").warn(s"unhandled msg: $msg")
    funit<commit_msg>remove duct queue, use future composition instead<commit_after>package lila.hub

import scala.concurrent.duration._
import scala.concurrent.stm._

import lila.base.LilaException

/*
 * Sequential like an actor, but for async functions,
 * and using an STM backend instead of akka actor.
 */
trait Duct {

  type ReceiveAsync = PartialFunction[Any, Fu[Any]]

  // implement async behaviour here
  protected val process: ReceiveAsync

  def !(msg: Any): Unit = atomic { implicit txn =>
    current.transform(_ >> process.applyOrElse(msg, Duct.fallback))
  }

  private[this] val current: Ref[Fu[Any]] = Ref(funit)
}

object Duct {

  private val fallback = { msg: Any =>
    lila.log("Duct").warn(s"unhandled msg: $msg")
    funit
{'fraction_non_alphanumeric': 0.11191135734072022, 'fraction_numerical': 0.0, 'mean_word_length': 3.654639175257732, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, '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': '3494359', 'n_tokens_mistral': 594, 'n_tokens_neox': 576, 'n_words': 170}