RethinkDB.Connection

A module for managing connections.

A Connection object is a process that can be started in various ways.

It is recommended to start it as part of a supervision tree with a name:

worker(RethinkDB.Connection, [[port: 28015, host: 'localhost', name: :rethinkdb_connection]])

Connections will by default connect asynchronously. If a connection fails, we retry with an exponential backoff. All queries will return %RethinkDB.Exception.ConnectionClosed{} until the connection is established.

If :sync_connect is set to true then the process will crash if we fail to connect. It’s recommended to only use this if the database is on the same host or if a rethinkdb proxy is running on the same host. If there’s any chance of a network partition, it’s recommended to stick with the default behavior.

Summary

Functions

Closes a feed

Fetch the next dataset for a feed

noreply_wait ensures that previous queries with the noreply flag have been processed by the server. Note that this guarantee only applies to queries run on the given connection

Run a query on a connection

Start connection as a linked process

Stop the connection

Macros

A convenience macro for naming connections

Functions

close(map)

Closes a feed.

Since a feed is tied to a particular connection, no connection is needed when calling close.

next(map)

Fetch the next dataset for a feed.

Since a feed is tied to a particular connection, no connection is needed when calling next.

noreply_wait(conn, timeout \\ 5000)

noreply_wait ensures that previous queries with the noreply flag have been processed by the server. Note that this guarantee only applies to queries run on the given connection.

run(query, conn, opts \\ [])

Run a query on a connection.

Supports the following options:

  • timeout - How long to wait for a response
  • db - Default database to use for query. Can also be specified as part of the query.
start_link(opts \\ [])

Start connection as a linked process

Accepts a Dict of options. Supported options:

  • :host - hostname to use to connect to database. Defaults to 'localhost'.
  • :port - port on which to connect to database. Defaults to 28015.
  • :auth_key - authorization key to use with database. Defaults to nil.
  • :db - default database to use with queries. Defaults to nil.
  • :sync_connect - whether to have init block until a connection succeeds. Defaults to false.
  • :max_pending - Hard cap on number of concurrent requests. Defaults to 10000
  • :ssl - a dict of options. Support SSL options:

    • :ca_certs - a list of file paths to cacerts.
stop(pid)

Stop the connection.

Stops the given connection.

Macros

__using__(opts)

A convenience macro for naming connections.

For convenience we provide the use RethinkDB.Connection macro, which automatically registers itself under the module name:

defmodule FooDatabase, do: use RethinkDB.Connection

Then in the supervision tree:

worker(FooDatabase, [[port: 28015, host: 'localhost']])

When use RethinkDB.Connection is called, it will define:

  • start_link
  • stop
  • run

All of these only differ from the normal RethinkDB.Connection functions in that they don’t accept a connection. They will use the current module as the process name. start_link will start the connection under the module name.

If you attempt to provide a name to start_link, it will raise an ArgumentError.