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

Extend Redix config to support all start_link/1 options #5

Merged
merged 1 commit into from
Feb 26, 2024
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ in your config to:
```elixir
config :guardian, Guardian.DB, adapter: GuardianRedis.Adapter
```

### Added
- support for all configuration options of Redix (except for name)
- support for `name_prefix` config

### Breaking
- bump `GuardianDB` from 2.1.1 to 3.0.0
- `port` and `pool_size` config options have to be integers now
- default Redix connection name prefix changed from `redix` to `guardian_redis`. Previous name can be preserved by adding `name_prefix: "redix"` to the config

## V0.1.0

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ config :guardian_redis, :redis,
pool_size: 10
```

More options are available as described in `GuardianRedis.Redix`.


## Implement Guardian.DB adapter for a different storage

Expand Down
4 changes: 2 additions & 2 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ config :guardian, Guardian.DB, adapter: GuardianRedis.Adapter

config :guardian_redis, :redis,
host: System.get_env("REDIS_HOST", "127.0.0.1"),
port: System.get_env("REDIS_PORT", "6379"),
pool_size: System.get_env("REDIS_POOL_SIZE", "1")
port: String.to_integer(System.get_env("REDIS_PORT", "6379")),
pool_size: String.to_integer(System.get_env("REDIS_POOL_SIZE", "1"))
55 changes: 35 additions & 20 deletions lib/redix.ex
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
defmodule GuardianRedis.Redix do
@moduledoc """
Redix module
Redix module.

### Configuration
Configuration options are the same as in `Redix.start_link/1`, except for
`pool_size`, `name_prefix`, and `name`.

- `pool_size` - determines how many Redix connections are created; defaults to `1`
- `name_prefix` - the prefix used in `name`; defaults to `"guardian_redis"`

The `name` option is overwritten based on the given `name_prefix` and `pool_size`.
In the case of the example below, `name` would be `:g_redis_0` for the first
Redis connection, and `:g_redis_1` for the second.

```
config :guardian_redis, :redis,
host: "127.0.0.1",
port: 6379,
pool_size: 2,
name_prefix: "g_redis"
```
"""
@default_redis_host "127.0.0.1"
@default_redis_port 6379
@default_redis_name_prefix "guardian_redis"
@default_redis_pool_size 1

@doc """
Specs for the Redix connections.
"""
def child_spec(_args) do
config = Keyword.drop(redis_config(), [:pool_size, :name_prefix, :name])
name_prefix = name_prefix()

children =
for index <- 0..(pool_size() - 1) do
Supervisor.child_spec(
{Redix, name: :"redix_#{index}", host: redis_host(), port: redis_port()},
id: {Redix, index}
)
args = Keyword.put(config, :name, :"#{name_prefix}_#{index}")

Supervisor.child_spec({Redix, args}, id: {Redix, index})
end

%{
Expand All @@ -40,27 +60,22 @@ defmodule GuardianRedis.Redix do
{:ok, Redix.Protocol.redis_value()}
| {:error, atom() | Redix.Error.t() | Redix.ConnectionError.t()}
def command(command_params) do
Redix.command(:"redix_#{random_index()}", command_params)
Redix.command(:"#{name_prefix()}_#{random_index()}", command_params)
end

defp redis_host do
redis_config()[:host] || @default_redis_host
end

defp redis_port do
# casting integer port value to String in case port value comes from ENV
("#{redis_config()[:port]}" || "#{@default_redis_port}") |> String.to_integer()
defp random_index do
Enum.random(0..(pool_size() - 1))
end

defp pool_size do
"#{redis_config()[:pool_size] || @default_redis_pool_size}" |> String.to_integer()
Keyword.get(redis_config(), :pool_size, @default_redis_pool_size)
end

defp redis_config do
Application.get_env(:guardian_redis, :redis)
defp name_prefix do
Keyword.get(redis_config(), :name_prefix, @default_redis_name_prefix)
end

defp random_index() do
Enum.random(0..(pool_size() - 1))
defp redis_config do
Application.get_env(:guardian_redis, :redis, [])
end
end
Loading