Merge pull request #5889 from betagouv/add_throttling_for_api_entreprise
Add throttling for api entreprise
This commit is contained in:
commit
48bc15a932
1 changed files with 18 additions and 0 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue