-
Notifications
You must be signed in to change notification settings - Fork 161
EMT-2369 -- Eliminate Thread.sleep() in Network Layer with Coroutine-based Retry Mechanism. #1309
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: wpinho-branch/EMT-2274
Are you sure you want to change the base?
Conversation
- Introduced BranchAsyncNetworkLayer for non-blocking network operations, replacing Thread.sleep() with coroutine delays. - Added BranchRemoteInterfaceUrlConnectionAsync to maintain API compatibility while leveraging modern async operations. - Enhanced retry mechanisms with exponential backoff and cancellation support for improved network reliability. - Updated BranchRemoteInterfaceUrlConnection to support both legacy and modern implementations, allowing for flexible usage. - Improved logging for better debugging and performance insights during network requests.
ReferenceEMT-2369 -- Eliminate Thread.sleep() in Network Layer with Coroutine-based Retry Mechanism. DescriptionSummary By MatterAI
🔄 What ChangedThis pull request introduces a new 🔍 Impact of the ChangeThe primary impact is the elimination of blocking operations during network retries, which significantly improves the responsiveness and resource utilization of the Branch SDK. This change prevents potential ANRs (Application Not Responding) and improves overall application performance, especially under poor network conditions. The exponential backoff with jitter enhances network stability by reducing server load during transient failures. The new architecture also provides a clear path for future asynchronous network operations and better error handling. 📁 Total Files Changed
🧪 Test AddedN/A - No explicit test files were added or modified in this pull request. The changes primarily focus on refactoring the network layer's implementation details. 🔒Security VulnerabilitiesNo new security vulnerabilities were introduced. The change improves reliability and performance without compromising existing security measures. The retry mechanism with exponential backoff and jitter is a robust pattern that helps prevent accidental Denial-of-Service (DoS) scenarios from aggressive retries. Testing InstructionsRisk Assessment [
|
Important PR Review SkippedPR review skipped as per the configuration setting. Run a manually review by commenting /matter review 💡Tips to use MatterAICommand List
|
Reference
EMT-2369 -- Eliminate Thread.sleep() in Network Layer with Coroutine-based Retry Mechanism.
Description
This PR addresses a critical performance issue in the Branch SDK's network layer where
Thread.sleep()
calls were blocking valuable threads during retry operations, leading to thread pool exhaustion and inefficient resource usage.Problem Solved:
Thread.sleep()
inBranchRemoteInterfaceUrlConnection.java
(lines 89-95, 124-125) were blocking threads during network retriesSolution Implemented:
BranchAsyncNetworkLayer.kt
replaces blockingThread.sleep()
with non-blockingdelay()
coroutinesBranchRemoteInterfaceUrlConnectionAsync.kt
adapter maintains 100% API compatibilityBranchRemoteInterfaceUrlConnection.java
routes to async implementation by default, with fallback supportKey Improvements:
Testing Instructions
Log Verification: Look for these key log messages indicating Thread.sleep() elimination:
Retry Behavior Test: Simulate network errors (500+ response codes) and verify:
Backward Compatibility Test: Verify existing Branch SDK functionality remains unchanged
Risk Assessment
LOW
Rationale for LOW risk:
Mitigation Strategies:
Feature flag allows instant rollback to legacy implementation
Comprehensive logging enables quick issue identification
Adapter pattern ensures no changes to existing call sites
I, the PR creator, have tested — integration, unit, or otherwise — this code.
Reviewer Checklist (To be checked off by the reviewer only)
cc @BranchMetrics/saas-sdk-devs for visibility.
Logs:
D BranchRemoteInterfaceUrlConnection: Using async implementation for POST (no Thread.sleep)
D BranchRemoteInterfaceUrlConnectionAsync: Executing POST request using async layer (no Thread.sleep)
D BranchAsyncNetworkLayer: Starting POST request to https://api2.branch.io/v1/open with payload size 1518 chars
D BranchAsyncNetworkLayer: Starting request with retry limit 3
V BranchAsyncNetworkLayer: Executing request attempt #0
D hide(ime(), fromIme=false)
I io.branch.branchandroidtestbed:3a1d9e7a: onCancelled at PHASE_CLIENT_ALREADY_HIDDEN
I Compiler allocated 5111KB to compile void android.view.ViewRootImpl.performTraversals()
V Branch Networking Success
URL: https://api2.branch.io/v1/open
Response Code: 200
Response Message: OK
Retry number: 0
requestId: 1bd6e249-4f3a-4558-920a-d34b13ab6adc-2025081421