mcl_core_pubsub (mcl_core v1.0.0)

View Source

Publish onto the mesh via macula's supervised macula_publisher, instead of every service hand-rolling catch macula:publish(...).

Confirmed by a 2026-08-24 workspace-wide survey (see guides/mesh_native_services.md, piece C): ~20 service repos independently wrote the same

case {mcl_core:macula_client(), mcl_core_identity:realm()} of {{ok, Pool}, {ok, Realm}} -> catch macula:publish(...), ok; _ -> ok end

idiom, in three different flavors of what to do with the outcome (silently discard it, log it, or return it to the caller). Building this against macula_publisher rather than a raw synchronous call gets every caller two things none of the hand-rolled sites had: cancellable in-flight publishes, and free pubsub.publish_started_v1 / pubsub.publish_completed_v1 mesh facts around every publish.

{error, mesh_unavailable} when this service isn't attached to a pool or has no realm configured yet — same contract as mcl_core:mesh_handles/0, so a caller that already checks for that shape elsewhere in its own code doesn't need a second case to learn.

Summary

Functions

Declare the desired subscription set: one supervised macula_subscriber per declared subscription not already running as declared (module implementing the macula_subscriber behaviour, and sources naming who may deliver to it); any running one whose declaration no longer matches is stopped. Safe to call repeatedly whenever the desired set changes at runtime — e.g. a federation_inbox-shaped service adding one topic per newly- registered entity — diffs against what's currently running and touches only the delta, restarting exactly the subscription whose declaration changed. Self-heals on a 30s reconcile tick independent of any caller: a topic that couldn't start because the mesh wasn't attached yet, or whose child exhausted its own restart budget after a full pool replacement, is retried automatically, and a failure that persists is on /health rather than in a log nobody reads.

How many publishes ended with their publisher exiting abnormally since this node started. Also on /health.

A publish on the mesh alone, with publish/3's options: what publish/3 did in mcl_om 1.9.0, and what the bridge does with a publish now.

Publish Payload on Topic using this service's own mesh handle and realm. Default mode is async_silent: starts the publish and returns ok immediately without waiting for it to land, matching how most real call sites in the fleet already behave — nobody blocks on a fact publish today.

Publish Payload on every topic in Topics. Convenience for a one-fact-fans-to-N-topics service (a news service publishes to a firehose topic plus one sub-topic per non-empty axis). Every topic is attempted regardless of an earlier one's outcome; returns ok only if every publish returned ok.

Publish on an explicit pool and realm, with publish/3's options. publish/2,3 resolve the pool and realm and come here.

The outcome-handling mode a publish actually uses: Opts's mode, defaulting to async_silent.

The realm a publish actually uses: Opts's realm override when given, otherwise this service's own default realm.

Start a supervised macula_publisher with a caller-supplied callback Module, using this service's own mesh handle and realm — the escape hatch for outcome handling publish/2,3's three fixed modes don't cover (e.g. retry-with-backoff on failure, fully decoupled from any caller waiting synchronously — a real need that just hasn't shown up in a surveyed repo yet, unlike the three modes, which were each derived from one). Resolves mcl_core:mesh_handles/0 the same way publish/2,3 does, so reaching for this instead of publish/2,3 doesn't mean duplicating that boilerplate — that resolution is the one thing worth getting from mcl_core_pubsub either way. Module must implement -behaviour(macula_publisher) itself; mcl_core_pubsub has no say over what its handle_published/2 does.

As start_publisher/3, with Args passed to Module:init/1.

Types

mode/0

-type mode() :: async_silent | async_log | sync.

publish_opts/0

-type publish_opts() ::
          #{realm => binary(), mode => mode(), timeout => pos_integer(), publisher_opts => map()}.

Functions

ensure_subscriptions(Desired)

-spec ensure_subscriptions([mcl_core_service:subscription()]) -> ok.

Declare the desired subscription set: one supervised macula_subscriber per declared subscription not already running as declared (module implementing the macula_subscriber behaviour, and sources naming who may deliver to it); any running one whose declaration no longer matches is stopped. Safe to call repeatedly whenever the desired set changes at runtime — e.g. a federation_inbox-shaped service adding one topic per newly- registered entity — diffs against what's currently running and touches only the delta, restarting exactly the subscription whose declaration changed. Self-heals on a 30s reconcile tick independent of any caller: a topic that couldn't start because the mesh wasn't attached yet, or whose child exhausted its own restart budget after a full pool replacement, is retried automatically, and a failure that persists is on /health rather than in a log nobody reads.

A declaration mcl_core cannot wire — a missing sources above all — raises {mcl_core_subscription_invalid, #{topic, reason}} in the caller, in the same shape as mcl_core_service:subscription()s own doc. See mcl_core_pubsub_subscriptions for the mechanism.

failed_publishes()

-spec failed_publishes() -> non_neg_integer().

How many publishes ended with their publisher exiting abnormally since this node started. Also on /health.

handle_published(Result, State)

init(_)

mesh_publish(Topic, Payload, Opts)

-spec mesh_publish(binary(), term(), publish_opts()) -> ok | {error, term()}.

A publish on the mesh alone, with publish/3's options: what publish/3 did in mcl_om 1.9.0, and what the bridge does with a publish now.

publish(Topic, Payload)

-spec publish(binary(), term()) -> ok | {error, term()}.

Publish Payload on Topic using this service's own mesh handle and realm. Default mode is async_silent: starts the publish and returns ok immediately without waiting for it to land, matching how most real call sites in the fleet already behave — nobody blocks on a fact publish today.

publish(Topic, Payload, Opts)

-spec publish(binary(), term(), publish_opts()) -> ok | {error, term()}.

As publish/2, with Opts:

realm — publish on a realm other than this service's own. A real, deployed need (see the dronex, robo-rumbler and biotope/society services in the survey): a fleet realm for mcl_core's own plumbing and a separate business/public realm for the facts themselves, off the same pool. mode — async_silent (default): fire-and-forget, outcome discarded. async_log: fire-and-forget, but a failed publish is logged (the victron, warden and sentinel services all re-added this after an earlier silent-swallow version ate refused frames unnoticed). sync: blocks until the publish resolves and returns its outcome (the biotope/society and mpong services all already block their own caller today; this just moves that blocking here). timeout — sync mode only. Milliseconds to wait for the outcome before returning {error, timeout}. Default 5000. publisher_opts — macula_publisher's own start options, passed through: announce => false publishes the payload alone, one frame instead of three.

A PUBLISHER THAT DIES IS A FAILED PUBLISH, NEVER THE CALLER'S DEATH. Each publisher runs under a watcher of its own rather than linked to the caller. macula 12.2 returns {ok, Pid} before the start announcement, so a failed announcement ends the publisher after the start; a crashed publish worker ends it too. Linked to the caller, either exit killed the service process that published. The watcher logs the exit, counts it (failed_publishes/0, and on /health), and answers a sync caller {error, {publisher_exited, Reason}} at once.

THROUGH THE TRANSPORT (mcl_core_transport:publish/3). With the LAN transport configured and a subscriber on the LAN, the publish goes to the LAN's subscribers and the options above, which are the mesh's, do not apply; a realm option always means the mesh. Otherwise the publish is mesh_publish/3's.

publish_many(Topics, Payload)

-spec publish_many([binary()], term()) -> ok | {error, term()}.

Publish Payload on every topic in Topics. Convenience for a one-fact-fans-to-N-topics service (a news service publishes to a firehose topic plus one sub-topic per non-empty axis). Every topic is attempted regardless of an earlier one's outcome; returns ok only if every publish returned ok.

publish_many(Topics, Payload, Opts)

-spec publish_many([binary()], term(), publish_opts()) -> ok | {error, term()}.

publish_on(Pool, Realm, Topic, Payload, Opts)

-spec publish_on(pid(), binary(), binary(), term(), publish_opts()) -> ok | {error, term()}.

Publish on an explicit pool and realm, with publish/3's options. publish/2,3 resolve the pool and realm and come here.

resolve_mode(Opts)

-spec resolve_mode(publish_opts()) -> mode().

The outcome-handling mode a publish actually uses: Opts's mode, defaulting to async_silent.

resolve_realm(Opts, DefaultRealm)

-spec resolve_realm(publish_opts(), binary()) -> binary().

The realm a publish actually uses: Opts's realm override when given, otherwise this service's own default realm.

start_publisher(Module, Topic, Payload)

-spec start_publisher(module(), binary(), term()) -> {ok, pid()} | {error, term()}.

Start a supervised macula_publisher with a caller-supplied callback Module, using this service's own mesh handle and realm — the escape hatch for outcome handling publish/2,3's three fixed modes don't cover (e.g. retry-with-backoff on failure, fully decoupled from any caller waiting synchronously — a real need that just hasn't shown up in a surveyed repo yet, unlike the three modes, which were each derived from one). Resolves mcl_core:mesh_handles/0 the same way publish/2,3 does, so reaching for this instead of publish/2,3 doesn't mean duplicating that boilerplate — that resolution is the one thing worth getting from mcl_core_pubsub either way. Module must implement -behaviour(macula_publisher) itself; mcl_core_pubsub has no say over what its handle_published/2 does.

Args is passed to Module:init/1, same as macula_publisher:start_link/6 itself; default undefined when omitted. Opts accepts realm only (see publish/3) — mode and timeout don't apply, since the caller's own Module owns the outcome entirely.

start_publisher(Module, Topic, Payload, Args)

-spec start_publisher(module(), binary(), term(), term()) -> {ok, pid()} | {error, term()}.

As start_publisher/3, with Args passed to Module:init/1.

start_publisher(Module, Topic, Payload, Args, Opts)

-spec start_publisher(module(), binary(), term(), term(), #{realm => binary()}) ->
                         {ok, pid()} | {error, term()}.

As start_publisher/4, with Opts (realm only — see above).