1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
open Lwt.Syntax
open Resource_pooling
let section = Logs.Src.create "os:db"
let ( >>= ) = Lwt.bind
module Lwt_thread = struct
include Lwt
let close_in = Lwt_io.close
let really_input = Lwt_io.read_into_exactly
let input_binary_int = Lwt_io.BE.read_int
let input_char = Lwt_io.read_char
let output_string = Lwt_io.write
let output_binary_int = Lwt_io.BE.write_int
let output_char = Lwt_io.write_char
let flush = Lwt_io.flush
let open_connection x = Lwt_io.open_connection x
type out_channel = Lwt_io.output_channel
type in_channel = Lwt_io.input_channel
end
module Lwt_PGOCaml = PGOCaml_generic.Make (Lwt_thread)
module PGOCaml = Lwt_PGOCaml
let host_r = ref None
let port_r = ref None
let user_r = ref None
let password_r = ref None
let database_r = ref None
let unix_domain_socket_dir_r = ref None
let init_r = ref None
let dispose db =
Lwt.catch (fun () -> PGOCaml.close db) (fun _ -> Lwt.return_unit)
let connect () =
let* h =
Lwt_PGOCaml.connect ?host:!host_r ?port:!port_r ?user:!user_r
?password:!password_r ?database:!database_r
?unix_domain_socket_dir:!unix_domain_socket_dir_r ()
in
match !init_r with
| Some init ->
let* () =
Lwt.catch
(fun () -> init h)
(fun exn ->
let* () = dispose h in
Lwt.fail exn
)
in
Lwt.return h
| None -> Lwt.return h
let validate db =
Lwt.catch
(fun () ->
let* () = Lwt_PGOCaml.ping db in
Lwt.return_true
)
(fun _ -> Lwt.return_false)
let pool : (string, bool) Hashtbl.t Lwt_PGOCaml.t Resource_pool.t ref =
ref @@ Resource_pool.create 16 ~validate ~dispose connect
let set_pool_size n = pool := Resource_pool.create n ~validate ~dispose connect
let init
?host
?port
?user
?password
?database
?unix_domain_socket_dir
?pool_size
?init
() =
host_r := host;
port_r := port;
user_r := user;
password_r := password;
database_r := database;
unix_domain_socket_dir_r := unix_domain_socket_dir;
init_r := init;
match pool_size with None -> () | Some n -> set_pool_size n
let connection_pool () = !pool
type wrapper =
{f : 'a. PGOCaml.pa_pg_data PGOCaml.t -> (unit -> 'a Lwt.t) -> 'a Lwt.t}
let connection_wrapper = ref {f = (fun _ f -> f ())}
let set_connection_wrapper f = connection_wrapper := f
let use_pool f =
Resource_pool.use !pool @@ fun db ->
!connection_wrapper.f db @@ fun () ->
Lwt.catch
(fun () -> f db)
(function
| Lwt_PGOCaml.Error msg as e ->
Logs.err ~src:section (fun fmt ->
fmt "postgresql protocol error: %s" msg
);
let* () = Lwt_PGOCaml.close db in
Lwt.fail e
| (Unix.Unix_error _ | End_of_file) as e ->
Logs.err ~src:section (fun fmt ->
fmt ("unix error" ^^ "@\n%s") (Printexc.to_string e)
);
let* () = Lwt_PGOCaml.close db in
Lwt.fail e
| Lwt.Canceled as e ->
Logs.err ~src:section (fun fmt -> fmt "thread canceled");
let* () = PGOCaml.close db in
Lwt.fail e
| exc -> Lwt.reraise exc
)
let transaction_block db f =
Lwt.catch
(fun () ->
Lwt_PGOCaml.begin_work db >>= fun _ ->
let* r = f () in
let* () = Lwt_PGOCaml.commit db in
Lwt.return r
)
(function
| (Lwt_PGOCaml.Error _ | Lwt.Canceled | Unix.Unix_error _ | End_of_file)
as e ->
Lwt.fail e
| e ->
let* () =
Lwt.catch
(fun () -> Lwt_PGOCaml.rollback db)
(function
| Lwt_PGOCaml.PostgreSQL_Error _ ->
Logs.err ~src:section (fun fmt -> fmt "rollback failed");
Lwt_PGOCaml.close db
| exc -> Lwt.reraise exc
)
in
Lwt.fail e
)
let full_transaction_block f =
use_pool (fun db -> transaction_block db (fun () -> f db))
let without_transaction = use_pool