Closed timbuchwaldt closed 8 years ago
I confirm this issue, I followed the documentation and I ran into a problem where I ran out of file descriptors. It took me a while to find out that it's not an http client that I'm using, but actually Exredis that's never closing connections.
Guys, @timbuchwaldt @arnvald you can create PR with note about this in documentation :)
And refer other to learn OTP principes.
I'm new to Elixir, and I spent about an hour trying to get it working with Supervisors, Workers, and so forth. I didn't have much luck, and it's probably something really dumb. I ended up using the Exredis.Api.defaultclient method in order to get a reusable client connection. But, with what I've read about supervisors, it seems like I must be doing something wrong.
I am using it in a Phoenix project where it's an alternate datasource to PostgreSQL. The project is here: https://github.com/micahwedemeyer/crashswitch_demo
I was trying to integrate it with the other Supervisors here: https://github.com/micahwedemeyer/crashswitch_demo/blob/master/lib/crashswitch_demo.ex
With my limited OTP knowledge, I just couldn't figure it out.
I'm not asking anyone to debug it for me, but if anyone can give an example of using ExRedis in an OTP application, I'd love to look at it.
And thanks for making ExRedis!
@micahwedemeyer, below is an example of how to register and supervise an Exredis process.
defmodule SomeApp do
use Application
def start(_type, _args) do
import Supervisor.Spec, warn: false
children = [
worker(SomeApp.RedisRepo, [:myredis, "redis://localhost:6379/0"]),
]
opts = [strategy: :one_for_one, name: SomeApp.Supervisor]
Supervisor.start_link(children, opts)
end
end
defmodule SomeApp.RedisRepo do
def start_link(name, uri) do
client = Exredis.start_using_connection_string(uri)
true = Process.register(client, name)
{:ok, client}
end
end
The supervisor will call SomeApp.RedisRepo.start_link
with the supplied arguments and expect {:ok, pid}
as a return value. The process will then be supervised. This example also registers the process name as :myredis
so you can use that when querying instead of supplying a pid.
You can call it like so:
Exredis.query(:myredis, ["PING"])
If you want a pool of processes take a look at https://github.com/devinus/poolboy
Thanks! This is a big help!
Readme is updated. Makes sense to close issue.
The first example in the docs has a big problem once used:
There should at least be a line of warning that this usage is just an example and noone should really do that (as it piles up connections used just once), also all parameters in piped functions are expected to be in parenthesis. I will hopefully find time to send a PR later.