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

Document :pool_index #303

Merged
merged 2 commits into from
Dec 2, 2023
Merged
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
27 changes: 24 additions & 3 deletions lib/db_connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,9 @@ defmodule DBConnection do
random exponential (default: `:rand_exp`)
* `:configure` - A function to run before every connect attempt to
dynamically configure the options, either a 1-arity fun,
`{module, function, args}` with options prepended to `args` or `nil` where
only returned options are passed to connect callback (default: `nil`). This
function is called *in the connection process*.
`{module, function, args}` or `nil`. This function is called
*in the connection process*. For more details, see
[Connection Configuration Callback](#start_link/2-connection-configuration-callback)
* `:after_connect` - A function to run on connect using `run/3`, either
a 1-arity fun, `{module, function, args}` with `t:DBConnection.t/0` prepended
to `args` or `nil` (default: `nil`)
Expand Down Expand Up @@ -524,6 +524,27 @@ defmodule DBConnection do
This feature is available since v2.6.0. Before this version `:connection_listeners` only
accepted a list of listener processes.

## Connection Configuration Callback

The `:configure` function will be called before each individual connection to the
database is made. It receives all of the options provided to `start_link/2` as well
as an additional generated value named `:pool_index`. The returned value will be
passed as the options into the appropriate `:connect` callback. This provides a way
for the user to dynamically configure the connection options.

`:pool_index` is an integer in `1..pool_size` that represents the current connection's
place in the enumeration of all of the pool's connections. It can be used, for example,
to configure a unique database per connection when asynchronous tests cannot be performed
on a single database.

The allowed callbacks are:

* A 1-arity function that recieves the options from `start_link/2` as well as
`:pool_index`
* `{module, function, args}` where the options from `start_link/2` as well as
`:pool_index` are prepended to `args` before the function is called
* `nil` if you do not want to modify the existing options

## Telemetry

A `[:db_connection, :connection_error]` event is published whenever a
Expand Down
Loading