This repository has been archived by the owner on Nov 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
196 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
defmodule GenRMQ.Consumer.QueueConfiguration do | ||
@moduledoc """ | ||
Represents configuration of a Consumer queue. | ||
While this module exists to make management of Consumer queue configurations | ||
easier, right now it should be considered a private implementation detail | ||
with respect to the consumer configuration API. | ||
""" | ||
|
||
defstruct name: nil, | ||
ttl: nil, | ||
max_priority: nil, | ||
durable: true | ||
|
||
@type t :: %__MODULE__{ | ||
name: String.t(), | ||
ttl: nil | pos_integer, | ||
max_priority: nil | pos_integer, | ||
durable: boolean | ||
} | ||
|
||
@type queue_options :: | ||
[] | ||
| [durable: boolean] | ||
| [durable: boolean, max_priority: pos_integer] | ||
| [durable: boolean, ttl: pos_integer] | ||
| [max_priority: pos_integer] | ||
| [max_priority: pos_integer, ttl: pos_integer] | ||
| [durable: boolean, max_priority: pos_integer, ttl: pos_integer] | ||
|
||
@spec new(String.t(), queue_options) :: t | ||
def new(name, args \\ []) do | ||
queue_ttl = Keyword.get(args, :ttl, nil) | ||
queue_mp = Keyword.get(args, :max_priority, nil) | ||
durable = Keyword.get(args, :durable, true) | ||
|
||
%__MODULE__{ | ||
name: name, | ||
ttl: queue_ttl, | ||
max_priority: set_max_priority_to_highest_value(queue_mp), | ||
durable: durable | ||
} | ||
end | ||
|
||
@spec new(String.t(), boolean, nil | pos_integer, nil | pos_integer) :: t | ||
def new(name, durable, ttl, mp) do | ||
%__MODULE__{ | ||
name: name, | ||
ttl: ttl, | ||
max_priority: set_max_priority_to_highest_value(mp), | ||
durable: durable | ||
} | ||
end | ||
|
||
@spec name(t) :: String.t() | ||
def name(%__MODULE__{name: n}), do: n | ||
|
||
@spec durable(t) :: boolean | ||
def durable(%__MODULE__{durable: d}), do: d | ||
|
||
@spec ttl(t) :: nil | pos_integer | ||
def ttl(%__MODULE__{ttl: ttl_v}), do: ttl_v | ||
|
||
@spec max_priority(t) :: nil | pos_integer | ||
def max_priority(%__MODULE__{max_priority: mp}), do: mp | ||
|
||
def build_queue_arguments(%__MODULE__{} = qc, arguments) do | ||
args_with_priority = setup_priority(arguments, qc.max_priority) | ||
|
||
qc | ||
|> build_ttl_arguments(args_with_priority) | ||
end | ||
|
||
def build_ttl_arguments(%__MODULE__{} = qc, arguments) do | ||
setup_ttl(arguments, qc.ttl) | ||
end | ||
|
||
defp setup_ttl(arguments, nil), do: arguments | ||
defp setup_ttl(arguments, ttl), do: [{"x-expires", :long, ttl} | arguments] | ||
|
||
defp setup_priority(arguments, max_priority) when is_integer(max_priority), | ||
do: [{"x-max-priority", :long, max_priority} | arguments] | ||
|
||
defp setup_priority(arguments, _), do: arguments | ||
|
||
@max_priority 255 | ||
|
||
defp set_max_priority_to_highest_value(nil), do: nil | ||
|
||
defp set_max_priority_to_highest_value(mp) | ||
when is_integer(mp) and mp > @max_priority do | ||
255 | ||
end | ||
|
||
defp set_max_priority_to_highest_value(mp) when is_integer(mp), do: mp | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
defmodule GenRMQ.Consumer.QueueConfigurationTest do | ||
use ExUnit.Case, async: true | ||
alias GenRMQ.Consumer.QueueConfiguration | ||
|
||
test "may be built with just a queue name" do | ||
qc = QueueConfiguration.new("some_queue_name") | ||
assert "some_queue_name" == QueueConfiguration.name(qc) | ||
end | ||
|
||
test "durable should default to true" do | ||
qc = QueueConfiguration.new("some_queue_name") | ||
assert QueueConfiguration.durable(qc) | ||
end | ||
|
||
test "may be built with all options" do | ||
name = "some_queue_name" | ||
ttl = 5000 | ||
durable = false | ||
max_priority = 200 | ||
|
||
qc = | ||
QueueConfiguration.new( | ||
name, | ||
durable, | ||
ttl, | ||
max_priority | ||
) | ||
|
||
assert name == QueueConfiguration.name(qc) | ||
assert durable == QueueConfiguration.durable(qc) | ||
assert ttl == QueueConfiguration.ttl(qc) | ||
assert max_priority == QueueConfiguration.max_priority(qc) | ||
end | ||
|
||
test "sets max_priority values that are too large to the max" do | ||
qc = QueueConfiguration.new("some_queue_name", max_priority: 500) | ||
assert 255 == QueueConfiguration.max_priority(qc) | ||
end | ||
|
||
test "builds empty arguments when neither ttl or max_priority are provided" do | ||
qc = QueueConfiguration.new("some_queue_name") | ||
assert [] == QueueConfiguration.build_queue_arguments(qc, []) | ||
end | ||
|
||
test "builds correct arguments when ttl and max_priority are provided" do | ||
ttl = 5000 | ||
max_priority = 5 | ||
|
||
qc = | ||
QueueConfiguration.new( | ||
"some_queue_name", | ||
ttl: ttl, | ||
max_priority: max_priority | ||
) | ||
|
||
assert [{"x-expires", :long, ttl}, {"x-max-priority", :long, max_priority}] == | ||
QueueConfiguration.build_queue_arguments(qc, []) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters