mirror of
https://github.com/penpot/penpot.git
synced 2025-01-26 08:29:42 -05:00
📎 Improve db module api.
This commit is contained in:
parent
c55f740978
commit
0ed14f0288
3 changed files with 28 additions and 19 deletions
|
@ -46,29 +46,26 @@
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
(declare instrument-jdbc!)
|
(declare instrument-jdbc!)
|
||||||
|
(declare apply-migrations!)
|
||||||
|
|
||||||
(s/def ::name keyword?)
|
(s/def ::name keyword?)
|
||||||
(s/def ::uri ::us/not-empty-string)
|
(s/def ::uri ::us/not-empty-string)
|
||||||
(s/def ::min-pool-size ::us/integer)
|
(s/def ::min-pool-size ::us/integer)
|
||||||
(s/def ::max-pool-size ::us/integer)
|
(s/def ::max-pool-size ::us/integer)
|
||||||
(s/def ::migrations map?)
|
(s/def ::migrations map?)
|
||||||
|
(s/def ::read-only ::us/boolean)
|
||||||
|
|
||||||
(defmethod ig/pre-init-spec ::pool [_]
|
(defmethod ig/pre-init-spec ::pool [_]
|
||||||
(s/keys :req-un [::uri ::name ::min-pool-size ::max-pool-size ::mtx/metrics]
|
(s/keys :req-un [::uri ::name ::min-pool-size ::max-pool-size]
|
||||||
:opt-un [::migrations]))
|
:opt-un [::migrations ::mtx/metrics ::read-only]))
|
||||||
|
|
||||||
(defmethod ig/init-key ::pool
|
(defmethod ig/init-key ::pool
|
||||||
[_ {:keys [migrations metrics] :as cfg}]
|
[_ {:keys [migrations metrics name] :as cfg}]
|
||||||
(l/info :action "initialize connection pool"
|
(l/info :action "initialize connection pool" :name (d/name name) :uri (:uri cfg))
|
||||||
:name (d/name (:name cfg))
|
(some-> metrics :registry instrument-jdbc!)
|
||||||
:uri (:uri cfg))
|
|
||||||
(instrument-jdbc! (:registry metrics))
|
|
||||||
(let [pool (create-pool cfg)]
|
(let [pool (create-pool cfg)]
|
||||||
(when (seq migrations)
|
(some->> (seq migrations) (apply-migrations! pool))
|
||||||
(with-open [conn ^AutoCloseable (open pool)]
|
|
||||||
(mg/setup! conn)
|
|
||||||
(doseq [[name steps] migrations]
|
|
||||||
(mg/migrate! conn {:name (d/name name) :steps steps}))))
|
|
||||||
pool))
|
pool))
|
||||||
|
|
||||||
(defmethod ig/halt-key! ::pool
|
(defmethod ig/halt-key! ::pool
|
||||||
|
@ -85,6 +82,13 @@
|
||||||
:name "database_query_total"
|
:name "database_query_total"
|
||||||
:help "An absolute counter of database queries."}))
|
:help "An absolute counter of database queries."}))
|
||||||
|
|
||||||
|
(defn- apply-migrations!
|
||||||
|
[pool migrations]
|
||||||
|
(with-open [conn ^AutoCloseable (open pool)]
|
||||||
|
(mg/setup! conn)
|
||||||
|
(doseq [[name steps] migrations]
|
||||||
|
(mg/migrate! conn {:name (d/name name) :steps steps}))))
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;; API & Impl
|
;; API & Impl
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
@ -94,28 +98,34 @@
|
||||||
"SET idle_in_transaction_session_timeout = 200000;"))
|
"SET idle_in_transaction_session_timeout = 200000;"))
|
||||||
|
|
||||||
(defn- create-datasource-config
|
(defn- create-datasource-config
|
||||||
[{:keys [metrics] :as cfg}]
|
[{:keys [metrics read-only] :or {read-only false} :as cfg}]
|
||||||
(let [dburi (:uri cfg)
|
(let [dburi (:uri cfg)
|
||||||
username (:username cfg)
|
username (:username cfg)
|
||||||
password (:password cfg)
|
password (:password cfg)
|
||||||
config (HikariConfig.)
|
config (HikariConfig.)]
|
||||||
mtf (PrometheusMetricsTrackerFactory. (:registry metrics))]
|
|
||||||
(doto config
|
(doto config
|
||||||
(.setJdbcUrl (str "jdbc:" dburi))
|
(.setJdbcUrl (str "jdbc:" dburi))
|
||||||
(.setPoolName (d/name (:name cfg)))
|
(.setPoolName (d/name (:name cfg)))
|
||||||
(.setAutoCommit true)
|
(.setAutoCommit true)
|
||||||
(.setReadOnly false)
|
(.setReadOnly read-only)
|
||||||
(.setConnectionTimeout 10000) ;; 10seg
|
(.setConnectionTimeout 10000) ;; 10seg
|
||||||
(.setValidationTimeout 10000) ;; 10seg
|
(.setValidationTimeout 10000) ;; 10seg
|
||||||
(.setIdleTimeout 120000) ;; 2min
|
(.setIdleTimeout 120000) ;; 2min
|
||||||
(.setMaxLifetime 1800000) ;; 30min
|
(.setMaxLifetime 1800000) ;; 30min
|
||||||
(.setMinimumIdle (:min-pool-size cfg 0))
|
(.setMinimumIdle (:min-pool-size cfg 0))
|
||||||
(.setMaximumPoolSize (:max-pool-size cfg 30))
|
(.setMaximumPoolSize (:max-pool-size cfg 30))
|
||||||
(.setMetricsTrackerFactory mtf)
|
|
||||||
(.setConnectionInitSql initsql)
|
(.setConnectionInitSql initsql)
|
||||||
(.setInitializationFailTimeout -1))
|
(.setInitializationFailTimeout -1))
|
||||||
|
|
||||||
|
;; When metrics namespace is provided
|
||||||
|
(when metrics
|
||||||
|
(->> (:registry metrics)
|
||||||
|
(PrometheusMetricsTrackerFactory.)
|
||||||
|
(.setMetricsTrackerFactory config)))
|
||||||
|
|
||||||
(when username (.setUsername config username))
|
(when username (.setUsername config username))
|
||||||
(when password (.setPassword config password))
|
(when password (.setPassword config password))
|
||||||
|
|
||||||
config))
|
config))
|
||||||
|
|
||||||
(defn pool?
|
(defn pool?
|
||||||
|
|
|
@ -44,8 +44,6 @@
|
||||||
:help "A total number of bytes processed by update-file."
|
:help "A total number of bytes processed by update-file."
|
||||||
:type :counter}}}
|
:type :counter}}}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
:app.migrations/all
|
:app.migrations/all
|
||||||
{:main (ig/ref :app.migrations/migrations)}
|
{:main (ig/ref :app.migrations/migrations)}
|
||||||
|
|
||||||
|
|
|
@ -69,6 +69,7 @@
|
||||||
:mtx-messages mtx-messages
|
:mtx-messages mtx-messages
|
||||||
:mtx-sessions mtx-sessions
|
:mtx-sessions mtx-sessions
|
||||||
)]
|
)]
|
||||||
|
|
||||||
(-> #(handler cfg %)
|
(-> #(handler cfg %)
|
||||||
(wrap-session)
|
(wrap-session)
|
||||||
(wrap-keyword-params)
|
(wrap-keyword-params)
|
||||||
|
|
Loading…
Add table
Reference in a new issue