<commit_before>package org.http4s.client.blaze

import java.nio.ByteBuffer
import java.util.concurrent.TimeoutException
import java.util.concurrent.atomic.AtomicReference

import scala.annotation.tailrec
import scala.concurrent.{Promise, Future}
import scala.concurrent.duration.Duration
import scala.util.{Failure, Success}

import org.http4s.blaze.pipeline.MidStage
import org.http4s.blaze.pipeline.Command.{Error, OutboundCommand, EOF, Disconnect}
import org.http4s.blaze.util.{ Cancellable, TickWheelExecutor }


final private class ClientTimeoutStage(idleTimeout: Duration, requestTimeout: Duration, exec: TickWheelExecutor)
  extends MidStage[ByteBuffer, ByteBuffer]
{ stage =>

  import ClientTimeoutStage.Closed

  private implicit val ec = org.http4s.blaze.util.Execution.directec

  // The 'per request' timeout. Lasts the lifetime of the stage.
  private val activeReqTimeout = new AtomicReference[ Cancellable](null)

  // The timeoutState contains a Cancellable, null, or a TimeoutException
  // It will also act as the point of synchronization
  private val timeoutState = new AtomicReference[AnyRef](null)

  override def name: String = s"ClientTimeoutStage: Idle: $idleTimeout, Request: $requestTimeout"

  /////////// Private impl bits //////////////////////////////////////////
  private def killswitch(name: String, timeout: Duration) = new Runnable {
    override def run(): Unit = {
      logger.debug(s"Client $name stage is disconnecting due to timeout after $timeout.")

      // check the idle timeout conditions
      timeoutState.getAndSet(new TimeoutException(s"Client $name timeout after $timeout.")) match {
        case null => // noop
        case c: Cancellable => c.cancel() // this should be the registration of us
        case _: TimeoutException => // Interesting that we got here.
      }

      // Cancel the active request timeout if it exists
      activeReqTimeout.getAndSet(Closed) match {
        case null    => /* We beat the startup. Maybe timeout is 0? */
          sendOutboundCommand(Disconnect)

        case Closed  => /* Already closed, no need to disconnect */<commit_msg>Clean up timeout log message<commit_after>package org.http4s.client.blaze

import java.nio.ByteBuffer
import java.util.concurrent.TimeoutException
import java.util.concurrent.atomic.AtomicReference

import scala.annotation.tailrec
import scala.concurrent.{Promise, Future}
import scala.concurrent.duration.Duration
import scala.util.{Failure, Success}

import org.http4s.blaze.pipeline.MidStage
import org.http4s.blaze.pipeline.Command.{Error, OutboundCommand, EOF, Disconnect}
import org.http4s.blaze.util.{ Cancellable, TickWheelExecutor }


final private class ClientTimeoutStage(idleTimeout: Duration, requestTimeout: Duration, exec: TickWheelExecutor)
  extends MidStage[ByteBuffer, ByteBuffer]
{ stage =>

  import ClientTimeoutStage.Closed

  private implicit val ec = org.http4s.blaze.util.Execution.directec

  // The 'per request' timeout. Lasts the lifetime of the stage.
  private val activeReqTimeout = new AtomicReference[ Cancellable](null)

  // The timeoutState contains a Cancellable, null, or a TimeoutException
  // It will also act as the point of synchronization
  private val timeoutState = new AtomicReference[AnyRef](null)

  override def name: String = s"ClientTimeoutStage: Idle: $idleTimeout, Request: $requestTimeout"

  /////////// Private impl bits //////////////////////////////////////////
  private def killswitch(name: String, timeout: Duration) = new Runnable {
    override def run(): Unit = {
      logger.debug(s"Client stage is disconnecting due to $name timeout after $timeout.")

      // check the idle timeout conditions
      timeoutState.getAndSet(new TimeoutException(s"Client $name timeout after $timeout.")) match {
        case null => // noop
        case c: Cancellable => c.cancel() // this should be the registration of us
        case _: TimeoutException => // Interesting that we got here.
      }

      // Cancel the active request timeout if it exists
      activeReqTimeout.getAndSet(Closed) match {
        case null    => /* We beat the startup. Maybe timeout is 0? */
          sendOutboundCommand(Disconnect)

        case Closed  => /* Already closed, no need to disconnect */
{'fraction_non_alphanumeric': 0.10827279118818842, 'fraction_numerical': 0.002812280290602297, 'mean_word_length': 3.516402116402116, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '6002414', 'n_tokens_mistral': 1169, 'n_tokens_neox': 1130, 'n_words': 372}