Packages
eventstore
1.4.0
1.4.8
1.4.7
1.4.6
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.2
1.3.1
1.3.0
1.2.3
1.2.2
1.2.1
1.2.0
1.1.0
1.0.3
1.0.2
1.0.1
1.0.0
1.0.0-rc.0
0.17.0
0.16.2
0.16.1
0.16.0
0.15.1
0.15.0
0.14.0
0.14.0-rc.0
0.13.2
0.13.1
0.13.0
0.12.1
0.12.0
0.11.0
0.11.0-rc.0
0.10.1
0.10.0
0.9.0
0.8.1
0.8.0
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.2
0.6.1
0.6.0
0.5.2
0.5.1
0.5.0
0.4.3
0.4.2
0.4.1
0.4.0
0.3.0
0.2.1
0.2.0
0.1.0
0.0.5
0.0.4
0.0.3
0.0.2
0.0.1
Event store using PostgreSQL for persistence.
Current section
Files
Jump to
Current section
Files
lib/event_store/storage/lock.ex
defmodule EventStore.Storage.Lock do
@moduledoc false
require Logger
alias EventStore.Sql.Statements
@doc """
Use two 32-bit key values for advisory locks where the first key acts as the
namespace.
The namespace key is derived from the unique `oid` value for the `subscriptions`
table. The `oid` is unique within a database and differs for identically named
tables defined in different schemas and on repeat table definitions.
This change aims to prevent lock collision with application level
advisory lock usage and other libraries using Postgres advisory locks. Now
there is a 1 in 2,147,483,647 chance of colliding with other locks.
"""
def try_acquire_exclusive_lock(conn, key, opts) do
{schema, opts} = Keyword.pop(opts, :schema)
query = Statements.try_advisory_lock(schema)
case Postgrex.query(conn, query, [key], opts) do
{:ok, %Postgrex.Result{rows: [[true]]}} -> :ok
{:ok, %Postgrex.Result{rows: [[false]]}} -> {:error, :lock_already_taken}
{:error, _error} = reply -> reply
end
end
def unlock(conn, key, opts) do
{schema, opts} = Keyword.pop(opts, :schema)
query = Statements.advisory_unlock(schema)
case Postgrex.query(conn, query, [key], opts) do
{:ok, %Postgrex.Result{rows: [[true]]}} -> :ok
{:ok, %Postgrex.Result{rows: [[false]]}} -> :ok
{:error, _error} = reply -> reply
end
end
end