Skip to content

Latest commit

 

History

History
116 lines (81 loc) · 3.14 KB

REF_API_MIGRATION.md

File metadata and controls

116 lines (81 loc) · 3.14 KB

Ref API Migration Guide

This document describes migration to the split reference model:

  • LocalRef<M>: local-only, zero-overhead hot path
  • RemoteRef<C>: remote wire contract
  • AnyRef<C> / Addr<C>: optional location-transparent wrapper

The goal is to preserve local performance while making remote usage explicit and type-safe.

1) Why split refs

Previous mixed usage tends to blur local and remote semantics. The split model enforces:

  1. Local call sites stay local-only (LocalRef<M>).
  2. Remote call sites declare a wire contract (RemoteRef<C> + RefContract).
  3. Location transparency is opt-in (AnyRef<C>), not default.

2) Migration Map

  1. Existing generated actor refs keep working.
  2. For local-only call sites, switch to as_local_ref() (or From conversions).
  3. For remote call sites, define a RefContract and use RemoteRef<C>.
  4. Only where needed, wrap local/remote as AnyRef<C>.

3) Local Path Migration

Generated refs expose local conversion helpers.

let actor_ref = spawn.actor(); // e.g. MyActorRef
let local: axactor::LocalRef<MyActorMsg> = actor_ref.as_local_ref();
local.try_send(MyActorMsg::Ping {})?;

LocalRef<M> uses the same mailbox path as local actor refs (MailboxTx<M>). No remote serialization/network branch is introduced.

4) Remote Path Migration

Define a remote contract with wire-safe types:

use axactor::RefContract;

struct ChatContract;

impl RefContract for ChatContract {
    type Tell = ChatTell;
    type Ask = ChatAsk;
    type Reply = ChatReply;
}

Then create remote ref:

let remote = axactor::RemoteRef::<ChatContract>::new(
    client,
    "room:general",
    std::time::Duration::from_secs(1),
);

Default codec is SimdJsonCodec. Contract associated types are compile-time constrained by RemoteSafe.

5) Location-Transparent Wrapper (AnyRef)

Use only at explicit boundaries:

let any_local = axactor::AnyRef::local(local_contract_ref);
let any_remote = axactor::AnyRef::remote(remote_ref);

AnyRef introduces a dispatch branch by design. Avoid using it on local hot loops where static local refs are sufficient.

6) Monitor Semantics

  • RemoteRef monitor: uses cluster monitor/down protocol.
  • AnyRef::monitor() is supported for both local and remote variants.
  • Local contract monitor requires explicit adapter:
let local = axactor::LocalContractRef::<MyContract>::new(tell, send_wait, ask)
    .with_monitor_fn(|| Box::pin(async move {
        // return mpsc::Receiver<axactor::cluster::DownEvent>
        todo!()
    }));

7) Guardrails and Validation

Existing tests cover:

  1. ref API correctness (axactor/tests/ref_api_test.rs)
  2. UI compile-fail guardrails (axactor/tests/ui/*.rs)
  3. local hot-path benchmark smoke (axactor/benches/local_ref_bench.rs)

Recommended checks:

cargo test -p axactor --test ref_api_test
cargo test -p axactor --test compile_tests
cargo test -p axactor --bench local_ref_bench -- --nocapture

8) Practical Rule of Thumb

  1. Actor-internal and node-local services: LocalRef
  2. Cluster-facing APIs: RemoteRef
  3. Mixed boundaries (plugin/bus/router abstractions): AnyRef