Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Os_db.init: no pool parameter means no pool at all #478

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/os_db.ml
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ let validate db =
with _ ->
Lwt.return_false

let pool : (string, bool) Hashtbl.t Lwt_PGOCaml.t Lwt_pool.t ref =
ref @@ Lwt_pool.create 16 ~validate connect
let pool : (string, bool) Hashtbl.t Lwt_PGOCaml.t Lwt_pool.t option ref =
ref None

let set_pool_size n = pool := Lwt_pool.create n ~validate connect
let set_pool_size n = pool := Some (Lwt_pool.create n ~validate connect)

let init ?host ?port ?user ?password ?database
?unix_domain_socket_dir ?pool_size () =
Expand All @@ -83,6 +83,15 @@ let init ?host ?port ?user ?password ?database
| None -> ()
| Some n -> set_pool_size n

let use f =
match !pool with
| None ->
lwt c = connect () in
lwt r = f c in
lwt () = Lwt_PGOCaml.close c in
Lwt.return r
| Some pool -> Lwt_pool.use pool f

let transaction_block db f =
Lwt_PGOCaml.begin_work db >>= fun _ ->
try_lwt
Expand All @@ -94,9 +103,9 @@ let transaction_block db f =
Lwt.fail e

let full_transaction_block f =
Lwt_pool.use !pool (fun db -> transaction_block db (fun () -> f db))
use (fun db -> transaction_block db (fun () -> f db))

let without_transaction f = Lwt_pool.use !pool (fun db -> f db)
let without_transaction f = use f

let view_one rq =
try List.hd rq
Expand Down
1 change: 1 addition & 0 deletions src/os_db.mli
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ exception Account_not_activated

(** [init ?host ?port ?user ?password ?database ?unix_domain_socket_dir ()]
initializes the variables for the database access.
No pool size means no pool at all.
*)
val init :
?host:string ->
Expand Down