Skip to content
Draft

p #12

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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Supported Redis features:
* Pipelining
* Authentication & multiple dbs
* Pubsub
* SSL connections

## Example

Expand All @@ -27,6 +28,10 @@ To connect to a Redis instance listening on a Unix domain socket:

{ok, C1} = eredis:start_link({local, "/var/run/redis.sock"}, 0).

To connect to a Redis instance with SSL enabled:

{ok, C2} = eredis:start_link("127.0.0.1", 6379, 0, "", 100, 5000, true).

MSET and MGET:

```erlang
Expand Down Expand Up @@ -91,6 +96,13 @@ ok
3>
```

SSL PubSub:

```erl
1> {ok, Sub} = eredis_sub:start_link("127.0.0.1", 6379, "", 100, infinity, drop, true).
2> Receiver = spawn_link(fun() -> eredis_sub:controlling_process(Sub), eredis_sub:subscribe(Sub, [<<"foo">>]), eredis_sub:receiver(Sub) end).
```

EUnit tests:

```console
Expand Down Expand Up @@ -125,6 +137,7 @@ the following arguments:
* Reconnect sleep, integer of milliseconds to sleep between reconnect attempts
* Connect timeout, timeout value in milliseconds to use in `gen_tcp:connect`, default is 5000
* Socket options, proplist of options to be sent to `gen_tcp:connect`, default is `?SOCKET_OPTS`
* IsSSL, boolean flag for SSL connection, default is false

## Reconnecting on Redis down / network failure / timeout / etc

Expand Down
5 changes: 4 additions & 1 deletion include/eredis_sub.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@
msg_state = need_ack :: ready | need_ack,

interval :: integer() | undefined,
tref :: reference() | undefined
tref :: reference() | undefined,

%% Whether to use SSL connection
is_ssl = false :: boolean()
}).
50 changes: 45 additions & 5 deletions src/eredis_sub.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
%%
-module(eredis_sub).
-include("eredis.hrl").
-include("eredis_sub.hrl").

%% Default timeout for calls to the client gen_server
%% Specified in http://www.erlang.org/doc/man/gen_server.html#call-3
-define(TIMEOUT, 5000).

-export([start_link/0, start_link/1, start_link/3, start_link/6, stop/1,
-export([start_link/0, start_link/1, start_link/3, start_link/4, start_link/6, start_link/7, stop/1,
controlling_process/1, controlling_process/2, controlling_process/3,
ack_message/1, subscribe/2, unsubscribe/2, channels/1]).

-export([psubscribe/2,punsubscribe/2]).

-export([receiver/1, sub_example/0, pub_example/0]).
-export([receiver/1, sub_example/0, pub_example/0, ssl_sub_example/0]).

-export([psub_example/0,ppub_example/0]).
-export([psub_example/0,ppub_example/0, ssl_psub_example/0]).

%%
%% PUBLIC API
Expand All @@ -27,6 +28,10 @@ start_link() ->

start_link(Host, Port, Password) ->
start_link(Host, Port, Password, 100, infinity, drop).
start_link([]).

start_link(Host, Port, Password, IsSSL) ->
start_link(Host, Port, Password, 100, infinity, drop, IsSSL).

start_link(Host, Port, Password, ReconnectSleep,
MaxQueueSize, QueueBehaviour)
Expand All @@ -38,7 +43,21 @@ start_link(Host, Port, Password, ReconnectSleep,
(QueueBehaviour =:= drop orelse QueueBehaviour =:= exit) ->

eredis_sub_client:start_link(Host, Port, Password, ReconnectSleep,
MaxQueueSize, QueueBehaviour).
MaxQueueSize, QueueBehaviour, false).

%% @doc: Start link with SSL support
start_link(Host, Port, Password, ReconnectSleep,
MaxQueueSize, QueueBehaviour, IsSSL)
when is_list(Host) andalso
is_integer(Port) andalso
is_list(Password) andalso
(is_integer(ReconnectSleep) orelse ReconnectSleep =:= no_reconnect) andalso
(is_integer(MaxQueueSize) orelse MaxQueueSize =:= infinity) andalso
(QueueBehaviour =:= drop orelse QueueBehaviour =:= exit) andalso
is_boolean(IsSSL) ->

eredis_sub_client:start_link(Host, Port, Password, ReconnectSleep,
MaxQueueSize, QueueBehaviour, IsSSL).


%% @doc: Callback for starting from poolboy
Expand All @@ -50,8 +69,9 @@ start_link(Args) ->
ReconnectSleep = proplists:get_value(reconnect_sleep, Args, 100),
MaxQueueSize = proplists:get_value(max_queue_size, Args, infinity),
QueueBehaviour = proplists:get_value(queue_behaviour, Args, drop),
IsSSL = proplists:get_value(is_ssl, Args, false),
start_link(Host, Port, Password, ReconnectSleep,
MaxQueueSize, QueueBehaviour).
MaxQueueSize, QueueBehaviour, IsSSL).

stop(Pid) ->
eredis_sub_client:stop(Pid).
Expand Down Expand Up @@ -183,4 +203,24 @@ ppub_example() ->
eredis:q(P, ["PUBLISH", "foo123", "bar"]),
eredis_client:stop(P).

%% @doc: Example of SSL subscription
ssl_sub_example() ->
{ok, Sub} = start_link("127.0.0.1", 6379, "", 100, infinity, drop, true),
Receiver = spawn_link(fun () ->
controlling_process(Sub),
subscribe(Sub, [<<"foo">>]),
receiver(Sub)
end),
{Sub, Receiver}.

%% @doc: Example of SSL pattern subscription
ssl_psub_example() ->
{ok, Sub} = start_link("127.0.0.1", 6379, "", 100, infinity, drop, true),
Receiver = spawn_link(fun () ->
controlling_process(Sub),
psubscribe(Sub, [<<"foo*">>]),
receiver(Sub)
end),
{Sub, Receiver}.


Loading