Pulsar.PartitionedProducer (Pulsar v2.8.11)

Copy Markdown View Source

A supervisor that manages individual producer groups for partitioned topics.

This module provides a logical abstraction over multiple producer groups, allowing the start_producer API to return a single PID for partitioned topics while maintaining the individual producer group architecture underneath.

The supervisor manages one producer group per partition, with the number of partitions provided by the caller.

Message Routing

Messages are routed to partitions based on the :partition_key option:

  • With :partition_key - Consistent hash routing via :erlang.phash2(partition_key, num_partitions)
  • Without :partition_key - Random selection

Summary

Functions

Returns a specification to start this module under a supervisor.

Gets information about all child producer groups managed by this supervisor.

Gets all producer processes from all partition groups managed by this supervisor.

Sends a message through the partitioned producer.

Starts a partitioned producer supervisor.

Stops a partitioned producer supervisor and all its child producer groups.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

get_partition_groups(supervisor_pid)

Gets information about all child producer groups managed by this supervisor.

Returns a list of {partition_topic, group_pid} tuples.

get_producers(supervisor_pid)

Gets all producer processes from all partition groups managed by this supervisor.

Returns a flat list of producer process PIDs from all partitions.

send_message(supervisor_pid, message, opts \\ [])

@spec send_message(pid(), binary(), keyword()) :: {:ok, map()} | {:error, term()}

Sends a message through the partitioned producer.

Routes the message to a partition based on the :partition_key option:

  • With :partition_key - Uses consistent hashing to route to a specific partition
  • Without :partition_key - Uses random partition selection

Parameters

Returns

{:ok, message_id} on success {:error, reason} on failure

start_link(name, topic, partitions, opts \\ [])

Starts a partitioned producer supervisor.

Parameters

  • name - Unique name for this partitioned producer
  • topic - The base partitioned topic name (without partition suffix)
  • partitions - Number of partitions for this topic
  • opts - Additional options passed to individual producer groups

Returns

{:ok, pid} - The supervisor PID that manages all partition producer groups {:error, reason} - Error if the supervisor failed to start

stop(supervisor_pid, reason \\ :normal, timeout \\ :infinity)

Stops a partitioned producer supervisor and all its child producer groups.