Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemanspiff2007 committed Apr 15, 2024
1 parent 68fd8a2 commit d7d73ed
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
11 changes: 10 additions & 1 deletion src/sml2mqtt/config/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ class HttpSourceSettings(SmlSourceSettingsBase):
user: str = Field(default='', description='User (if needed)')
password: str = Field(default='', description='Password (if needed)')

request_timeout: StrictInt | StrictFloat | None = Field(
default=None, alias='request timeout', description='Dedicated timeout for the http request',
in_file=False
)

@override
def get_device_name(self) -> str:
return self.url.host
Expand All @@ -102,4 +107,8 @@ def check_timeout_gt_interval(self):
return self

def get_request_timeout(self) -> ClientTimeout:
return ClientTimeout(self.interval)
value = self.interval if self.request_timeout is None else self.request_timeout
if value is None:
msg = 'No value for ClientTimeout'
raise ValueError(msg)
return ClientTimeout(total=value)
7 changes: 3 additions & 4 deletions src/sml2mqtt/sml_source/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,9 @@ async def _http_task(self) -> None:
com_errors += 1
max_ignore: int = 7
if com_errors <= max_ignore:
# errors: 1, 2, 3, 4, 5, 6, 7, 8, ...
# factor: 0.5, 1.1, 2.1, 3.6, 5.5, 8.1, 11.1, 14.5, ...
# With 0.8 x interval we can do the first three retries without going into timeout
interval = (((com_errors - 0.5) ** 2) / 4 + 0.5) * (self.interval * 0.8)
# errors: 1, 2, 3, 4, 5, 6, 7, 8, 9, ...
# factor: 0.4, 0.9, 1.9, 3.4, 5.4, 7.9, 10.9, 14.4, 18.4, ...
interval = (((com_errors - 0.5) ** 2) / 4 + 0.3) * self.interval
log.debug(f'Ignored {com_errors:d}/{max_ignore:d} {e}')
continue

Expand Down

0 comments on commit d7d73ed

Please sign in to comment.