mirror of
https://github.com/penpot/penpot.git
synced 2025-02-18 21:06:11 -05:00
✨ Make the pool initialization process and defaults reusable
And add the ability to skip pool initialization if no enough data is provided. Mainly for initialize pools based on configuration for not essential/dynamic services.
This commit is contained in:
parent
199360efa6
commit
dce479bc4b
1 changed files with 42 additions and 30 deletions
|
@ -55,54 +55,66 @@
|
||||||
(s/def ::migrations map?)
|
(s/def ::migrations map?)
|
||||||
(s/def ::name keyword?)
|
(s/def ::name keyword?)
|
||||||
(s/def ::password ::us/string)
|
(s/def ::password ::us/string)
|
||||||
(s/def ::read-only ::us/boolean)
|
|
||||||
(s/def ::uri ::us/not-empty-string)
|
(s/def ::uri ::us/not-empty-string)
|
||||||
(s/def ::username ::us/string)
|
(s/def ::username ::us/string)
|
||||||
(s/def ::validation-timeout ::us/integer)
|
(s/def ::validation-timeout ::us/integer)
|
||||||
|
(s/def ::read-only? ::us/boolean)
|
||||||
|
|
||||||
(defmethod ig/pre-init-spec ::pool [_]
|
(s/def ::pool-options
|
||||||
(s/keys :req-un [::uri ::name
|
(s/keys :opt-un [::uri ::name
|
||||||
::min-size
|
::min-size
|
||||||
::max-size
|
::max-size
|
||||||
::connection-timeout
|
::connection-timeout
|
||||||
::validation-timeout]
|
::validation-timeout
|
||||||
:opt-un [::migrations
|
::migrations
|
||||||
::username
|
::username
|
||||||
::password
|
::password
|
||||||
::mtx/metrics
|
::mtx/metrics
|
||||||
::read-only]))
|
::read-only?]))
|
||||||
|
|
||||||
(defmethod ig/prep-key ::pool
|
(def defaults
|
||||||
[_ cfg]
|
{:name :main
|
||||||
(merge {:name :main
|
|
||||||
:min-size 0
|
:min-size 0
|
||||||
:max-size 30
|
:max-size 30
|
||||||
:connection-timeout 10000
|
:connection-timeout 10000
|
||||||
:validation-timeout 10000
|
:validation-timeout 10000
|
||||||
:idle-timeout 120000 ; 2min
|
:idle-timeout 120000 ; 2min
|
||||||
:max-lifetime 1800000 ; 30m
|
:max-lifetime 1800000 ; 30m
|
||||||
:read-only false}
|
:read-only? false})
|
||||||
(d/without-nils cfg)))
|
|
||||||
|
(defmethod ig/prep-key ::pool
|
||||||
|
[_ cfg]
|
||||||
|
(merge defaults (d/without-nils cfg)))
|
||||||
|
|
||||||
|
;; Don't validate here, just validate that a map is received.
|
||||||
|
(defmethod ig/pre-init-spec ::pool [_] ::pool-options)
|
||||||
|
|
||||||
(defmethod ig/init-key ::pool
|
(defmethod ig/init-key ::pool
|
||||||
[_ {:keys [migrations name read-only] :as cfg}]
|
[_ {:keys [migrations read-only? uri] :as cfg}]
|
||||||
|
(if uri
|
||||||
|
(let [pool (create-pool cfg)]
|
||||||
(l/info :hint "initialize connection pool"
|
(l/info :hint "initialize connection pool"
|
||||||
:name (d/name name)
|
:name (d/name (:name cfg))
|
||||||
:uri (:uri cfg)
|
:uri uri
|
||||||
:read-only read-only
|
:read-only read-only?
|
||||||
:with-credentials (and (contains? cfg :username)
|
:with-credentials (and (contains? cfg :username)
|
||||||
(contains? cfg :password))
|
(contains? cfg :password))
|
||||||
:min-size (:min-size cfg)
|
:min-size (:min-size cfg)
|
||||||
:max-size (:max-size cfg))
|
:max-size (:max-size cfg))
|
||||||
|
(when-not read-only?
|
||||||
(let [pool (create-pool cfg)]
|
|
||||||
(when-not read-only
|
|
||||||
(some->> (seq migrations) (apply-migrations! pool)))
|
(some->> (seq migrations) (apply-migrations! pool)))
|
||||||
pool))
|
pool)
|
||||||
|
|
||||||
|
(do
|
||||||
|
(l/warn :hint "unable to initialize pool, missing url"
|
||||||
|
:name (d/name (:name cfg))
|
||||||
|
:read-only read-only?)
|
||||||
|
nil)))
|
||||||
|
|
||||||
(defmethod ig/halt-key! ::pool
|
(defmethod ig/halt-key! ::pool
|
||||||
[_ pool]
|
[_ pool]
|
||||||
(.close ^HikariDataSource pool))
|
(when pool
|
||||||
|
(.close ^HikariDataSource pool)))
|
||||||
|
|
||||||
(defn- apply-migrations!
|
(defn- apply-migrations!
|
||||||
[pool migrations]
|
[pool migrations]
|
||||||
|
@ -126,7 +138,7 @@
|
||||||
(.setJdbcUrl (str "jdbc:" uri))
|
(.setJdbcUrl (str "jdbc:" uri))
|
||||||
(.setPoolName (d/name (:name cfg)))
|
(.setPoolName (d/name (:name cfg)))
|
||||||
(.setAutoCommit true)
|
(.setAutoCommit true)
|
||||||
(.setReadOnly (:read-only cfg))
|
(.setReadOnly (:read-only? cfg))
|
||||||
(.setConnectionTimeout (:connection-timeout cfg))
|
(.setConnectionTimeout (:connection-timeout cfg))
|
||||||
(.setValidationTimeout (:validation-timeout cfg))
|
(.setValidationTimeout (:validation-timeout cfg))
|
||||||
(.setIdleTimeout (:idle-timeout cfg))
|
(.setIdleTimeout (:idle-timeout cfg))
|
||||||
|
|
Loading…
Add table
Reference in a new issue