Skip to content

Commit

Permalink
make formatter happy
Browse files Browse the repository at this point in the history
  • Loading branch information
mabiede committed Oct 18, 2023
1 parent 95e0e71 commit 67a7ec7
Show file tree
Hide file tree
Showing 11 changed files with 832 additions and 815 deletions.
69 changes: 34 additions & 35 deletions backend/database_pools.ml
Original file line number Diff line number Diff line change
Expand Up @@ -53,33 +53,32 @@ end

module Make (Config : ConfigSig) = struct
let main_pool_ref
: (Caqti_lwt.connection, Caqti_error.t) Caqti_lwt.Pool.t option ref
: (Caqti_lwt.connection, Caqti_error.t) Caqti_lwt.Pool.t option ref
=
ref None
;;

let pools
: ( string, (Caqti_lwt.connection, Caqti_error.t) Caqti_lwt.Pool.t )
Hashtbl.t
: (string, (Caqti_lwt.connection, Caqti_error.t) Caqti_lwt.Pool.t) Hashtbl.t
=
let spare_for_pools = 5 in
Hashtbl.create
(match Config.database with
| SinglePool _ -> 1
| MultiPools pools -> CCList.length pools + spare_for_pools)
| SinglePool _ -> 1
| MultiPools pools -> CCList.length pools + spare_for_pools)
;;

let print_pool_usage ?tags pool =
let n_connections = Caqti_lwt.Pool.size pool in
let max_connections = Config.database_pool_size in
Logs.debug ~src (fun m ->
m ?tags "Pool usage: %i/%i" n_connections max_connections)
m ?tags "Pool usage: %i/%i" n_connections max_connections)
;;

let connect_or_failwith
?(pool_size = Config.database_pool_size)
ok_fun
database_url
?(pool_size = Config.database_pool_size)
ok_fun
database_url
=
database_url
|> Uri.of_string
Expand Down Expand Up @@ -128,15 +127,15 @@ module Make (Config : ConfigSig) = struct
| SinglePool database_url when CCOption.is_none !main_pool_ref ->
database_url
|> connect_or_failwith (fun pool ->
main_pool_ref := Some pool;
())
main_pool_ref := Some pool;
())
| SinglePool _ -> ()
| MultiPools pools' ->
pools'
|> CCList.filter (fun (name, _) ->
CCOption.is_none (Hashtbl.find_opt pools name))
CCOption.is_none (Hashtbl.find_opt pools name))
|> CCList.iter (fun (name, url) ->
url |> connect_or_failwith (Hashtbl.add pools name))
url |> connect_or_failwith (Hashtbl.add pools name))
;;

let fetch_pool ?(ctx = []) () =
Expand All @@ -149,8 +148,8 @@ module Make (Config : ConfigSig) = struct
find_pool_name ctx
>>= Hashtbl.find_opt pools
|> (function
| Some pool -> pool
| None -> failwith "Unknown Pool: Please 'add_pool' first!")
| Some pool -> pool
| None -> failwith "Unknown Pool: Please 'add_pool' first!")
;;

let transaction ?ctx f =
Expand All @@ -160,28 +159,28 @@ module Make (Config : ConfigSig) = struct
Caqti_lwt.Pool.use
(fun connection ->
Logs.debug ~src (fun m ->
m ?tags:(LogTag.ctx_opt ?ctx ()) "Fetched connection from pool");
m ?tags:(LogTag.ctx_opt ?ctx ()) "Fetched connection from pool");
let (module Connection : Caqti_lwt.CONNECTION) = connection in
let open Caqti_error in
match%lwt Connection.start () with
| Error msg ->
Logs.debug ~src (fun m ->
m
?tags:(LogTag.ctx_opt ?ctx ())
"Failed to start transaction: %s"
(show msg));
m
?tags:(LogTag.ctx_opt ?ctx ())
"Failed to start transaction: %s"
(show msg));
Lwt.return_error msg
| Ok () ->
Logs.debug ~src (fun m ->
m ?tags:(LogTag.ctx_opt ?ctx ()) "Started transaction");
m ?tags:(LogTag.ctx_opt ?ctx ()) "Started transaction");
Lwt.catch
(fun () ->
match%lwt Connection.commit () with
| Ok () ->
Logs.debug ~src (fun m ->
m
?tags:(LogTag.ctx_opt ?ctx ())
"Successfully committed transaction");
m
?tags:(LogTag.ctx_opt ?ctx ())
"Successfully committed transaction");
f connection |> Lwt_result.return
| Error error ->
Exception
Expand All @@ -194,9 +193,9 @@ module Make (Config : ConfigSig) = struct
match%lwt Connection.rollback () with
| Ok () ->
Logs.debug ~src (fun m ->
m
?tags:(LogTag.ctx_opt ?ctx ())
"Successfully rolled back transaction");
m
?tags:(LogTag.ctx_opt ?ctx ())
"Successfully rolled back transaction");
Lwt.fail e
| Error error ->
Exception
Expand All @@ -223,25 +222,25 @@ module Make (Config : ConfigSig) = struct

let find_opt ?ctx request input =
query' ?ctx (fun connection ->
let module Connection = (val connection : Caqti_lwt.CONNECTION) in
Connection.find_opt request input)
let module Connection = (val connection : Caqti_lwt.CONNECTION) in
Connection.find_opt request input)
;;

let find ?ctx request input =
query' ?ctx (fun connection ->
let module Connection = (val connection : Caqti_lwt.CONNECTION) in
Connection.find request input)
let module Connection = (val connection : Caqti_lwt.CONNECTION) in
Connection.find request input)
;;

let collect ?ctx request input =
query' ?ctx (fun connection ->
let module Connection = (val connection : Caqti_lwt.CONNECTION) in
Connection.collect_list request input)
let module Connection = (val connection : Caqti_lwt.CONNECTION) in
Connection.collect_list request input)
;;

let exec ?ctx request input =
query' ?ctx (fun connection ->
let module Connection = (val connection : Caqti_lwt.CONNECTION) in
Connection.exec request input)
let module Connection = (val connection : Caqti_lwt.CONNECTION) in
Connection.exec request input)
;;
end
Loading

0 comments on commit 67a7ec7

Please sign in to comment.