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: Add initial delay for polling feature configuration #2257

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .sonarcloud.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ sonar.organization=thomaspoignant
sonar.projectKey=thomaspoignant_go-feature-flag
sonar.projectName=GO Feature Flag GitHub action
sonar.sources=.
sonar.exclusions=**/*_test.go, examples/**, openfeature/provider_tests/**, **/test_*.py, openfeature/providers/kotlin-provider/gofeatureflag=kotlin-provider/src/test/**, cmd/relayproxy/helm-charts/**
sonar.exclusions=**/*_test.go, examples/**, openfeature/provider_tests/**, **/test_*.py, openfeature/providers/kotlin-provider/gofeatureflag-kotlin-provider/src/test/**, cmd/relayproxy/helm-charts/**
Copy link
Owner

Choose a reason for hiding this comment

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

Oh, good catch 👌
I want to fast here 🤦

sonar.tests=.
sonar.test.inclusions=**/*_test.go, openfeature/provider_tests/**
sonar.go.coverage.reportPaths=coverage.cov
1 change: 1 addition & 0 deletions openfeature/providers/kotlin-provider/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ When initializing the provider, you can pass some options to configure the provi
| `apiKey` | String | | (optional) If GO Feature Flag is configured to authenticate the requests, you should provide an API Key to the provider. Please ask the administrator of the relay proxy to provide an API Key. |
| `pollingIntervalInMillis` | Long | 300000 | (optional) Polling interval in millisecond to check with GO Feature Flag relay proxy if there is a flag configuration change. |
| `flushIntervalMs` | Long | 1000 | (optional) Time to wait before calling GO Feature Flag to store all the data about the evaluation in the relay proxy. |
| `pollingDelayInMillis` | Long | | (optional) Polling delay in millisecond for initial check with GO Feature Flag relay proxy. If null takes [pollingIntervalInMillis] |

### Update the Evaluation Context

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class GoFeatureFlagProvider(private val options: GoFeatureFlagOptions) : Feature
keepAliveDuration = options.keepAliveDuration,
headers = authorizationHeader,
pollingIntervalInMillis = options.pollingIntervalInMillis,
pollingDelayInMillis = options.pollingDelayInMillis,
)
this.ofrepProvider = OfrepProvider(ofrepOptions)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ data class GoFeatureFlagOptions(
*/
val pollingIntervalInMillis: Long = 300000,

/**
* (optional) initial delay for polling in millisecond to refresh the flags
* Default: Used from [pollingIntervalInMillis]
*/
val pollingDelayInMillis: Long? = null,

/**
* (optional) interval time we publish statistics collection data to the proxy.
* The parameter is used only if the cache is enabled, otherwise the collection of the data is done directly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,16 @@ class OfrepProvider(
}
}
}
this.startPolling(this.ofrepOptions.pollingIntervalInMillis)
this.startPolling(
pollingIntervalInMillis = this.ofrepOptions.pollingIntervalInMillis,
initialPollingDelayInMillis = this.ofrepOptions.pollingDelayInMillis
)
}

/**
* Start polling for flag updates
*/
private fun startPolling(pollingIntervalInMillis: Long) {
private fun startPolling(pollingIntervalInMillis: Long, initialPollingDelayInMillis: Long?) {
val task: TimerTask = object : TimerTask() {
override fun run() {
runBlocking {
Expand Down Expand Up @@ -113,7 +116,11 @@ class OfrepProvider(
}
}
val timer = Timer()
timer.schedule(task, pollingIntervalInMillis, pollingIntervalInMillis)
timer.schedule(
/* task = */ task,
/* delay = */ initialPollingDelayInMillis ?: pollingIntervalInMillis,
/* period = */ pollingIntervalInMillis
)
this.pollingTimer = timer
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,11 @@ data class OfrepOptions(
* (optional) polling interval in millisecond to refresh the flags
* Default: 300000 (5 minutes)
*/
val pollingIntervalInMillis: Long = 300000
val pollingIntervalInMillis: Long = 300000,

/**
* (optional) initial delay for polling in millisecond to refresh the flags
* Default: Used from [pollingIntervalInMillis]
*/
val pollingDelayInMillis: Long? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,83 @@ class OfrepProviderTest {
assertEquals(want2, got2)
}

@Test
fun `should collect both responses if interval is longer then initial wait`(): Unit = runBlocking {
enqueueMockResponse(
"org.gofeatureflag.openfeature.ofrep/valid_api_short_response.json",
200
)
enqueueMockResponse(
"org.gofeatureflag.openfeature.ofrep/valid_api_response_2.json",
200
)

val provider = OfrepProvider(
OfrepOptions(
pollingIntervalInMillis = 1500,
pollingDelayInMillis = 100,
endpoint = mockWebServer?.url("/").toString()
)
)
OpenFeatureAPI.setProviderAndWait(provider, Dispatchers.IO, defaultEvalCtx)
val client = OpenFeatureAPI.getClient()
val got = client.getStringDetails("badge-class2", "default")
val want = FlagEvaluationDetails<String>(
flagKey = "badge-class2",
value = "green",
variant = "nocolor",
reason = "DEFAULT",
errorCode = null,
errorMessage = null,
)
assertEquals(want, got)
Thread.sleep(1000)
val got2 = client.getStringDetails("badge-class2", "default")
val want2 = FlagEvaluationDetails<String>(
flagKey = "badge-class2",
value = "blue",
variant = "xxxx",
reason = "TARGETING_MATCH",
errorCode = null,
errorMessage = null,
)
assertEquals(want2, got2)
}

@Test
fun `should collect first responses if delay is not set and interval is longer than wait`(): Unit = runBlocking {
enqueueMockResponse(
"org.gofeatureflag.openfeature.ofrep/valid_api_short_response.json",
200
)
enqueueMockResponse(
"org.gofeatureflag.openfeature.ofrep/valid_api_response_2.json",
200
)

val provider = OfrepProvider(
OfrepOptions(
pollingIntervalInMillis = 1500,
endpoint = mockWebServer?.url("/").toString()
)
)
OpenFeatureAPI.setProviderAndWait(provider, Dispatchers.IO, defaultEvalCtx)
val client = OpenFeatureAPI.getClient()
val got = client.getStringDetails("badge-class2", "default")
val want = FlagEvaluationDetails<String>(
flagKey = "badge-class2",
value = "green",
variant = "nocolor",
reason = "DEFAULT",
errorCode = null,
errorMessage = null,
)
assertEquals(want, got)
Thread.sleep(1000)
val got2 = client.getStringDetails("badge-class2", "default")
assertEquals(want, got2)
}

private fun enqueueMockResponse(
fileName: String,
responseCode: Int = 200,
Expand Down
Loading