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

feat: check connection availability before fetching sourceConfig #103

Merged

Conversation

1abhishekpandey
Copy link
Contributor

@1abhishekpandey 1abhishekpandey commented Jan 14, 2025

Description

  • In this PR, we’ve added logic to check and wait for the connection to be available before attempting to fetch the sourceConfig. This ensures that the sourceConfig is not fetched until the network is available.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Code refactor/optimization

Implementation Details

SourceConfigManager

  • First, we will fetch the cached sourceConfig from storage, if available, and immediately notify the observers with the cached sourceConfig.
  • Then, we will refresh the sourceConfig by downloading the latest version, store it, and notify all observers.
  • Added test cases: Rewrote them from scratch.

Core Analytics

  • Refactored the setup portion of the core Analytics

LoggerAnalytics

  • Improved LoggerAnalytics by handling edge cases. I noticed that if it’s not initialized early in the lifecycle and a message is logged, it throws a fatal exception. Now, in such cases, we simply ignore any logging requests.

Checklist

  • I have added tests that prove my fix is effective or that my feature works.
  • I have added the necessary documentation (if appropriate).
  • I have ensured that my code follows the project's code style.
  • I have checked for potential performance impacts and optimized if necessary.
  • I have checked the code for security issues.
  • I have updated the changelog (if required).

How to test?

  • Run the Android app and toggle on the connection. The SC will be immediately fetched, as soon as the connection becomes available.
  • Run the Android app on different SDK API versions i.e., compatible SDK version - API 24 and above and legacy once API 21 - 23.
  • Run the core module, and an attempt will be made to fetch the sourceConfig immediately, as we assume (or the default behaviour is) that the network is always available in core SDKs.
  • Try to enable and disable connection multiple times, but sourceConfig will only be fetched once.

Log:

SourceConfig is fetched successfully: SourceConfig(source=RudderServerConfigSource(sourceId=......

Breaking Changes

Maintainers Checklist

  • The code has been reviewed.
  • CI tests have passed.
  • All necessary documentation has been updated.

Screenshots (if applicable)

Additional Context

This is to ensure that we don't need to do un-necessary casting of application object
There are few refactors to improve the readability and maintainability of the code.
@1abhishekpandey 1abhishekpandey self-assigned this Jan 14, 2025
@1abhishekpandey 1abhishekpandey marked this pull request as ready for review January 14, 2025 22:11
Copy link
Collaborator

@ChryssaAliferi ChryssaAliferi left a comment

Choose a reason for hiding this comment

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

Great effort!! 🚀

@@ -15,6 +15,7 @@ import org.jetbrains.annotations.VisibleForTesting
*
* @property source The configuration details of a RudderStack source.
*/
@Suppress("MaximumLineLength")
Copy link
Contributor

Choose a reason for hiding this comment

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

NIT: We don't need to suppress MaximumLineLength rule here. We can have updatedSourceConfig of NotifyObserversAction on a new line. For some reason, detekt fails to autocorrect it.

Comment on lines +15 to +21
fun Analytics.notifyOnlyOnceOnConnectionAvailable(block: suspend () -> Unit) {
this.analyticsScope.launch {
[email protected]
.filter { it }
.take(ONLY_ONE_ELEMENT)
.collect { block() }
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice!

@Before
fun setUp() {
Dispatchers.setMain(testDispatcher)
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove the Dispatchers.resetMain() from teardown as well.

@@ -34,7 +35,7 @@ internal data class SourceConfig(
)
}

class UpdateAction(@VisibleForTesting internal val updatedSourceConfig: SourceConfig) : FlowAction<SourceConfig> {
class NotifyObserversAction(@VisibleForTesting internal val updatedSourceConfig: SourceConfig) : FlowAction<SourceConfig> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion: IMO we should not call it NotifyObserversAction because notifying the observers is the inherent property of a MutableStateFlow and it is not the responsibility of this specific action. This action's responsibility is to just update (or set) the sourceConfig state. Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree. Thanks for the clarification.

Comment on lines +88 to +93
private fun runForBaseTypeOnly() {
if (this::class == Analytics::class) {
setLogger(logger = KotlinLogger())
connectivityState.dispatch(ConnectivityState.SetDefaultStateAction())
setupSourceConfig()
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Great job in detecting this subtle error about base and parent class!

Base automatically changed from feat/sdk-2850-implementation-network-observer-using-plugin to develop January 17, 2025 05:49
@1abhishekpandey 1abhishekpandey merged commit fdbe891 into develop Jan 17, 2025
2 checks passed
@1abhishekpandey 1abhishekpandey deleted the feat/sdk-2849-improved-implement-of-source-config-logic branch January 17, 2025 09:03
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.

3 participants