-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add Adaptive Polling Intervals to WebServer #7864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Add Adaptive Polling Intervals to WebServer #7864
Conversation
Replace fixed 5ms polling with adaptive intervals based on HTTP activity: - 50ms during active periods (first 5 seconds after request) - 200ms during medium activity (5-30 seconds) - 1000ms during idle periods (30+ seconds) Reduces CPU usage significantly during idle periods while maintaining responsiveness when handling HTTP requests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements adaptive polling intervals for the WebServer to reduce CPU and memory usage during idle periods. The optimization replaces the fixed 5ms polling interval with dynamic intervals based on HTTP request activity.
Key changes:
- Added activity tracking with
markActivity()
method called from HTTP request handlers - Implemented
getAdaptiveInterval()
to return 50ms/200ms/1000ms intervals based on time since last activity - Modified
runOnce()
to use adaptive intervals instead of fixed 5ms polling
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
File | Description |
---|---|
src/mesh/http/WebServer.h | Added private lastActivityTime member and public markActivity() method declaration |
src/mesh/http/WebServer.cpp | Implemented adaptive polling logic with three-tier interval system |
src/mesh/http/ContentHandler.cpp | Added activity tracking calls to HTTP request handlers |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
|
||
int32_t WebServerThread::getAdaptiveInterval() | ||
{ | ||
uint32_t timeSinceActivity = millis() - lastActivityTime; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential integer overflow issue when millis()
wraps around (approximately every 50 days). When millis()
resets to 0 and lastActivityTime
contains a large value, the subtraction will underflow and produce an incorrect large positive value, causing the system to incorrectly stay in idle mode.
Copilot uses AI. Check for mistakes.
if (timeSinceActivity < 5000) { | ||
return 50; | ||
} else if (timeSinceActivity < 30000) { | ||
return 200; | ||
} else { | ||
return 1000; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Magic numbers should be defined as named constants to improve code readability and maintainability. Consider defining constants like ACTIVE_THRESHOLD_MS = 5000
, MEDIUM_THRESHOLD_MS = 30000
, ACTIVE_INTERVAL_MS = 50
, etc.
Copilot uses AI. Check for mistakes.
🤔 Problem
WebServer currently polls every
5ms
regardless of activity, consuming unnecessary CPU resources during idle periods.🦥 Solution
Replace fixed 5ms interval with adaptive polling based on HTTP request activity:
50ms
- Active mode (first 5 seconds after HTTP request)200ms
- Medium mode (5-30 seconds after last activity)1000ms
- Idle mode (30+ seconds without requests)📝 Implementation
markActivity()
calls to HTTP request handlersgetAdaptiveInterval()
for dynamic timingThis optimization improves battery life on devices with WiFi enabled while keeping the web interface fully responsive
when needed.
📊 Measurement Results
Measured on ESP32 (
esp32-pico-d4
) over 30-second intervals:🔋 Benefits up to ~10% battery life improvement
🤝 Attestations