Skip to content
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

Disconnect from RC real-time server when app is backgrounded #5865

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

qdpham13
Copy link
Contributor

@qdpham13 qdpham13 commented Apr 12, 2024

Changes required to strongly close the Http stream connection when app is backgrounded:

  • Store instances of HttpUrlConnection and ConfigAutoFetch in ConfigRealtimeHttpClient so that they can be used to disconnect the current connection and propagate this background state change
  • Add isInBackground flag to ConfigAutoFetch
  • Add earlier references to InputStream within ConfigRealtimeHttpClient and ConfigAutoFetch so that we don't need to get the stream from HttpUrlConnection after the connection has already been disconnected
  • The rest of the changes are follow on work to handle network errors thrown when HttpUrlConnection#disconnect
    • These exceptions are thrown b/c listening on the stream is a blocking call and interrupting while listening will release a SocketException

go/rc-realtime-close-background-connections

Copy link
Contributor

github-actions bot commented Apr 12, 2024

Release note changes

No release note changes were detected. If you made changes that should be
present in the next release, ensure you've added an entry in the appropriate
CHANGELOG.md file(s).

@google-oss-bot
Copy link
Contributor

1 Warning
⚠️ Did you forget to add a changelog entry? (Add the 'no-changelog' label to the PR to silence this warning.)

Generated by 🚫 Danger

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Apr 12, 2024

Coverage Report 1

Affected Products

  • firebase-config

    Overall coverage changed from 84.11% (5c3f7ef) to 84.43% (d759291) by +0.32%.

    FilenameBase (5c3f7ef)Merge (d759291)Diff
    ConfigAutoFetch.java87.74%86.61%-1.13%
    ConfigRealtimeHttpClient.java68.93%73.36%+4.43%

Test Logs

  1. https://storage.googleapis.com/firebase-sdk-metric-reports/Gw6PjfZ0QZ.html

Copy link
Contributor

github-actions bot commented Apr 12, 2024

Unit Test Results

  40 files   -    180    40 suites   - 180   1m 31s ⏱️ - 2m 42s
311 tests  -    990  311 ✔️  -    974  0 💤  - 16  0 ±0 
634 runs   - 1 984  634 ✔️  - 1 952  0 💤  - 32  0 ±0 

Results for commit 2a4b6ee. ± Comparison against base commit 1d717ea.

♻️ This comment has been updated with latest results.

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Apr 12, 2024

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Apr 12, 2024

Startup Time Report 1

Note: Layout is sometimes suboptimal due to limited formatting support on GitHub. Please check this report on GCS.

Notes

Startup Times

  • fire-rc

    DeviceStatisticsDistributions
    oriole-32
    Percentile5c3f7efd759291DiffSignificant (?)
    p10629 ±802 μs106 ±94 μs-523 μs (-83.1%)NO
    p25659 ±837 μs112 ±96 μs-547 μs (-83.0%)NO
    p50701 ±888 μs121 ±101 μs-580 μs (-82.7%)NO
    p75806 ±1024 μs140 ±107 μs-665 μs (-82.6%)NO
    p90943 ±1174 μs173 ±112 μs-771 μs (-81.7%)NO

    20 test runs in comparison
    CommitTest Runs
    5c3f7ef
    • 2024-04-23_18:23:47.311020_NkBs
    • 2024-04-23_18:23:47.311065_hRla
    • 2024-04-23_18:23:47.311075_AOBv
    • 2024-04-23_18:23:47.311083_dJRF
    • 2024-04-23_18:23:47.311090_DsLH
    • 2024-04-23_18:23:47.311098_JKun
    • 2024-04-23_18:23:47.311105_tywt
    • 2024-04-23_18:23:47.311113_YAgY
    • 2024-04-23_18:23:47.311120_bTAl
    • 2024-04-23_18:23:47.311128_HZWG
    d759291
    • 2024-04-23_19:30:36.395984_MBaq
    • 2024-04-23_19:30:36.396033_aHDS
    • 2024-04-23_19:30:36.396047_lXRT
    • 2024-04-23_19:30:36.396056_YiSz
    • 2024-04-23_19:30:36.396065_iBZn
    • 2024-04-23_19:30:36.396074_tqXD
    • 2024-04-23_19:30:36.396089_fgKW
    • 2024-04-23_19:30:36.396099_QjvT
    • 2024-04-23_19:30:36.396108_zgLm
    • 2024-04-23_19:30:36.396117_BlFA
    redfin-30
    Percentile5c3f7efd759291DiffSignificant (?)
    p10183 ±59 μs358 ±363 μs+175 μs (+95.4%)NO
    p25204 ±78 μs407 ±434 μs+203 μs (+99.1%)NO
    p50242 ±133 μs493 ±537 μs+251 μs (+103.9%)NO
    p75294 ±216 μs646 ±718 μs+352 μs (+119.8%)NO
    p90363 ±332 μs890 ±1108 μs+527 μs (+145.0%)NO

    20 test runs in comparison
    CommitTest Runs
    5c3f7ef
    • 2024-04-23_18:23:47.311020_NkBs
    • 2024-04-23_18:23:47.311065_hRla
    • 2024-04-23_18:23:47.311075_AOBv
    • 2024-04-23_18:23:47.311083_dJRF
    • 2024-04-23_18:23:47.311090_DsLH
    • 2024-04-23_18:23:47.311098_JKun
    • 2024-04-23_18:23:47.311105_tywt
    • 2024-04-23_18:23:47.311113_YAgY
    • 2024-04-23_18:23:47.311120_bTAl
    • 2024-04-23_18:23:47.311128_HZWG
    d759291
    • 2024-04-23_19:30:36.395984_MBaq
    • 2024-04-23_19:30:36.396033_aHDS
    • 2024-04-23_19:30:36.396047_lXRT
    • 2024-04-23_19:30:36.396056_YiSz
    • 2024-04-23_19:30:36.396065_iBZn
    • 2024-04-23_19:30:36.396074_tqXD
    • 2024-04-23_19:30:36.396089_fgKW
    • 2024-04-23_19:30:36.396099_QjvT
    • 2024-04-23_19:30:36.396108_zgLm
    • 2024-04-23_19:30:36.396117_BlFA
  • timeToInitialDisplay

    DeviceStatisticsDistributions
    oriole-32
    Percentile5c3f7efd759291DiffSignificant (?)
    p10203 ±4 ms208 ±6 ms+5.15 ms (+2.5%)NO
    p25209 ±5 ms215 ±6 ms+5.75 ms (+2.7%)NO
    p50217 ±6 ms223 ±6 ms+6.03 ms (+2.8%)NO
    p75225 ±6 ms232 ±7 ms+6.78 ms (+3.0%)NO
    p90234 ±6 ms248 ±9.7 ms+14.4 ms (+6.2%)NO

    20 test runs in comparison
    CommitTest Runs
    5c3f7ef
    • 2024-04-23_18:23:47.311020_NkBs
    • 2024-04-23_18:23:47.311065_hRla
    • 2024-04-23_18:23:47.311075_AOBv
    • 2024-04-23_18:23:47.311083_dJRF
    • 2024-04-23_18:23:47.311090_DsLH
    • 2024-04-23_18:23:47.311098_JKun
    • 2024-04-23_18:23:47.311105_tywt
    • 2024-04-23_18:23:47.311113_YAgY
    • 2024-04-23_18:23:47.311120_bTAl
    • 2024-04-23_18:23:47.311128_HZWG
    d759291
    • 2024-04-23_19:30:36.395984_MBaq
    • 2024-04-23_19:30:36.396033_aHDS
    • 2024-04-23_19:30:36.396047_lXRT
    • 2024-04-23_19:30:36.396056_YiSz
    • 2024-04-23_19:30:36.396065_iBZn
    • 2024-04-23_19:30:36.396074_tqXD
    • 2024-04-23_19:30:36.396089_fgKW
    • 2024-04-23_19:30:36.396099_QjvT
    • 2024-04-23_19:30:36.396108_zgLm
    • 2024-04-23_19:30:36.396117_BlFA
    redfin-30
    Percentile5c3f7efd759291DiffSignificant (?)
    p10249 ±5 ms276 ±16 ms+27.6 ms (+11.1%)NO
    p25255 ±6 ms283 ±17 ms+28.0 ms (+11.0%)NO
    p50262 ±9 ms293 ±23 ms+30.6 ms (+11.6%)NO
    p75270 ±9 ms307 ±36 ms+36.5 ms (+13.5%)NO
    p90279 ±11 ms320 ±38 ms+41.4 ms (+14.9%)NO

    20 test runs in comparison
    CommitTest Runs
    5c3f7ef
    • 2024-04-23_18:23:47.311020_NkBs
    • 2024-04-23_18:23:47.311065_hRla
    • 2024-04-23_18:23:47.311075_AOBv
    • 2024-04-23_18:23:47.311083_dJRF
    • 2024-04-23_18:23:47.311090_DsLH
    • 2024-04-23_18:23:47.311098_JKun
    • 2024-04-23_18:23:47.311105_tywt
    • 2024-04-23_18:23:47.311113_YAgY
    • 2024-04-23_18:23:47.311120_bTAl
    • 2024-04-23_18:23:47.311128_HZWG
    d759291
    • 2024-04-23_19:30:36.395984_MBaq
    • 2024-04-23_19:30:36.396033_aHDS
    • 2024-04-23_19:30:36.396047_lXRT
    • 2024-04-23_19:30:36.396056_YiSz
    • 2024-04-23_19:30:36.396065_iBZn
    • 2024-04-23_19:30:36.396074_tqXD
    • 2024-04-23_19:30:36.396089_fgKW
    • 2024-04-23_19:30:36.396099_QjvT
    • 2024-04-23_19:30:36.396108_zgLm
    • 2024-04-23_19:30:36.396117_BlFA

  1. https://storage.googleapis.com/firebase-sdk-metric-reports/XBvPwLOdFa/index.html

try {
stream.close();
} catch (IOException ex) {
Log.d(TAG, "Exception thrown when closing connection stream. Retrying connection...", ex);
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we actually retry the connection in this case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah if it we made it to this point where the stream is not null and we're trying to close it, we reset (http://shortn/_XOhXyPPLvd) the retry count which should guarantee a retry

void setRealtimeBackgroundState(boolean backgroundState) {
isInBackground = backgroundState;
public void setRealtimeBackgroundState(boolean backgroundState) {
// Make changes in synchronized block so that everything is updated in a single atomic
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think synchronized guarantees atomicity or transactionality (i.e. everything succeeds or fails together), only that no two threads modify the same variables at the same time. Is it important these operations happen in a transaction?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh my bad, I wrote the description wrong. These operations don't need to happen in a transaction but within a synchronized block where only one thread should operate at a time. Updated the comment

try {
// Only need to close the InputStream, ConfigRealtimeHttpClient will disconnect
// HttpUrlConnection
inputStream.close();
Copy link
Contributor

Choose a reason for hiding this comment

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

As much as it's possible we should keep all the network calls required to connect/disconnect in the same place to avoid situations where one method is called but not the other. That's a bit challenging here since it's already split up some, but if it's possible I'd aim for it

Copy link
Contributor Author

@qdpham13 qdpham13 Apr 23, 2024

Choose a reason for hiding this comment

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

Yeah I agree it'd be better to keep in one area. I think we could do this in a separate PR where instead of passing HttpUrlConnection to ConfigAutoFetch, we pass the InputStream we pull out in ConfigHttpRealtimeClient.beginRealtimeHttpStream. I'll add this as a followup in the doc

// HttpUrlConnection.
if (isInBackground) {
if (httpURLConnection != null) {
httpURLConnection.disconnect();
Copy link
Contributor

Choose a reason for hiding this comment

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

Would this instance of HTTPUrlConnection would be (re)used after this point? The docs indicate it shouldn't be reused: https://developer.android.com/reference/java/net/HttpURLConnection#disconnect(). Wondering if there could be side-effects or errors from another thread still reading the connection

Copy link
Contributor Author

@qdpham13 qdpham13 Apr 23, 2024

Choose a reason for hiding this comment

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

No it won't be reused. We'll disconnect here and the thread that was listening on the connection (blocking at http://shortn/_q8UFRszL0G) will stop listening and and will exit beginRealtimeHttpStream resetting all the flags so that the next time beginRealtimeHttpStream is called a new connection will be used.

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

Successfully merging this pull request may close these issues.

None yet

3 participants