Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Publish message asynchronously #27

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/coney/coney.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ defmodule Coney do
ConnectionServer.publish(exchange_name, routing_key, message)
end

@spec publish_async(String.t(), binary()) :: :ok
def publish_async(exchange_name, message) do
ConnectionServer.publish_async(exchange_name, message)
end

@spec publish_async(String.t(), String.t(), binary()) :: :ok
def publish_async(exchange_name, routing_key, message) do
ConnectionServer.publish_async(exchange_name, routing_key, message)
end

@spec status() :: list({pid(), :pending | :connected | :disconnected})
def status do
ConnectionRegistry.status()
Expand Down
23 changes: 23 additions & 0 deletions lib/coney/connection_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ defmodule Coney.ConnectionServer do
GenServer.call(__MODULE__, {:publish, exchange_name, routing_key, message})
end

@spec publish_async(String.t(), any()) :: :ok
def publish_async(exchange_name, message) do
GenServer.cast(__MODULE__, {:publish, exchange_name, message})
end

@spec publish_async(String.t(), String.t(), any()) :: :ok
def publish_async(exchange_name, routing_key, message) do
GenServer.cast(__MODULE__, {:publish, exchange_name, routing_key, message})
end

@spec subscribe(any()) :: reference()
def subscribe(consumer) do
GenServer.call(__MODULE__, {:subscribe, consumer})
Expand Down Expand Up @@ -135,6 +145,19 @@ defmodule Coney.ConnectionServer do
{:reply, :published, state}
end

@impl GenServer
def handle_cast({:publish, exchange_name, message}, %State{} = state) do
state.adapter.publish(state.amqp_conn, exchange_name, "", message)

{:noreply, state}
end

def handle_cast({:publish, exchange_name, routing_key, message}, %State{} = state) do
state.adapter.publish(state.amqp_conn, exchange_name, routing_key, message)

{:noreply, state}
end

defp rabbitmq_connect(
%State{
adapter: adapter,
Expand Down
Loading