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

Make Redis channel configurable #66

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
32 changes: 12 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,25 @@ end
children = [
# ...,
{Phoenix.PubSub,
name: MyApp.PubSub,
adapter: Phoenix.PubSub.Redis,
host: "192.168.1.100",
node_name: System.get_env("NODE")}
```

Config Options

Option | Description | Default |
:-----------------------| :------------------------------------------------------------------------ | :------------- |
`:name` | The required name to register the PubSub processes, ie: `MyApp.PubSub` | |
`:node_name` | The required and unique name of the node, ie: `System.get_env("NODE")` | |
`:url` | The redis-server URL, ie: `redis://username:password@host:port` | |
`:host` | The redis-server host IP | `"127.0.0.1"` |
`:port` | The redis-server port | `6379` |
`:password` | The redis-server password | `""` |
`:compression_level` | Compression level applied to serialized terms (`0` - none, `9` - highest) | `0` |
`:socket_opts` | The redis-server network layer options | `[]` |

And also add `:phoenix_pubsub_redis` to your list of applications:

```elixir
# mix.exs
def application do
[mod: {MyApp, []},
applications: [..., :phoenix, :phoenix_pubsub_redis]]
end
```
Option | Description | Default |
:-----------------------| :------------------------------------------------------------------------ | :---------------- |
`:name` | The required name to register the PubSub processes, ie: `MyApp.PubSub` | |
`:node_name` | The required and unique name of the node, ie: `System.get_env("NODE")` | |
`:url` | The redis-server URL, ie: `redis://username:password@host:port` | |
`:host` | The redis-server host IP | `"127.0.0.1"` |
`:port` | The redis-server port | `6379` |
`:password` | The redis-server password | `""` |
`:compression_level` | Compression level applied to serialized terms (`0` - none, `9` - highest) | `0` |
`:socket_opts` | The redis-server network layer options | `[]` |
`:redis_channel` | The redis pubsub channel, i.e. `myapp:pubsub` | Derived from name |

## License

Expand Down
6 changes: 6 additions & 0 deletions lib/phoenix_pubsub_redis/redis.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Phoenix.PubSub.Redis do
To start it, list it in your supervision tree as:

{Phoenix.PubSub,
name: MyApp.PubSub,
adapter: Phoenix.PubSub.Redis,
host: "192.168.1.100",
node_name: System.get_env("NODE")}
Expand All @@ -28,6 +29,7 @@ defmodule Phoenix.PubSub.Redis do
* `:compression_level` - Compression level applied to serialized terms - from `0` (no compression), to `9` (highest). Defaults `0`
* `:socket_opts` - List of options that are passed to the network layer when connecting to the Redis server. Default `[]`
* `:sentinel` - Redix sentinel configuration. Default to `nil`
* `:redis_channel` - Redis channel to use, derived from the name by default

"""

Expand Down Expand Up @@ -66,6 +68,7 @@ defmodule Phoenix.PubSub.Redis do
pubsub_name = Keyword.fetch!(opts, :name)
adapter_name = Keyword.fetch!(opts, :adapter_name)
compression_level = Keyword.get(opts, :compression_level, 0)
redis_channel = Keyword.get(opts, :redis_channel, default_redis_channel(adapter_name))

opts = handle_url_opts(opts)
opts = Keyword.merge(@defaults, opts)
Expand All @@ -77,6 +80,7 @@ defmodule Phoenix.PubSub.Redis do
:ets.new(adapter_name, [:public, :named_table, read_concurrency: true])
:ets.insert(adapter_name, {:node_name, node_name})
:ets.insert(adapter_name, {:compression_level, compression_level})
:ets.insert(adapter_name, {:redis_channel, redis_channel})

pool_opts = [
name: {:local, adapter_name},
Expand Down Expand Up @@ -123,4 +127,6 @@ defmodule Phoenix.PubSub.Redis do

:ok
end

defp default_redis_channel(adapter_name), do: "phx:#{adapter_name}"
end
8 changes: 5 additions & 3 deletions lib/phoenix_pubsub_redis/redis_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defmodule Phoenix.PubSub.RedisServer do
end

defp publish(adapter_name, mode, node_name, topic, message, dispatcher) do
namespace = redis_namespace(adapter_name)
namespace = redis_channel(adapter_name)
compression_level = compression_level(adapter_name)
redis_msg = {@redis_msg_vsn, mode, node_name, topic, message, dispatcher}
bin_msg = :erlang.term_to_binary(redis_msg, compressed: compression_level)
Expand Down Expand Up @@ -125,7 +125,7 @@ defmodule Phoenix.PubSub.RedisServer do
end

defp establish_success(%{redix_pid: redix_pid, adapter_name: adapter_name} = state) do
{:ok, _reference} = Redix.PubSub.subscribe(redix_pid, redis_namespace(adapter_name), self())
{:ok, _reference} = Redix.PubSub.subscribe(redix_pid, redis_channel(adapter_name), self())
state
end

Expand All @@ -139,5 +139,7 @@ defmodule Phoenix.PubSub.RedisServer do
end
end

defp redis_namespace(adapter_name), do: "phx:#{adapter_name}"
defp redis_channel(adapter_name) do
:ets.lookup_element(adapter_name, :redis_channel, 2)
end
end