Skip to content

Commit

Permalink
introduce some waiting time, retries and cooldown to avoid getting ki…
Browse files Browse the repository at this point in the history
…cked out by SE too often
  • Loading branch information
gjdv committed Apr 7, 2023
1 parent 874468a commit df94fc1
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/solaredgeoptimizers/solaredgeoptimizers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
""" My module """
import time

import requests
import json
import logging
Expand Down Expand Up @@ -115,7 +117,7 @@ def requestPanelHistory(self, itemId, starttime=None, endtime=None, parameter="P
starttime, endtime, parameter
)

r = self._doRequest("GET", url)
r = self._doRequestWithCooldown("GET", url)
if r.startswith("ERROR001"):
raise Exception("Error while doing request: %s" % r)

Expand All @@ -139,6 +141,24 @@ def requestHistoricalData(self, starttime=None, endtime=None, parameter="Power")

return data

def _doRequestWithCooldown(self, method, request_url, data=None, wait_sec=0.1, cooldown_sec=5, n_retries=3):
"""
Same as _doRequest, but waiting before each call, and in between retries in case it fails
"""
e = Exception("Could not perform request within %d retries" % n_retries)
for i in range(n_retries):
try:
time.sleep(wait_sec)
res = self._doRequest(method=method, request_url=request_url, data=data)
return res
except ConnectionError as e:
if isinstance(e.args[0], Exception) and len(e.args[0].args) > 1 and \
isinstance(e.args[0].args[1], ConnectionResetError) and e.args[0].args[1].errno == 10054:
time.sleep(cooldown_sec)
continue
raise e
raise e

def _doRequest(self, method, request_url, data=None):
session = Session()
session.head(
Expand Down

0 comments on commit df94fc1

Please sign in to comment.