Skip to content

Commit

Permalink
feat: increase flag polling interval; add flag poller interval config (
Browse files Browse the repository at this point in the history
  • Loading branch information
bgiori authored Nov 25, 2024
1 parent f1ee01d commit 214c7d3
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 &&
Expand Down
15 changes: 15 additions & 0 deletions sdk/src/main/java/com/amplitude/experiment/ExperimentConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -131,6 +133,11 @@ class ExperimentConfig internal constructor(
*/
const val POLL_ON_START = true

/**
* 300000
*/
const val FLAG_CONFIG_POLLING_INTERVAL_MILLIS = 300000L

/**
* null
*/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -291,6 +304,7 @@ class ExperimentConfig internal constructor(
retryFetchOnFailure = retryFetchOnFailure,
automaticExposureTracking = automaticExposureTracking,
pollOnStart = pollOnStart,
flagConfigPollingIntervalMillis = flagConfigPollingIntervalMillis,
fetchOnStart = fetchOnStart,
automaticFetchOnAmplitudeIdentityChange = automaticFetchOnAmplitudeIdentityChange,
userProvider = userProvider,
Expand All @@ -315,6 +329,7 @@ class ExperimentConfig internal constructor(
.retryFetchOnFailure(retryFetchOnFailure)
.automaticExposureTracking(automaticExposureTracking)
.pollOnStart(pollOnStart)
.flagConfigPollingIntervalMillis(flagConfigPollingIntervalMillis)
.fetchOnStart(fetchOnStart)
.automaticFetchOnAmplitudeIdentityChange((automaticFetchOnAmplitudeIdentityChange))
.userProvider(userProvider)
Expand Down
43 changes: 43 additions & 0 deletions sdk/src/test/java/com/amplitude/experiment/ExperimentClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1295,4 +1295,47 @@ class ExperimentClientTest {
verify(exactly = retryCalled) { client["startRetries"](any<ExperimentUser>(), any<FetchOptions>()) }
}
}

@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)
}
}

0 comments on commit 214c7d3

Please sign in to comment.