Skip to content

Optimize database logging and network reload handling#533

Merged
kasnder merged 2 commits intomasterfrom
claude/fix-battery-drain-iu5eB
Apr 1, 2026
Merged

Optimize database logging and network reload handling#533
kasnder merged 2 commits intomasterfrom
claude/fix-battery-drain-iu5eB

Conversation

@kasnder
Copy link
Copy Markdown
Member

@kasnder kasnder commented Apr 1, 2026

Summary

This PR implements performance optimizations to reduce database I/O overhead and improve responsiveness by batching log writes and debouncing network state change handlers.

Key Changes

Database Logging Optimization (DatabaseHelper.java)

  • Implemented log batching to reduce per-packet database I/O operations
    • Logs are accumulated in memory and flushed in batches of 50 or every 5 seconds
    • Moved ContentValues construction outside the database transaction
    • Batch flush is triggered before log queries and clears to ensure consistency

Network Reload Debouncing (ServiceSinkhole.java)

  • Added debounced reload mechanism to prevent excessive reloads from rapid network state changes
    • Network callbacks (onAvailable, onLinkPropertiesChanged, onCapabilitiesChanged, onLost) now use debouncedReload() instead of direct reload() calls
    • Enforces minimum 1-second interval between consecutive reloads
    • Pending reloads are coalesced to avoid redundant operations

Battery Optimization (ServiceSinkhole.java)

  • Skip stats updates when screen is off to reduce battery drain
    • Stats update frequency increased from 1000ms to 2000ms default

ICMP Response for Blocked UDP (udp.c)

  • Send ICMP Port Unreachable responses for blocked UDP packets
    • IPv4: ICMP Destination Unreachable (Type 3, Code 3)
    • IPv6: ICMPv6 Destination Unreachable (Type 1, Code 4)
    • Includes original packet data per RFC 792/4443 for proper error handling
    • Prevents long timeouts and improves app responsiveness

Implementation Details

  • Log batching uses synchronized list to safely accumulate entries across threads
  • Debouncing uses Handler with delayed execution to coalesce rapid network changes
  • ICMP responses properly construct headers with correct checksums and include original packet data
  • All optimizations maintain backward compatibility and data consistency

https://claude.ai/code/session_01EAkj12Bt6ywz1bQxNJEcKt

claude and others added 2 commits April 1, 2026 19:43
- Debounce network callback reloads (1s window) to prevent rapid
  successive VPN rule reconstructions when network state changes rapidly
- Send ICMP Port Unreachable for blocked UDP packets instead of silently
  dropping them, eliminating 4+ minute timeout hangs that waste battery
  and block app UI (addresses #381, #243, #176)
- Batch database log writes (50 entries or 5s) instead of per-packet
  transactions, significantly reducing I/O overhead
- Skip notification stats updates when screen is off (poll at 10s
  instead of generating bitmaps), and increase default stats frequency
  from 1000ms to 2000ms

https://claude.ai/code/session_01EAkj12Bt6ywz1bQxNJEcKt
- Remove ICMP Port Unreachable for blocked UDP (silent drop is safer,
  avoids per-packet write overhead and aggressive app retries)
- Remove network callback debouncing (risk of stale VPN rules during
  network transitions outweighs the reload savings)
- Make flushLogBatch() public and call it from onDestroy to prevent
  silent log loss on service shutdown
- Add SNI inspection trade-off analysis to TODO.md

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@kasnder kasnder merged commit 9f15d42 into master Apr 1, 2026
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