-
Notifications
You must be signed in to change notification settings - Fork 626
Add SessionMaintainerFollower
and create in FirebaseSessions
when…
#5368
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: session-maintainer
Are you sure you want to change the base?
Changes from 3 commits
b9f0e1d
57e01ea
c8f87ae
b9937d5
1e04fe1
d2b5ddb
b3945b9
5c4ceee
2300e61
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 2023 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.follower | ||
|
||
import android.util.Log | ||
import com.google.firebase.sessions.SessionMaintainer | ||
import com.google.firebase.sessions.api.FirebaseSessionsDependencies | ||
import com.google.firebase.sessions.api.SessionSubscriber | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
|
||
class SessionMaintainerFollower(private val sessionsDataRepository: SessionsDataRepository) : | ||
SessionMaintainer { | ||
val tag = "SessionMaintainerFollow" | ||
|
||
override fun register(subscriber: SessionSubscriber) { | ||
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. You can say 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. Is a no-op the right thing to do for followers? Should they still notify the subscribers on register? Perf relies on the AQS session id being set before the call to 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. Okay. We can force a lookup. 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. actually, I'm unsure. We notify subscribers on init, and |
||
// NOOP | ||
} | ||
|
||
override fun start(backgroundDispatcher: CoroutineDispatcher) { | ||
CoroutineScope(backgroundDispatcher).launch { | ||
sessionsDataRepository.firebaseSessionDataFlow.collect { | ||
if (it.sessionId == null) { | ||
Log.d( | ||
tag, | ||
"No session data available in shared storage." + " subscribers will not be notified." | ||
) | ||
} else { | ||
Log.d( | ||
tag, | ||
"Follower process has observed a change to the repository and will notify subscribers. New session id is ${it.sessionId}" | ||
) | ||
notifySubscribers(it.sessionId) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private suspend fun notifySubscribers(sessionId: String) { | ||
val subscribers = FirebaseSessionsDependencies.getRegisteredSubscribers() | ||
if (subscribers.isEmpty()) { | ||
Log.d(tag, "Sessions SDK did not have any subscribers. Events will not be sent.") | ||
} else { | ||
subscribers.values.forEach { subscriber -> | ||
// Notify subscribers, irregardless ;) of sampling and data collection state. | ||
Log.d(tag, "Sending session id $sessionId to subscriber $subscriber.") | ||
subscriber.onSessionChanged(SessionSubscriber.SessionDetails(sessionId)) | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2023 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.follower | ||
|
||
import android.content.Context | ||
import android.util.Log | ||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.edit | ||
import androidx.datastore.preferences.core.emptyPreferences | ||
import androidx.datastore.preferences.core.stringPreferencesKey | ||
import androidx.datastore.preferences.preferencesDataStore | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.catch | ||
import kotlinx.coroutines.flow.map | ||
|
||
data class FirebaseSessionsData(val sessionId: String?) | ||
|
||
/** Persists session data that needs to be synchronized across processes */ | ||
class SessionsDataRepository(private val context: Context) { | ||
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. These classes should all be 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. 👍 |
||
private val tag = "FirebaseSessionsRepo" | ||
|
||
private object FirebaseSessionDataKeys { | ||
val SESSION_ID = stringPreferencesKey("session_id") | ||
} | ||
|
||
val firebaseSessionDataFlow: Flow<FirebaseSessionsData> = | ||
context.dataStore.data | ||
.catch { exception -> | ||
Log.e(tag, "Error reading stored session data.", exception) | ||
emit(emptyPreferences()) | ||
} | ||
.map { preferences -> mapSessionsData(preferences) } | ||
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. Can this be something like 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. that didn't work |
||
|
||
suspend fun updateSessionId(sessionId: String) { | ||
context.dataStore.edit { preferences -> | ||
preferences[FirebaseSessionDataKeys.SESSION_ID] = sessionId | ||
} | ||
} | ||
|
||
private fun mapSessionsData(preferences: Preferences): FirebaseSessionsData = | ||
FirebaseSessionsData(preferences[FirebaseSessionDataKeys.SESSION_ID]) | ||
} | ||
|
||
const val SESSION_CONFIGS_NAME = "firebase_session_settings" | ||
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. Putting top level constants directly in the file will cause the Firebase API check to complain, because it will generate a public class to put this in for Java. Maybe this and the FirebaseSessionDataKeys can go in a companion object instead. |
||
|
||
private val Context.dataStore: DataStore<Preferences> by | ||
preferencesDataStore(name = SESSION_CONFIGS_NAME) |
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.
I think this should go back to
runningAppProcessInfo.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND
becauseIMPORTANCE_FOREGROUND_SERVICE
is only available after a specific api level, and it'll make our logic complicated to also count services as leaders.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.
the one integration test fails if we don't consider foreground services to be foreground processes https://github.com/firebase/firebase-android-sdk/blob/master/firebase-sessions/test-app/src/androidTest/kotlin/com/google/firebase/testing/sessions/FirebaseSessionsTest.kt
That suggests to me that we may need it. Thoughts?