Skip to content

Commit 8e5ac97

Browse files
committed
Update rate limiting documentation to match implementation
- Fix incorrect default values in config example - Add comprehensive exponential backoff example - Update log message format to match actual output - Add advanced usage section with datetime-based retry example
1 parent 8aaa295 commit 8e5ac97

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

docs/RATE_LIMITING.md

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ client = FitbitClient(
4848
redirect_uri="https://localhost:8080",
4949

5050
# Rate limiting options (all optional)
51-
max_retries=3, # Maximum retry attempts (default: 5)
52-
retry_after_seconds=10, # Base wait time if headers missing (default: 30)
53-
retry_backoff_factor=1.5 # Multiplier for successive waits (default: 2.0)
51+
max_retries=5, # Maximum retry attempts (default: 3)
52+
retry_after_seconds=60, # Base wait time if headers missing (default: 60)
53+
retry_backoff_factor=1.5 # Multiplier for successive waits (default: 1.5)
5454
)
5555
```
5656

@@ -68,12 +68,13 @@ The client uses the following strategy for retries:
6868
retry_time = retry_after_seconds * (retry_backoff_factor ^ retry_count)
6969
```
7070

71-
With the default settings and no headers:
71+
With example settings of 5 retries and no headers:
7272

73-
- First retry: Wait 30 seconds
74-
- Second retry: Wait 60 seconds (30 * 2.0)
75-
- Third retry: Wait 240 seconds (60 * 2.0²)
76-
- Fourth retry: Wait 240 seconds (240 * 2.0²)
73+
- First retry: Wait 60 seconds (base time)
74+
- Second retry: Wait 90 seconds (60 * 1.5¹)
75+
- Third retry: Wait 135 seconds (60 * 1.5²)
76+
- Fourth retry: Wait 202.5 seconds (60 * 1.5³)
77+
- Fifth retry: Wait 303.75 seconds (60 * 1.5⁴)
7778

7879
## Logging
7980

@@ -93,7 +94,7 @@ client = FitbitClient(...)
9394
You'll see log messages like:
9495

9596
```
96-
WARNING:fitbit_client.SleepResource:Rate limit exceeded for get_sleep_log_list to sleep/list.json. [Rate Limit: 0/150, Reset in: 600s] (Will retry after 600 seconds if retries are enabled)
97+
WARNING:fitbit_client.SleepResource:Rate limit exceeded for get_sleep_log_list to sleep/list.json. [Rate Limit: 0/150] Retrying in 600 seconds. (4 retries remaining)
9798
```
9899

99100
## Handling Unrecoverable Rate Limits
@@ -133,3 +134,27 @@ except RateLimitExceededException as e:
133134

134135
These can be used to implement more sophisticated retry or backoff strategies in
135136
your application.
137+
138+
## Advanced Usage
139+
140+
You can implement custom strategies by combining rate limit information with
141+
your own timing logic:
142+
143+
```python
144+
from datetime import datetime, timedelta
145+
from time import sleep
146+
147+
try:
148+
client.get_daily_activity_summary(date="today")
149+
except RateLimitExceededException as e:
150+
# Calculate next reset time (typically the top of the next hour)
151+
reset_time = datetime.now() + timedelta(seconds=e.rate_limit_reset)
152+
print(f"Rate limit reached. Pausing until {reset_time.strftime('%H:%M:%S')}")
153+
154+
# Wait until reset time plus a small buffer
155+
wait_seconds = e.rate_limit_reset + 5
156+
sleep(wait_seconds)
157+
158+
# Try again after waiting
159+
client.get_daily_activity_summary(date="today")
160+
```

0 commit comments

Comments
 (0)