123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156(* Copyright (C) 2014--2021 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.
*)openCaqti_common_privopenCaqti_compat[@@warning"-33"]letdefault_max_size=tryint_of_string(Sys.getenv"CAQTI_POOL_MAX_SIZE")withNot_found->8letdefault_log_src=Logs.Src.create"Caqti_pool"moduleMake(System:Caqti_driver_sig.System_common)=structopenSystemlet(>>=?)mmf=m>>=(functionOkx->mfx|Errore->return(Errore))moduleTask=structtypet={priority:float;mvar:unitMvar.t}letwake{mvar;_}=Mvar.store()mvarletcompare{priority=pA;_}{priority=pB;_}=Float.comparepBpAendmoduleTaskq=Caqti_heap.Make(Task)type'aentry={resource:'a;mutableused_count:int;}type('a,+'e)t={create:unit->('a,'e)resultfuture;free:'a->unitfuture;check:'a->(bool->unit)->unit;validate:'a->boolfuture;log_src:Logs.Src.t;max_idle_size:int;max_size:int;max_use_count:intoption;mutablecur_size:int;pool:'aentryQueue.t;mutablewaiting:Taskq.t;}letcreate?(max_size=default_max_size)?(max_idle_size=max_size)?(max_use_count=None)?(check=fun_f->ftrue)?(validate=fun_->returntrue)?(log_src=default_log_src)createfree=assert(max_size>0);assert(max_size>=max_idle_size);assert(Option.for_all(funn->n>0)max_use_count);{create;free;check;validate;log_src;max_idle_size;max_size;max_use_count;cur_size=0;pool=Queue.create();waiting=Taskq.empty}letsize{cur_size;_}=cur_sizeletwait~priorityp=letmvar=Mvar.create()inp.waiting<-Taskq.pushTask.({priority;mvar})p.waiting;Mvar.fetchmvarletschedulep=ifnot(Taskq.is_emptyp.waiting)thenbeginlettask,taskq=Taskq.pop_ep.waitinginp.waiting<-taskq;Task.waketaskendletreallocp=p.create()>|=(function|Oke->Ok{resource=e;used_count=0}|Errorerr->p.cur_size<-p.cur_size-1;schedulep;Errorerr)letrecacquire~priorityp=ifQueue.is_emptyp.poolthenbeginifp.cur_size<p.max_sizethenbeginp.cur_size<-p.cur_size+1;reallocpendelsewait~priorityp>>=fun()->acquire~prioritypendelsebeginlete=Queue.takep.poolinp.validatee.resource>>=funok->ifokthenreturn(Oke)elsebeginLog.warn~src:p.log_src(funf->f"Dropped pooled connection due to invalidation.")>>=fun()->reallocpendendletcan_reusepe=p.cur_size<=p.max_idle_size&&Option.for_all(funn->e.used_count<n)p.max_use_countletreleasepe=ifnot(can_reusepe)thenbeginp.cur_size<-p.cur_size-1;p.freee.resource>|=fun()->schedulependelsebeginp.checke.resourcebeginfunok->ifokthenQueue.addep.poolelsebeginLogs.warn~src:p.log_src(funf->f"Will not repool connection due to invalidation.");p.cur_size<-p.cur_size-1end;schedulepend;return()endletuse?(priority=0.0)fp=acquire~priorityp>>=?fune->finally(fun()->fe.resource)(fun()->e.used_count<-e.used_count+1;releasepe)letdisposepe=p.freee>|=fun()->p.cur_size<-p.cur_size-1letrecdrainp=ifp.cur_size=0thenreturn()else(ifQueue.is_emptyp.poolthenwait~priority:0.0pelsedisposep(Queue.takep.pool).resource)>>=fun()->drainpend