Skip to content

proxy: fix resource leaks and improve mobile battery life#861

Open
cyrilleberaud wants to merge 2 commits into
masterfrom
fix/proxy-mobile-battery-optimization
Open

proxy: fix resource leaks and improve mobile battery life#861
cyrilleberaud wants to merge 2 commits into
masterfrom
fix/proxy-mobile-battery-optimization

Conversation

@cyrilleberaud

Copy link
Copy Markdown
Contributor

Summary

Fix three proxy issues that cause excessive resource usage on the server and battery drain on mobile clients.

Server: Fix HTTP connection timeout

  • read_next_http_message_timelimit was set to std::numeric_limits<int>::max() milliseconds (~24 days)
  • Dead mobile clients would hold server resources for weeks
  • Fixed: set timeout to 1 hour, matching the proxy operation refresh cycle

Client: Add exponential backoff for reconnection

  • When disconnected, the client retried every 60 seconds regardless of failure count
  • On flaky mobile networks, this caused unnecessary battery drain
  • Fixed: exponential backoff from 1 min → 2 → 4 → 8 → 16 → 30 min (capped)
  • Backoff resets on explicit connectivity change (getConnectivityStatus())

Client: Add jitter to listener resubscription

  • On reconnect, all listeners were resubscribed immediately, causing a thundering herd
  • Fixed: add random 0-2s jitter before resubscribing each listener

These fixes are part of a broader effort to make Jami work reliably with push notifications on Android while minimizing battery usage.

cyrilleberaud and others added 2 commits May 16, 2026 18:47
Server: Reduce HTTP idle connection timeout from ~24 days
(numeric_limits<int>::max() milliseconds) to 1 hour. Dead mobile
clients are now cleaned up promptly instead of holding server
resources for weeks.

Client: Add exponential backoff for proxy reconnection when
disconnected (1 min -> 2 -> 4 -> 8 -> 16 -> 30 min cap). Previously
used a fixed 1-minute retry which drained battery on flaky mobile
networks. Reset backoff on successful connection or explicit
connectivity change (WiFi/cellular switch).

Client: Add small random jitter (0-2s) before resubscribing all
listeners on reconnection to avoid burst of requests to the server.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Change-Id: Ic29c4a0b2850997db9e559f01146024f41190a5c
The HTTP Urgency header set on WebPush requests is not exposed to
UnifiedPush Android clients by the connector library. Include a
"priority" field ("high" or "normal") directly in the JSON body
so that clients can determine whether a push notification requires
immediate wake-up processing or can be deferred until the next
natural activity.

This complements the daemon-side priority assignment (Gerrit #34059)
and allows UnifiedPush (F-Droid) builds to benefit from the same
battery savings as FCM builds.

Change-Id: Id316ff89e64e59d1b224f5b4e14da6b36dc62c95
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown

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 targets excessive proxy resource usage and mobile battery drain by tightening server-side HTTP timeouts and making client reconnection/listener restarts less aggressive.

Changes:

  • Server: reduce RESTinio idle/read request time limits from ~24 days to 1 hour; add push priority into UnifiedPush JSON payload.
  • Client: introduce exponential backoff for proxy confirmation/reconnect attempts (1 → 2 → 4 → … → 30 minutes) and add a small reconnect-time jitter before restarting listeners.
  • Repo hygiene: ignore jamid.log.

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 3 comments.

File Description
src/dht_proxy_server.cpp Uses a bounded (1h) connection/message timeout; adds push priority to UnifiedPush JSON payload.
src/dht_proxy_client.cpp Adds reconnect backoff logic and a reconnect-time jitter before restarting listeners.
include/opendht/dht_proxy_client.h Stores backoff state (proxyRetryDelay_) and a max cap constant.
.gitignore Ignores jamid.log.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/dht_proxy_client.cpp
Comment on lines +1179 to +1180
// Reset backoff on explicit connectivity change (e.g., WiFi/cellular switch)
proxyRetryDelay_ = std::chrono::minutes(1);
Comment thread src/dht_proxy_client.cpp
Comment on lines +785 to +788
// Small random delay (0-2s) to avoid resubscription burst
auto jitter = std::chrono::milliseconds(
std::rand() % 2000);
listenerRestartTimer_->expires_at(std::chrono::steady_clock::now() + jitter);
Comment thread src/dht_proxy_client.cpp
Comment on lines +785 to 790
// Small random delay (0-2s) to avoid resubscription burst
auto jitter = std::chrono::milliseconds(
std::rand() % 2000);
listenerRestartTimer_->expires_at(std::chrono::steady_clock::now() + jitter);
listenerRestartTimer_->async_wait(std::bind(&DhtProxyClient::restartListeners, this, std::placeholders::_1));
if (not onConnectCallbacks_.empty()) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants