Locks, backed by Postgres and Component
Lockjaw is a simple Component which uses Postgres' advisory locks which are managed by Postgrs itself and are really lightweight. They are not meant to be used for row level locking, but for implementing concurrency control primitives in applications.
Intended usage is to ensure that at any given time, only one instance of something is doing the work, usually it's for ensuring that only one scheduler at a time is queueing up jobs for periodical processing. See how it integrates with Eternity
Ideally, you do not do much work while holding the lock. E.g. ensuring that your application pushed only 1 job to a queue or call an endpoint once. Relying on performing long running tasks while holding the lock is not advised. Use it as a coordination mechanism, not the business logic.
The usage boils down to:
(def lock-component (lockjaw.core/create {:name "delete-account"}))
(lockjaw.protocol/acquire! lock-component)
- acquire the lock(lockjaw.protocol/release! lock-component)
- release the lock (usually not needed, but just in case)Internally we use a lock ID which is an integer. The lock is established per connection (session), meaning the following is true:
It's important that you use the right connection type, especially when using tools like pgBouncer, as they might mess with the advisory locks and when they are (not) acquired. See here for more details.
On Postgres level lock ids are just integers, and Lockjaw makes it easier to create them - we create an id out of a provided name
configuration option.
We use the CRC algorithm for ensuring that given string always produces same integer. Inspired by Zencoder's Locker library.
While you can set the lock name while creating the component, you might need to dynamically create locks to ensure that only single user account is being processed at a given time. In that case you cannot create a component for each user ID, so you will need to use the following functions to acquire per-ID locks:
(lockjaw.protocol/acquire-by-name! lock (str "user:" (-> user :id)))
to hold a lock for given user id(lockjaw.protocol/acquire-by-name! lock (str "user:" (-> user :id)))
to release itSame caveats apply as to 'default' locks: do not hold them for too long, and use them as a coordination mechanism instead.
(require [lockjaw.core
[com.stuartsierra.component :as component]
[utility-belt.sql.component.db-pool :as db-pool]
lockjaw.protocol :as lock])
(def a-lock
(.start
(component/using
;; unique id, per service, in 99% of the cases service name is ok
(lockjaw.core/create {:name "some-service" })
[:db-conn]))) ;; assumes a hikari-cp pool is here, can be any other JDBC Postgres driver though!
;; explicitly:
(if (lock/acquire! a-lock)
(try
(log/info "doing some work, exclusively")
(do-work)
(finally ;; release when done
(lock/release! a-lock)))
(log/warn "someone else is doing work"))
;;; and with a simple macro:
(lock/with-lock a-lock
(do-some-work))
;; if the lock is NOT acquired, it will return right away with :lockjaw.operation/no-lock keyword
;;; 'dynamic' locking
(lock/with-named-lock! (:a-lock component) "delete-account:1"
(do-delete component {:account-id 1 }))
Lockjaw ships with a mock component, which doesn't depend on Postgrs and will always acquire the lock. It can also be configured to never acquire it:
(let [always-lock (lockjaw.mock/create {:always-acquire true})
never-lock (lockjaw.mock/create {:always-acquire false})]
(.start always-lock)
(.start never-lock)
(lock/acquire! always-lock) ;; => true
(lock/acquire! never-lock) ;; => false
;; you can also pass the name lock name:
(lock/acquire-by-name! always-lock "who?") ;; => true
(.stop always-lock)
(.stop never-lock))
Just run:
lein test
Ensure no other database connections are currently holding advisory locks when running tests.
next.jdbc
, allows passing lock-name when asking for lock.next.jdbc
In alphabetical order
Can you improve this documentation? These fine people already did:
Łukasz Korecki & AfonsoTsukamotoEdit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close