Provides pooled Riak connections for Elixir. Re-connects automatically upon disconnection.
Can also be used in Erlang. Based on Basho's Riak erlang client
-
To use this in your Elixir project, add it to your dependency list in
mix.exs
defp deps do [ {:riak_pool, github: "HashNuke/riak_pool"} ] end
-
Run
mix deps.get
There are two ways to start RiakPool
-
RiakPool.start_link(address, port)
RiakPool.start_link '127.0.0.1', 8087
-
RiakPool.start_link(address, port, options)
RiakPool.start_link('127.0.0.1', 8087, [ pool_options: [size: 6, max_overflow: 12] ])
Valid options are
pool_options
- options for the pool. Default value forsize
is 5 andmax_overflow
is 10.connection_options
- options for the Riak connectionretry_interval
- incase of disconnection from Riak, this is the number of seconds (interval), at which a re-connection will be tried for. By default the value is60
(seconds).
Here's an example. Notice the init
function.
defmodule YourApp.Supervisor do
use Supervisor.Behaviour
def start_link do
:supervisor.start_link(__MODULE__, [])
end
def init([]) do
children = [
# We are connecting to localhost, on port 8087.
# You can also pass a third argument for size options
worker(RiakPool, ['127.0.0.1', 8087])
]
supervise children, strategy: :one_for_one
end
end
The library provides the following functions.
RiakPool.ping
Used to test if connection to your database is fine. Should return :pong
.
iex(1)> RiakPool.ping
:pong
RiakPool.put(riakc_obj)
Used to create or update values in the database. Accepts a riak object, created with the :riakc_obj
module as the argument. Refer to this blog post on how to use the :riakc_obj
module to encapsulate your data.
RiakPool.get(bucket, key)
Used to get the value stored for a key from in a bucket. It accepts a bucket name and the key.
RiakPool.delete(bucket, key)
Used to delete a key/value from a bucket in the database. It accepts a bucket name and the key to delete.
RiakPool.run(fun)
Pass a function that accepts a worker pid as the argument. It'll run the function for you.
Use this function, to perform other tasks, that this library doesn't provide helper functions for. You can then use the worker pid with the :riakc_pb_socket
module to connect to Riak and do something on your own.
Here's an example that lists keys in the "students" bucket:
RiakPool.run fn(worker)->
:riakc_pb_socket.list_keys worker, "students"
end
Akash Manohar wrote this.
RiakPool is available in the public domain. Or optionally under the MIT License.
If you use it somewhere, send me an email to tell me about it - that'll make me extremely happy.