package caqti

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

Source file caqti_pool_config.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
(* Copyright (C) 2023  Petter A. Urkedal <paurkedal@gmail.com>
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version, with the LGPL-3.0 Linking Exception.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * and the LGPL-3.0 Linking Exception along with this library.  If not, see
 * <http://www.gnu.org/licenses/> and <https://spdx.org>, respectively.
 *)

module Log = (val Logs.src_log (Logs.Src.create "caqti.config"))

type t = {
  max_size: int option;
  max_idle_size: int option;
  max_idle_age: Mtime.Span.t option option;
  max_use_count: int option option;
}

type _ key =
  | Max_size : int key
  | Max_idle_size : int key
  | Max_idle_age : Mtime.Span.t option key
  | Max_use_count : int option key

type any_key = Any : _ key -> any_key

let keys = [
  Any Max_size;
  Any Max_idle_size;
  Any Max_idle_age;
  Any Max_use_count;
]

let create
      ?max_size
      ?max_idle_size
      ?max_idle_age
      ?max_use_count
      () =
  {max_size; max_idle_size; max_idle_age; max_use_count}

let option_of_string f = function
 | "" | "none" -> None
 | s -> Some (f s)

let mtime_span_of_string s =
  let x = float_of_string s in
  (match Mtime.Span.of_float_ns (x *. 1e9) with
   | None -> failwith "Mtime.Span.of_float_ns"
   | Some x -> x)

let default = create ()

let create_from_env pfx =
  let get conv sfx =
    let var = pfx ^ sfx in
    (match Sys.getenv_opt var with
     | None -> None
     | Some str ->
        (match conv str with
         | value -> Some value
         | exception Failure _ ->
            Log.err (fun m -> m "Failed to parse $%s = %s." var str);
            None))
  in
  {
    max_size = get int_of_string "_MAX_SIZE";
    max_idle_size = get int_of_string "_MAX_IDLE_SIZE";
    max_idle_age = get (option_of_string mtime_span_of_string) "_MAX_IDLE_AGE";
    max_use_count = get (option_of_string int_of_string) "_MAX_USE_COUNT";
  }

let default_from_env () = create_from_env "CAQTI_POOL"

let max_size = Max_size
let max_idle_size = Max_idle_size
let max_idle_age = Max_idle_age
let max_use_count = Max_use_count

let get (type a) (k : a key) config : a option =
  (match k with
   | Max_size -> config.max_size
   | Max_idle_size -> config.max_idle_size
   | Max_idle_age -> config.max_idle_age
   | Max_use_count -> config.max_use_count)

let modify (type a) (k : a key) (v : a option) config =
  (match k with
   | Max_size -> {config with max_size = v}
   | Max_idle_size -> {config with max_idle_size = v}
   | Max_idle_age -> {config with max_idle_age = v}
   | Max_use_count -> {config with max_use_count = v})

let set k v config = modify k (Some v) config
let unset k config = modify k None config

let merge_left cL cR =
  let add acc (Any k) =
    match get k cL with None -> acc | Some v -> (set k v acc)
  in
  List.fold_left add cR keys
OCaml

Innovation. Community. Security.