Skip to content

improve OkHttpClient reuse & efficiency#1131

Merged
meladRaouf merged 1 commit into
mainfrom
bugfix/MS-934-oom-in-okhttp
Mar 12, 2025
Merged

improve OkHttpClient reuse & efficiency#1131
meladRaouf merged 1 commit into
mainfrom
bugfix/MS-934-oom-in-okhttp

Conversation

@meladRaouf
Copy link
Copy Markdown
Collaborator

@meladRaouf meladRaouf commented Mar 8, 2025

JIRA ticket
Will be released in: 2025.2.0

Root cause analysis (for bugfixes only)

First known affected version:From the moment we started using OkHttp

The issue was identified through this Crashlytics OOM error, revealing that OkHttp had created 300 threads. After reviewing the OkHttp documentation, I discovered that SID was incorrectly creating a new OkHttpClient instance for each API call instead of reusing a single instance.

Notable changes

  • Reuses OkHttpClient for identical auth tokens, reducing memory usage.
  • Ensures thread-safe access with a lock.
  • Creates new instances only when the auth token changes.
  • Extracts timeouts as constants.

Testing guidance

As the change is in the network layer then all general api calls should be checked

  1. Login & Logout – Validate authentication/ non-authnticated API calls.
  2. Sync (Scheduled & Manual) – Ensure correct up/down sync behavior.
  3. License or Firmware files Download – Verify required files download properly.
  4. Image Upload – Check successful capture, upload, and retries.

Additional work checklist

  • Effect on other features and security has been considered
  • Design document marked as "In development" (if applicable)
  • External (Gitbook) and internal (Confluence) Documentation is up to date (or ticket created)
  • Test cases in Testiny are up to date (or ticket created)
  • Other teams notified about the changes (if applicable)

@cla-bot cla-bot Bot added the ... label Mar 8, 2025
- Reuses `OkHttpClient` for identical auth tokens, reducing memory usage.
- Ensures thread-safe access with a lock.
- Creates new instances only when the auth token changes.
- Extracts timeouts as constants.
@meladRaouf meladRaouf force-pushed the bugfix/MS-934-oom-in-okhttp branch from a0a5128 to f1ecc2b Compare March 8, 2025 19:27
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented Mar 8, 2025

@meladRaouf meladRaouf marked this pull request as ready for review March 10, 2025 02:41
Copy link
Copy Markdown
Contributor

@luhmirin-s luhmirin-s left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change feels unsafe, but I cannot pin-point why :D

@meladRaouf meladRaouf merged commit dae7931 into main Mar 12, 2025
@meladRaouf meladRaouf deleted the bugfix/MS-934-oom-in-okhttp branch March 12, 2025 07:21

fun get(
private var okHttpClient: OkHttpClient? = null
private var currentAuthToken: String? = null
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to double check - once logged in, we always use just one auth token for all calls going through OkHttp!?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, We use the same firebase auth token in every authenticated api request
if the token changed then the BuildOkHttpClientUseCase will create another OK http client

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants