Add throttling for api entreprise

This commit is contained in:
simon lehericey 2021-02-10 09:59:58 +01:00
parent b6d8409c9f
commit f7ae974f29

View file

@ -11,6 +11,7 @@ class APIEntreprise::API
PRIVILEGES_RESOURCE_NAME = "privileges"
TIMEOUT = 20
DEFAULT_API_ENTREPRISE_DELAY = 0.0
def self.entreprise(siren, procedure_id)
call_with_siret(ENTREPRISE_RESOURCE_NAME, siren, procedure_id)
@ -60,6 +61,15 @@ class APIEntreprise::API
def self.call_with_token(resource_name, token)
url = "#{API_ENTREPRISE_URL}/#{resource_name}"
# this is a poor man throttling
# the idea is to queue api entreprise job on 1 worker
# and add a delay between each call
# example: API_ENTREPRISE_DELAY=1 => 60 rpm max
if api_entreprise_delay != 0.0
sleep api_entreprise_delay
end
response = Typhoeus.get(url,
headers: { Authorization: "Bearer #{token}" },
timeout: TIMEOUT)
@ -76,6 +86,10 @@ class APIEntreprise::API
url = url(resource_name, siret_or_siren)
params = params(siret_or_siren, procedure_id, user_id)
if api_entreprise_delay != 0.0
sleep api_entreprise_delay
end
response = Typhoeus.get(url,
headers: { Authorization: "Bearer #{token_for_procedure(procedure_id)}" },
params: params,
@ -125,4 +139,8 @@ class APIEntreprise::API
procedure = Procedure.find(procedure_id)
procedure.api_entreprise_token
end
def self.api_entreprise_delay
ENV.fetch("API_ENTREPRISE_DELAY", DEFAULT_API_ENTREPRISE_DELAY).to_f
end
end