InterceptorActor

cc.otavia.core.interceptor.InterceptorActor
abstract class InterceptorActor[M <: Call] extends StateActor[M]

Base class for interceptor actors that wrap a target Address and forward messages through a chain.

Interceptors are normal actors that use the standard Stack coroutine pattern (override resumeAsk / resumeNotice with state matching). The forwardAsk helper encapsulates the common pattern of forwarding to the next address and suspending until the reply arrives.

Concept mapping (for understanding, not API):

// Industry concept        -> Otavia equivalent
// chain.proceed()         -> forwardAsk(stack)
// preHandle return false  -> stack.return(rejectReply) without calling forwardAsk
// postHandle              -> match FutureState with ForwardStateId, process reply, then stack.return
// onion model             -> Stack StartState -> suspend -> FutureState naturally implements this

Example - logging interceptor:

class LoggingInterceptor(val next: Address[HttpRequest]) extends InterceptorActor[HttpRequest] {
  override protected def resumeAsk(stack: AskStack[HttpRequest & Ask[? <: Reply]]): StackYield = {
    stack.state match {
      case _: StartState =>
        stack.attach(System.nanoTime())
        forwardAsk(stack)
      case state: FutureState[_] if state.id == ForwardStateId =>
        val elapsed = (System.nanoTime() - stack.attach[Long]) / 1_000_000
        logger.info(s"${stack.ask.method} ${stack.ask.path} -> ${elapsed}ms")
        stack.`return`(state.future.getNow.asInstanceOf)
    }
  }
}

Type parameters

M

the message type this interceptor handles, must match the target actor's type

Attributes

Graph
Supertypes
class StateActor[M]
trait Actor[M]
class Object
trait Matchable
class Any

Members list

Value members

Abstract methods

protected def next: Address[M]

The next interceptor or target actor in the chain. Users provide this via a constructor parameter.

The next interceptor or target actor in the chain. Users provide this via a constructor parameter.

Attributes

Concrete methods

protected def forwardAsk(stack: AskStack[M & Ask[_ <: Reply]]): StackYield

Forward the ask to next and suspend the current stack. When the reply arrives, resumeAsk is called again with a FutureState whose FutureState.id equals ForwardStateId.

Forward the ask to next and suspend the current stack. When the reply arrives, resumeAsk is called again with a FutureState whose FutureState.id equals ForwardStateId.

Attributes

protected def forwardNotice(stack: NoticeStack[M & Notice]): StackYield

Forward the notice to next and complete the stack.

Forward the notice to next and complete the stack.

Attributes

Inherited methods

final def actorId: Long

The unique id of this actor distributed by ActorSystem, when a actor instance is mounted to a ActorSystem, the actor system will distribute a unique id to the instance.

The unique id of this actor distributed by ActorSystem, when a actor instance is mounted to a ActorSystem, the actor system will distribute a unique id to the instance.

Attributes

Returns

id number

Inherited from:
Actor
final def autowire[A <: Actor[_] : ClassTag](qualifier: String): Address[MessageOf[A]]

Attributes

Inherited from:
Actor
final def autowire[A <: Actor[_] : ClassTag](qualifier: Option[String] = ...): Address[MessageOf[A]]

Attributes

Inherited from:
Actor
def batchable: Boolean

Whether this actor supports batch message processing. When true, the ActorSystem dispatches multiple messages in bulk rather than individually.

Whether this actor supports batch message processing. When true, the ActorSystem dispatches multiple messages in bulk rather than individually.

Attributes

Inherited from:
Actor
final override def context: ActorContext

Context of this actor. This method can only used after actor instance mount to actor system

Context of this actor. This method can only used after actor instance mount to actor system

Attributes

Definition Classes
AbstractActor -> Actor
Inherited from:
AbstractActor (hidden)
inline protected def deriveDispatch: Unit

Discover handler methods in this actor and generate match/case dispatch. Call once in the actor constructor body. The macro scans for methods with AskStack or NoticeStack parameters, verifies they cover all message types in the actor's type parameter, and generates dispatch code equivalent to hand-written match/case.

Discover handler methods in this actor and generate match/case dispatch. Call once in the actor constructor body. The macro scans for methods with AskStack or NoticeStack parameters, verifies they cover all message types in the actor's type parameter, and generates dispatch code equivalent to hand-written match/case.

Attributes

Inherited from:
AbstractActor (hidden)
def maxBatchSize: Int

Maximum number of messages per batch. Used by the scheduling system.

Maximum number of messages per batch. Used by the scheduling system.

Attributes

Inherited from:
Actor

Attributes

Inherited from:
Actor
def nice: Int

Attributes

Inherited from:
Actor
final protected def noticeSelfHead(call: Notice & M): Unit

Send a Notice to self, inserted at the head of the notice mailbox before all other pending notices.

Send a Notice to self, inserted at the head of the notice mailbox before all other pending notices.

Attributes

Inherited from:
AbstractActor (hidden)
protected def resumeAsk(stack: AskStack[M & Ask[_ <: Reply]]): StackYield

Handle an Ask message. Called on initial receipt and on each resume after an awaited reply completes.

Handle an Ask message. Called on initial receipt and on each resume after an awaited reply completes.

Match on stack.state to distinguish initial entry from resumed execution:

override protected def resumeAsk(stack: AskStack[MyAsk]): StackYield =
  stack.state match
    case _: StartState =>
      val state = FutureState[MyReply]()
      address.ask(MyRequest(), state.future)
      stack.suspend(state)
    case state: FutureState[MyReply] =>
      stack.return(state.future.getNow)

Attributes

Inherited from:
AbstractActor (hidden)
protected def resumeBatchAsk(stack: BatchAskStack[M & Ask[_ <: Reply]]): StackYield

Handle a batch of Ask messages in a single stack.

Handle a batch of Ask messages in a single stack.

Attributes

Inherited from:
AbstractActor (hidden)
protected def resumeBatchNotice(stack: BatchNoticeStack[M & Notice]): StackYield

Handle a batch of Notice messages in a single stack.

Handle a batch of Notice messages in a single stack.

Attributes

Inherited from:
AbstractActor (hidden)
protected def resumeNotice(stack: NoticeStack[M & Notice]): StackYield

Handle a Notice message. Called on initial receipt and on each resume.

Handle a Notice message. Called on initial receipt and on each resume.

Attributes

Inherited from:
AbstractActor (hidden)
override def self: ActorAddress[M]

Typed self-address for this StateActor.

Typed self-address for this StateActor.

Attributes

Definition Classes
StateActor -> AbstractActor
Inherited from:
StateActor
protected def setAskDispatch(fn: (AskStack[M & Ask[_ <: Reply]]) => StackYield): Unit

Set the ask dispatch function. Called by deriveDispatch macro expansion.

Set the ask dispatch function. Called by deriveDispatch macro expansion.

Attributes

Inherited from:
AbstractActor (hidden)
protected def setNoticeDispatch(fn: (NoticeStack[M & Notice]) => StackYield): Unit

Set the notice dispatch function. Called by deriveDispatch macro expansion.

Set the notice dispatch function. Called by deriveDispatch macro expansion.

Attributes

Inherited from:
AbstractActor (hidden)
final def system: ActorSystem

The ActorSystem of this actor instance is running

The ActorSystem of this actor instance is running

Attributes

Returns

ActorSystem

Inherited from:
Actor
final def timer: Timer

Attributes

Inherited from:
Actor

Concrete fields

protected val ForwardStateId: Int

State ID used internally by forwardAsk. Users should pick positive IDs for their own FutureState instances to avoid collisions.

State ID used internally by forwardAsk. Users should pick positive IDs for their own FutureState instances to avoid collisions.

Attributes

Inherited fields

val batchAskFilter: (Ask[_]) => Boolean

Filter function for batching Ask messages. Return true to include in batch.

Filter function for batching Ask messages. Return true to include in batch.

Attributes

Inherited from:
Actor
val batchNoticeFilter: Notice => Boolean

Filter function for batching Notice messages. Return true to include in batch.

Filter function for batching Notice messages. Return true to include in batch.

Attributes

Inherited from:
Actor
protected var logger: Logger

Attributes

Inherited from:
AbstractActor (hidden)