Skip to content

Commit

Permalink
binary pack option
Browse files Browse the repository at this point in the history
  • Loading branch information
devsnek committed Jan 18, 2023
1 parent 1093c51 commit ce9808b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/manifold.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ defmodule Manifold do
def send([pid], message, options), do: __MODULE__.send(pid, message, options)

def send(pids, message, options) when is_list(pids) do
message = Utils.pack_message(options[:pack_mode], message)

case options[:send_mode] do
:offload ->
Sender.send(current_sender(), current_partitioner(), pids, message)
Expand Down
8 changes: 8 additions & 0 deletions lib/manifold/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,12 @@ defmodule Manifold.Utils do

hibernate_delay + :rand.uniform(hibernate_jitter)
end

@spec pack_message(mode :: atom(), message :: term()) :: term()
def pack_message(:binary, message), do: {:manifold_binary, :erlang.term_to_binary(message)}
def pack_message(_mode, message), do: message

@spec unpack_message(message :: term()) :: term()
def unpack_message({:manifold_binary, binary}), do: :erlang.binary_to_term(binary)
def unpack_message(message), do: message
end
2 changes: 2 additions & 0 deletions lib/manifold/worker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ defmodule Manifold.Worker do
end

def handle_cast({:send, [pid], message}, nil) do
message = Utils.unpack_message(message)
send(pid, message)
{:noreply, nil}
end

def handle_cast({:send, pids, message}, nil) do
message = Utils.unpack_message(message)
for pid <- pids, do: send(pid, message)
{:noreply, nil}
end
Expand Down
16 changes: 16 additions & 0 deletions test/manifold_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ defmodule ManifoldTest do
end
end

test "pack_mode option" do
me = self()
message = :hello
pids = for _ <- 0..10000 do
spawn_link fn ->
receive do
message -> send(me, {self(), message})
end
end
end
Manifold.send(pids, message, pack_mode: :binary)
for pid <- pids do
assert_receive {^pid, ^message}, 1000
end
end

test "send to list of one" do
me = self()
message = :hello
Expand Down

0 comments on commit ce9808b

Please sign in to comment.