123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)typeattempt_event={attempt:int;delay:float;text:string}moduleAttempt_logging=Internal_event.Make(structtypet=attempt_eventletsection=Noneletname="rpc_http_attempt"letdoc="Error emitted when an HTTP request returned a 502 error."letencoding=Data_encoding.(conv(fun{attempt;delay;text}->(attempt,delay,text))(fun(attempt,delay,text)->{attempt;delay;text})(obj3(req"attempt"int8)(req"delay"float)(req"text"string)))letpp~all_fields:_~block:_f{attempt;delay;text}=Format.fprintff"Attempt number %d/10, will retry after %g seconds.\n\
Original body follows.\n\
%s"attemptdelaytextletlevel=Internal_event.Errorend)moduleRetryClient:Cohttp_lwt.S.Clientwithtypectx=Cohttp_lwt_unix.Net.ctx=structincludeCohttp_lwt_unix.Clientletclone_body=function`Streams->`Stream(Lwt_stream.clones)|x->xletcall?ctx?headers?body?chunkedmethuri=letreccall_and_retry_on_502attemptdelay=letopenLwt_syntaxinlet*response,ansbody=Cohttp_lwt_unix.Client.call?ctx?headers?body?chunkedmethuriinletstatus=Cohttp.Response.statusresponseinmatchstatuswith|`Bad_gateway->letlog_ansbody=clone_bodyansbodyinlet*text=Cohttp_lwt.Body.to_stringlog_ansbodyinlet*_=Attempt_logging.emit{attempt;delay;text}inifattempt>=10thenreturn(response,ansbody)elselet*()=Lwt_unix.sleepdelayincall_and_retry_on_502(attempt+1)(delay+.0.1)|_->return(response,ansbody)incall_and_retry_on_50210.endincludeRPC_client.Make(Resto_cohttp_client.Client.OfCohttp(RetryClient))