diff --git a/sdk/src/main/java/com/amplitude/experiment/DefaultExperimentClient.kt b/sdk/src/main/java/com/amplitude/experiment/DefaultExperimentClient.kt index 81935ea..c58dca3 100644 --- a/sdk/src/main/java/com/amplitude/experiment/DefaultExperimentClient.kt +++ b/sdk/src/main/java/com/amplitude/experiment/DefaultExperimentClient.kt @@ -46,7 +46,7 @@ import com.amplitude.experiment.analytics.ExposureEvent as OldExposureEvent private const val EU_SERVER_URL = "https://api.lab.eu.amplitude.com/" private const val EU_FLAGS_SERVER_URL = "https://flag.lab.eu.amplitude.com/" -private const val FLAG_POLLER_INTERVAL_MILLIS: Long = 60000 +private const val MIN_FLAG_POLLING_INTERVAL_MILLIS: Long = 60000 internal class DefaultExperimentClient internal constructor( private val apiKey: String, @@ -96,7 +96,19 @@ internal class DefaultExperimentClient internal constructor( scalar = 1.5f, ) - private val poller: Poller = Poller(this.executorService, ::doFlags, FLAG_POLLER_INTERVAL_MILLIS) + internal val flagConfigPollingIntervalMillis = + if (config.flagConfigPollingIntervalMillis < MIN_FLAG_POLLING_INTERVAL_MILLIS) { + MIN_FLAG_POLLING_INTERVAL_MILLIS + } else { + config.flagConfigPollingIntervalMillis + } + + private val poller: Poller = + Poller( + this.executorService, + ::doFlags, + flagConfigPollingIntervalMillis, + ) internal val serverUrl: HttpUrl = if (config.serverUrl == ExperimentConfig.Defaults.SERVER_URL && diff --git a/sdk/src/main/java/com/amplitude/experiment/ExperimentConfig.kt b/sdk/src/main/java/com/amplitude/experiment/ExperimentConfig.kt index c64e458..31ecc83 100644 --- a/sdk/src/main/java/com/amplitude/experiment/ExperimentConfig.kt +++ b/sdk/src/main/java/com/amplitude/experiment/ExperimentConfig.kt @@ -46,6 +46,8 @@ class ExperimentConfig internal constructor( @JvmField val pollOnStart: Boolean = Defaults.POLL_ON_START, @JvmField + val flagConfigPollingIntervalMillis: Long = Defaults.FLAG_CONFIG_POLLING_INTERVAL_MILLIS, + @JvmField val fetchOnStart: Boolean = Defaults.FETCH_ON_START, @JvmField val automaticFetchOnAmplitudeIdentityChange: Boolean = Defaults.AUTOMATIC_FETCH_ON_AMPLITUDE_IDENTITY_CHANGE, @@ -131,6 +133,11 @@ class ExperimentConfig internal constructor( */ const val POLL_ON_START = true + /** + * 300000 + */ + const val FLAG_CONFIG_POLLING_INTERVAL_MILLIS = 300000L + /** * null */ @@ -179,6 +186,7 @@ class ExperimentConfig internal constructor( private var retryFetchOnFailure = Defaults.RETRY_FETCH_ON_FAILURE private var automaticExposureTracking = Defaults.AUTOMATIC_EXPOSURE_TRACKING private var pollOnStart = Defaults.POLL_ON_START + private var flagConfigPollingIntervalMillis = Defaults.FLAG_CONFIG_POLLING_INTERVAL_MILLIS private var fetchOnStart = Defaults.FETCH_ON_START private var automaticFetchOnAmplitudeIdentityChange = Defaults.AUTOMATIC_FETCH_ON_AMPLITUDE_IDENTITY_CHANGE private var userProvider = Defaults.USER_PROVIDER @@ -250,6 +258,11 @@ class ExperimentConfig internal constructor( this.pollOnStart = pollOnStart } + fun flagConfigPollingIntervalMillis(flagConfigPollingIntervalMillis: Long) = + apply { + this.flagConfigPollingIntervalMillis = flagConfigPollingIntervalMillis + } + fun fetchOnStart(fetchOnStart: Boolean?) = apply { this.fetchOnStart = fetchOnStart ?: true @@ -291,6 +304,7 @@ class ExperimentConfig internal constructor( retryFetchOnFailure = retryFetchOnFailure, automaticExposureTracking = automaticExposureTracking, pollOnStart = pollOnStart, + flagConfigPollingIntervalMillis = flagConfigPollingIntervalMillis, fetchOnStart = fetchOnStart, automaticFetchOnAmplitudeIdentityChange = automaticFetchOnAmplitudeIdentityChange, userProvider = userProvider, @@ -315,6 +329,7 @@ class ExperimentConfig internal constructor( .retryFetchOnFailure(retryFetchOnFailure) .automaticExposureTracking(automaticExposureTracking) .pollOnStart(pollOnStart) + .flagConfigPollingIntervalMillis(flagConfigPollingIntervalMillis) .fetchOnStart(fetchOnStart) .automaticFetchOnAmplitudeIdentityChange((automaticFetchOnAmplitudeIdentityChange)) .userProvider(userProvider) diff --git a/sdk/src/test/java/com/amplitude/experiment/ExperimentClientTest.kt b/sdk/src/test/java/com/amplitude/experiment/ExperimentClientTest.kt index 906ea4e..c83c2cc 100644 --- a/sdk/src/test/java/com/amplitude/experiment/ExperimentClientTest.kt +++ b/sdk/src/test/java/com/amplitude/experiment/ExperimentClientTest.kt @@ -1295,4 +1295,47 @@ class ExperimentClientTest { verify(exactly = retryCalled) { client["startRetries"](any(), any()) } } } + + @Test + fun `test flag config polling interval, config not set`() { + val client = + DefaultExperimentClient( + API_KEY, + ExperimentConfig(), + OkHttpClient(), + mockStorage, + Experiment.executorService, + ) + Assert.assertEquals(300000, client.flagConfigPollingIntervalMillis) + } + + @Test + fun `test flag config polling interval, config set under min`() { + val client = + DefaultExperimentClient( + API_KEY, + ExperimentConfig( + flagConfigPollingIntervalMillis = 1000, + ), + OkHttpClient(), + mockStorage, + Experiment.executorService, + ) + Assert.assertEquals(60000, client.flagConfigPollingIntervalMillis) + } + + @Test + fun `test flag config polling interval, config set over min`() { + val client = + DefaultExperimentClient( + API_KEY, + ExperimentConfig( + flagConfigPollingIntervalMillis = 900000, + ), + OkHttpClient(), + mockStorage, + Experiment.executorService, + ) + Assert.assertEquals(900000, client.flagConfigPollingIntervalMillis) + } }