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

Fix: race condition in caching layer #218

Merged
merged 1 commit into from
Jul 24, 2024

Conversation

sighphyre
Copy link
Member

@sighphyre sighphyre commented Jul 23, 2024

Description

This fixes a bit of a subtle bug in the caching layer where the SDK can start to return empty sets of flags if the cache is invalidated between checking the cache status and doing the actual retrieve. This forces the read from cache to be atomic.

Fixes # (issue)

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Custom script - spin up 50 processes each running their own SDK, backed by Redis all checking a feature that should always be enabled. This typically produces a false result in 10-20 seconds. Apply fix rerun script for 20 minutes.

  • Integration tests / Manual Tests

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@sighphyre sighphyre marked this pull request as ready for review July 23, 2024 13:29
@sighphyre sighphyre requested review from RikudouSage and chriswk July 23, 2024 13:29
@@ -140,11 +140,10 @@ private function getCachedFeatures(): ?array
{
$cache = $this->configuration->getCache();

if (!$cache->has(CacheKey::FEATURES)) {
$result = $cache->get(CacheKey::FEATURES);
if ($result === null) {

Choose a reason for hiding this comment

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

Could this $results from cache could have empty arrays like []?

Copy link
Member Author

Choose a reason for hiding this comment

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

In the previous iteration yeah, in this, that shouldn't happen unless something puts an empty array in there. Assuming no bugs are present in the code, that should only ever happen if the SDK is using a token that's connected to a project that has no features

Copy link
Collaborator

@RikudouSage RikudouSage left a comment

Choose a reason for hiding this comment

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

Looks good!

@sighphyre sighphyre merged commit ca61d32 into main Jul 24, 2024
13 checks passed
@sighphyre sighphyre deleted the fix/race-condition-in-cache-load branch July 24, 2024 06:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants