-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathtiming.py
64 lines (48 loc) · 1.48 KB
/
timing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from __future__ import annotations
import os
import sys
import time
from typing import Callable, Iterator, Optional, overload
system_delays = {
# based on data from https://github.com/Chia-Network/chia-blockchain/pull/13724
"github": {
"darwin": 20,
"linux": 1,
"win32": 10,
},
# arbitrarily selected
"local": {
"darwin": 2,
"linux": 1,
"win32": 1,
},
}
if os.environ.get("GITHUB_ACTIONS") == "true":
# https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables
_system_delay = system_delays["github"][sys.platform]
else:
try:
_system_delay = system_delays["local"][sys.platform]
except KeyError:
_system_delay = system_delays["local"]["linux"]
@overload
def adjusted_timeout(timeout: float) -> float: ...
@overload
def adjusted_timeout(timeout: None) -> None: ...
def adjusted_timeout(timeout: Optional[float]) -> Optional[float]:
if timeout is None:
return None
return timeout + _system_delay
def backoff_times(
initial: float = 0.001,
final: float = 0.100,
time_to_final: float = 0.5,
clock: Callable[[], float] = time.monotonic,
) -> Iterator[float]:
# initially implemented as a simple linear backoff
start = clock()
delta: float = 0
result_range = final - initial
while True:
yield min(final, initial + ((delta / time_to_final) * result_range))
delta = clock() - start