-
Notifications
You must be signed in to change notification settings - Fork 646
Fallback mode v2 #6872
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
Closed
Closed
Fallback mode v2 #6872
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| /* | ||
| * Copyright 2023 Google LLC | ||
| * Copyright 2025 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
|
|
@@ -19,22 +19,31 @@ package com.google.firebase.sessions | |
| import android.app.Activity | ||
| import android.app.Application.ActivityLifecycleCallbacks | ||
| import android.os.Bundle | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| /** | ||
| * Lifecycle callbacks that will inform the [SharedSessionRepository] whenever an [Activity] in this | ||
| * application process goes foreground or background. | ||
| */ | ||
| @Singleton | ||
| internal class SessionsActivityLifecycleCallbacks | ||
| @Inject | ||
| constructor(private val sharedSessionRepository: SharedSessionRepository) : | ||
| ActivityLifecycleCallbacks { | ||
|
|
||
| override fun onActivityResumed(activity: Activity) = sharedSessionRepository.appForeground() | ||
| internal interface SessionLifecycleClient { | ||
| var localSessionData: SessionData | ||
| fun appForegrounded() | ||
| fun appBackgrounded() | ||
| fun unregister() = Unit | ||
| } | ||
|
|
||
| override fun onActivityPaused(activity: Activity) = sharedSessionRepository.appBackground() | ||
| internal object SessionInitiator : ActivityLifecycleCallbacks { | ||
| var currentLocalSession: SessionDetails? = null | ||
| get() { | ||
| return lifecycleClient?.localSessionData?.sessionDetails | ||
| } | ||
| var lifecycleClient: SessionLifecycleClient? = null | ||
| set(lifecycleClient) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove the setter |
||
| field = lifecycleClient | ||
| } | ||
|
|
||
| override fun onActivityResumed(activity: Activity) { | ||
| lifecycleClient?.appForegrounded() | ||
| } | ||
|
|
||
| override fun onActivityPaused(activity: Activity) { | ||
| lifecycleClient?.appBackgrounded() | ||
| } | ||
|
|
||
| override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) = Unit | ||
|
|
||
|
|
||
87 changes: 87 additions & 0 deletions
87
...rc/main/kotlin/com/google/firebase/sessions/SessionsFallbackActivityLifecycleCallbacks.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /* | ||
| * Copyright 2025 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.firebase.sessions | ||
|
|
||
| import android.util.Log | ||
| import com.google.firebase.annotations.concurrent.Background | ||
| import com.google.firebase.sessions.api.FirebaseSessionsDependencies | ||
| import com.google.firebase.sessions.api.SessionSubscriber | ||
| import com.google.firebase.sessions.settings.SessionsSettings | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
| import kotlin.coroutines.CoroutineContext | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.launch | ||
|
|
||
| /** | ||
| * This is the fallback module for datastore implementation (SharedSessionRepository). We are | ||
| * fallback to pre multi-process support behavior. | ||
| */ | ||
| @Singleton | ||
| internal class SessionsFallbackActivityLifecycleCallbacks | ||
| @Inject | ||
| constructor( | ||
| private val sessionsSettings: SessionsSettings, | ||
| private val sessionGenerator: SessionGenerator, | ||
| private val timeProvider: TimeProvider, | ||
| @Background private val backgroundDispatcher: CoroutineContext, | ||
| ) : SessionLifecycleClient { | ||
|
|
||
| override var localSessionData: SessionData = | ||
| SessionData( | ||
| sessionDetails = sessionGenerator.generateNewSession(null), | ||
| timeProvider.currentTime() | ||
| ) | ||
|
|
||
| init { | ||
| notifySubscribers(localSessionData.sessionDetails.sessionId) | ||
| } | ||
|
|
||
| override fun appBackgrounded() { | ||
| localSessionData = localSessionData.copy(backgroundTime = timeProvider.currentTime()) | ||
| } | ||
|
|
||
| override fun appForegrounded() { | ||
| if (shouldInitiateNewSession(localSessionData)) { | ||
| val newSessionDetails = sessionGenerator.generateNewSession(localSessionData.sessionDetails) | ||
| localSessionData = localSessionData.copy(sessionDetails = newSessionDetails) | ||
| notifySubscribers(localSessionData.sessionDetails.sessionId) | ||
| } | ||
| } | ||
|
|
||
| private fun notifySubscribers(sessionId: String) { | ||
| CoroutineScope(backgroundDispatcher).launch { | ||
| // Only notify subscriber for session change, not send to event to firelog | ||
| FirebaseSessionsDependencies.getRegisteredSubscribers().values.forEach { subscriber -> | ||
| subscriber.onSessionChanged(SessionSubscriber.SessionDetails(sessionId)) | ||
| Log.d( | ||
| TAG, | ||
| "Notified ${subscriber.sessionSubscriberName} of new session $sessionId with fallback mode" | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun shouldInitiateNewSession(sessionData: SessionData): Boolean { | ||
| val interval = timeProvider.currentTime() - sessionData.backgroundTime | ||
| return interval > sessionsSettings.sessionRestartTimeout | ||
| } | ||
|
|
||
| private companion object { | ||
| const val TAG = "SessionsFallbackALC" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,17 +27,13 @@ import javax.inject.Inject | |
| import javax.inject.Singleton | ||
| import kotlin.coroutines.CoroutineContext | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.Job | ||
| import kotlinx.coroutines.cancel | ||
| import kotlinx.coroutines.flow.cancellable | ||
| import kotlinx.coroutines.launch | ||
|
|
||
| /** Repository to persist session data to be shared between all app processes. */ | ||
| internal interface SharedSessionRepository { | ||
| fun appBackground() | ||
|
|
||
| fun appForeground() | ||
| } | ||
|
|
||
| @Singleton | ||
| internal class SharedSessionRepositoryImpl | ||
| internal class SharedSessionRepository | ||
| @Inject | ||
| constructor( | ||
| private val sessionsSettings: SessionsSettings, | ||
|
|
@@ -46,26 +42,29 @@ constructor( | |
| private val timeProvider: TimeProvider, | ||
| private val sessionDataStore: DataStore<SessionData>, | ||
| @Background private val backgroundDispatcher: CoroutineContext, | ||
| ) : SharedSessionRepository { | ||
| ) : SessionLifecycleClient { | ||
| /** Local copy of the session data. Can get out of sync, must be double-checked in datastore. */ | ||
| private lateinit var localSessionData: SessionData | ||
| override lateinit var localSessionData: SessionData | ||
|
|
||
| private var jobForCancel: Job? = null | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dataFlowJob |
||
|
|
||
| init { | ||
| CoroutineScope(backgroundDispatcher).launch { | ||
| sessionDataStore.data.collect { sessionData -> | ||
| localSessionData = sessionData | ||
| val sessionId = sessionData.sessionDetails.sessionId | ||
| jobForCancel = | ||
| CoroutineScope(backgroundDispatcher).launch { | ||
| sessionDataStore.data.cancellable().collect { sessionData -> | ||
| localSessionData = sessionData | ||
| val sessionId = sessionData.sessionDetails.sessionId | ||
|
|
||
| FirebaseSessionsDependencies.getRegisteredSubscribers().values.forEach { subscriber -> | ||
| // Notify subscribers, regardless of sampling and data collection state | ||
| subscriber.onSessionChanged(SessionSubscriber.SessionDetails(sessionId)) | ||
| Log.d(TAG, "Notified ${subscriber.sessionSubscriberName} of new session $sessionId") | ||
| FirebaseSessionsDependencies.getRegisteredSubscribers().values.forEach { subscriber -> | ||
| // Notify subscribers, regardless of sampling and data collection state | ||
| subscriber.onSessionChanged(SessionSubscriber.SessionDetails(sessionId)) | ||
| Log.d(TAG, "Notified ${subscriber.sessionSubscriberName} of new session $sessionId") | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override fun appBackground() { | ||
| override fun appBackgrounded() { | ||
| if (!::localSessionData.isInitialized) { | ||
| Log.d(TAG, "App backgrounded, but local SessionData not initialized") | ||
| return | ||
|
|
@@ -81,7 +80,7 @@ constructor( | |
| } | ||
| } | ||
|
|
||
| override fun appForeground() { | ||
| override fun appForegrounded() { | ||
| if (!::localSessionData.isInitialized) { | ||
| Log.d(TAG, "App foregrounded, but local SessionData not initialized") | ||
| return | ||
|
|
@@ -105,6 +104,10 @@ constructor( | |
| } | ||
| } | ||
|
|
||
| override fun unregister() { | ||
| jobForCancel?.cancel("Datastore turned off, stop flow") | ||
| } | ||
|
|
||
| private fun shouldInitiateNewSession(sessionData: SessionData): Boolean { | ||
| val interval = timeProvider.currentTime() - sessionData.backgroundTime | ||
| return interval > sessionsSettings.sessionRestartTimeout | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var localSessionData: SessionData
private set()