123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109(* 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.
*)moduleLog=(valLogs.src_log(Logs.Src.create"caqti.config"))typet={max_size:intoption;max_idle_size:intoption;max_idle_age:Mtime.Span.toptionoption;max_use_count:intoptionoption;}type_key=|Max_size:intkey|Max_idle_size:intkey|Max_idle_age:Mtime.Span.toptionkey|Max_use_count:intoptionkeytypeany_key=Any:_key->any_keyletkeys=[AnyMax_size;AnyMax_idle_size;AnyMax_idle_age;AnyMax_use_count;]letcreate?max_size?max_idle_size?max_idle_age?max_use_count()={max_size;max_idle_size;max_idle_age;max_use_count}letoption_of_stringf=function|""|"none"->None|s->Some(fs)letmtime_span_of_strings=letx=float_of_stringsin(matchMtime.Span.of_float_ns(x*.1e9)with|None->failwith"Mtime.Span.of_float_ns"|Somex->x)letdefault=create()letcreate_from_envpfx=letgetconvsfx=letvar=pfx^sfxin(matchSys.getenv_optvarwith|None->None|Somestr->(matchconvstrwith|value->Somevalue|exceptionFailure_->Log.err(funm->m"Failed to parse $%s = %s."varstr);None))in{max_size=getint_of_string"_MAX_SIZE";max_idle_size=getint_of_string"_MAX_IDLE_SIZE";max_idle_age=get(option_of_stringmtime_span_of_string)"_MAX_IDLE_AGE";max_use_count=get(option_of_stringint_of_string)"_MAX_USE_COUNT";}letdefault_from_env()=create_from_env"CAQTI_POOL"letmax_size=Max_sizeletmax_idle_size=Max_idle_sizeletmax_idle_age=Max_idle_ageletmax_use_count=Max_use_countletget(typea)(k:akey)config:aoption=(matchkwith|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)letmodify(typea)(k:akey)(v:aoption)config=(matchkwith|Max_size->{configwithmax_size=v}|Max_idle_size->{configwithmax_idle_size=v}|Max_idle_age->{configwithmax_idle_age=v}|Max_use_count->{configwithmax_use_count=v})letsetkvconfig=modifyk(Somev)configletunsetkconfig=modifykNoneconfigletmerge_leftcLcR=letaddacc(Anyk)=matchgetkcLwithNone->acc|Somev->(setkvacc)inList.fold_leftaddcRkeys