package sihl

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file service.ml

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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
open Lwt.Syntax
open Model

module Default : Sig.SERVICE = struct
  let print_pool_usage pool =
    let n_connections = Caqti_lwt.Pool.size pool in
    let max_connections =
      Option.value (Core.Configuration.read_int "DATABASE_POOL_SIZE") ~default:10
    in
    Logs.debug (fun m -> m "DB: Pool usage: %i/%i" n_connections max_connections)
  ;;

  let create_pool () =
    match !pool_ref with
    | Some pool ->
      Logs.debug (fun m -> m "DB: Skipping pool creation, re-using existing pool");
      pool
    | None ->
      let pool_size =
        Option.value (Core.Configuration.read_int "DATABASE_POOL_SIZE") ~default:10
      in
      Logs.debug (fun m -> m "DB: Create pool with size %i" pool_size);
      Option.get ("DATABASE_URL" |> Core.Configuration.read_string)
      |> Uri.of_string
      |> Caqti_lwt.connect_pool ~max_size:pool_size
      |> (function
      | Ok pool ->
        pool_ref := Some pool;
        pool
      | Error err ->
        let msg = "DB: Failed to connect to DB pool" in
        Logs.err (fun m -> m "%s %s" msg (Caqti_error.show err));
        raise (Exception ("DB: Failed to create pool " ^ msg)))
  ;;

  let ctx_with_pool () =
    let pool = create_pool () in
    Core.Ctx.(empty |> add_pool pool)
  ;;

  let add_pool ctx =
    let pool = create_pool () in
    add_pool pool ctx
  ;;

  let query ctx f =
    match find_transaction ctx, find_connection ctx, find_pool ctx with
    | Some connection, None, None ->
      let* result = f connection in
      (match result with
      | Ok result -> Lwt.return result
      | Error error ->
        let msg = Caqti_error.show error in
        Logs.err (fun m -> m "DB: %s" msg);
        Lwt.fail (Exception msg))
    | None, Some connection, _ ->
      let* result = f connection in
      (match result with
      | Ok result -> Lwt.return result
      | Error error ->
        let msg = Caqti_error.show error in
        Logs.err (fun m -> m "DB: %s" msg);
        Lwt.fail (Exception msg))
    | None, None, Some pool ->
      print_pool_usage pool;
      let* result = Caqti_lwt.Pool.use f pool in
      (match result with
      | Ok result -> Lwt.return result
      | Error error ->
        let msg = Caqti_error.show error in
        Logs.err (fun m -> m "DB: %s" msg);
        Lwt.fail (Exception msg))
    | Some _, Some _, Some _ ->
      Logs.err (fun m ->
          m
            "DB: Connection AND transaction AND pool found in context, this should never \
             happen and might indicate connection leaks. Please report this issue.");
      Lwt.fail (Exception "Connection and pool found")
    | _ ->
      Logs.err (fun m -> m "DB: No connection pool found");
      Logs.info (fun m -> m "DB: Have you applied the DB middleware?");
      Lwt.fail (Exception "No connection pool found")
  ;;

  let with_connection ctx f =
    match find_transaction ctx, find_connection ctx, find_pool ctx with
    | Some _, None, None -> ctx |> remove_pool |> f
    | None, Some _, None -> ctx |> remove_pool |> f
    | None, None, Some pool ->
      print_pool_usage pool;
      let* pool_result =
        Caqti_lwt.Pool.use
          (fun connection ->
            Logs.debug (fun m -> m "DB TX: Fetched connection from pool");
            let (module Connection : Caqti_lwt.CONNECTION) = connection in
            let ctx_with_connection =
              ctx |> remove_pool |> add_connection (module Connection)
            in
            Lwt.catch
              (fun () ->
                let* result = f ctx_with_connection in
                Lwt.return @@ Ok result)
              (fun e -> Lwt.fail e))
          pool
      in
      (match pool_result with
      | Ok result ->
        (* All good, return result of f ctx *)
        Lwt.return result
      | Error pool_err ->
        (* Failed to start, commit or rollback transaction *)
        Lwt.fail (Exception (pool_err |> Caqti_error.show)))
    | Some _, Some _, Some _ ->
      Logs.err (fun m ->
          m
            "DB: Connection AND transaction AND pool found in context, this should never \
             happen and might indicate connection leaks. Please report this issue.");
      Lwt.fail (Exception "Connection and pool found")
    | _ ->
      Logs.err (fun m -> m "No connection pool found");
      Logs.info (fun m -> m "Have you applied the DB middleware?");
      Lwt.fail (Exception "No connection pool found")
  ;;

  let atomic ctx f =
    match find_transaction ctx, find_connection ctx, find_pool ctx with
    | Some connection, None, None ->
      (* Make sure [f] can not use the pool or some other connection *)
      ctx |> remove_pool |> remove_connection |> add_transaction connection |> f
    | None, Some connection, None ->
      (* TODO start transaction and store current connection as transaction in trx *)
      let (module Connection : Caqti_lwt.CONNECTION) = connection in
      let* start_result = Connection.start () in
      (match start_result with
      | Error msg ->
        Logs.debug (fun m ->
            m "DB TX: Failed to start transaction %s" (Caqti_error.show msg));
        Lwt.fail @@ Exception (Caqti_error.show msg)
      | Ok () ->
        Logs.debug (fun m -> m "DB TX: Started transaction");
        (* Remove the pool so that all subsequent queries are executed on the connection.
           A transaction can only be done only at one connection, it can not span multiple
           connections. *)
        let ctx_with_connection =
          ctx |> remove_pool |> remove_connection |> add_transaction (module Connection)
        in
        Lwt.catch
          (fun () ->
            let* result = f ctx_with_connection in
            let* commit_result = Connection.commit () in
            match commit_result with
            | Ok () ->
              Logs.debug (fun m -> m "DB TX: Successfully committed transaction");
              Lwt.return @@ result
            | Error error ->
              Logs.err (fun m ->
                  m "DB TX: Failed to commit transaction %s" (Caqti_error.show error));
              Lwt.fail @@ Exception "Failed to commit transaction")
          (fun e ->
            let* rollback_result = Connection.rollback () in
            match rollback_result with
            | Ok () ->
              Logs.debug (fun m -> m "DB TX: Successfully rolled back transaction");
              Lwt.fail e
            | Error error ->
              Logs.err (fun m ->
                  m "DB TX: Failed to rollback transaction %s" (Caqti_error.show error));
              Lwt.fail @@ Exception "Failed to rollback transaction"))
    | None, None, Some pool ->
      (* There is no transaction active, create a new one *)
      print_pool_usage pool;
      let* pool_result =
        Caqti_lwt.Pool.use
          (fun connection ->
            Logs.debug (fun m -> m "DB TX: Fetched connection from pool");
            let (module Connection : Caqti_lwt.CONNECTION) = connection in
            let* start_result = Connection.start () in
            match start_result with
            | Error msg ->
              Logs.debug (fun m ->
                  m "DB TX: Failed to start transaction %s" (Caqti_error.show msg));
              Lwt.return @@ Error msg
            | Ok () ->
              Logs.debug (fun m -> m "DB TX: Started transaction");
              (* Remove the pool so that all subsequent queries are executed on the
                 connection. A transaction can only be done only at one connection, it can
                 not span multiple connections. *)
              let ctx_with_connection =
                ctx
                |> remove_pool
                |> remove_connection
                |> add_transaction (module Connection)
              in
              Lwt.catch
                (fun () ->
                  let* result = f ctx_with_connection in
                  let* commit_result = Connection.commit () in
                  match commit_result with
                  | Ok () ->
                    Logs.debug (fun m -> m "DB TX: Successfully committed transaction");
                    Lwt.return @@ Ok result
                  | Error error ->
                    Logs.err (fun m ->
                        m
                          "DB TX: Failed to commit transaction %s"
                          (Caqti_error.show error));
                    Lwt.fail @@ Exception "Failed to commit transaction")
                (fun e ->
                  let* rollback_result = Connection.rollback () in
                  match rollback_result with
                  | Ok () ->
                    Logs.debug (fun m -> m "DB TX: Successfully rolled back transaction");
                    Lwt.fail e
                  | Error error ->
                    Logs.err (fun m ->
                        m
                          "DB TX: Failed to rollback transaction %s"
                          (Caqti_error.show error));
                    Lwt.fail @@ Exception "Failed to rollback transaction"))
          pool
      in
      (match pool_result with
      | Ok result ->
        (* All good, return result of f ctx *)
        Lwt.return result
      | Error pool_err ->
        (* Failed to start, commit or rollback transaction *)
        Lwt.fail (Exception (pool_err |> Caqti_error.show)))
    | Some _, Some _, Some _ ->
      Logs.err (fun m ->
          m
            "DB: Connection AND transaction AND pool found in context, this should never \
             happen and might indicate connection leaks. Please report this issue.");
      Lwt.fail (Exception "Connection and pool found")
    | _ ->
      Logs.err (fun m -> m "No connection pool found");
      Logs.info (fun m -> m "Have you applied the DB middleware?");
      Lwt.fail (Exception "No connection pool found")
  ;;

  let set_fk_check_request =
    Caqti_request.exec Caqti_type.bool "SET FOREIGN_KEY_CHECKS = ?;"
  ;;

  let set_fk_check ctx ~check =
    query ctx (fun (module Connection : Caqti_lwt.CONNECTION) ->
        Connection.exec set_fk_check_request check)
  ;;

  let with_disabled_fk_check ctx f =
    with_connection ctx (fun ctx ->
        let* () = set_fk_check ctx ~check:false in
        Lwt.finalize (fun () -> f ctx) (fun () -> set_fk_check ctx ~check:true))
  ;;

  let start ctx = ctx |> add_pool |> Lwt.return
  let stop _ = Lwt.return ()
  let lifecycle = Core.Container.Lifecycle.create "db" ~start ~stop

  let configure configuration =
    let configuration = Core.Configuration.make configuration in
    Core.Container.Service.create ~configuration lifecycle
  ;;
end
OCaml

Innovation. Community. Security.