Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .sampo/changesets/flag-cache-rediss-scheme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
pypi/posthog: patch
---

Accept rediss:// URLs in flag_fallback_cache_url so the flag fallback cache can use Redis over TLS.
5 changes: 3 additions & 2 deletions posthog/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4823,6 +4823,7 @@ def _initialize_flag_cache(self, cache_url):
- "memory://local/?ttl=300&size=10000": Memory cache with TTL and size
- "redis://localhost:6379/0/?ttl=300": Redis cache with TTL
- "redis://username:password@host:port/?ttl=300": Redis with auth
- "rediss://username:password@host:port/?ttl=300": Redis over TLS

Example usage:
# Memory cache
Expand Down Expand Up @@ -4858,7 +4859,7 @@ def _initialize_flag_cache(self, cache_url):
size = int(query_params.get("size", [10000])[0])
return FlagCache(size, ttl)

elif scheme == "redis":
elif scheme in ("redis", "rediss"):
try:
# Not worth importing redis if we're not using it
import redis
Expand Down Expand Up @@ -4891,7 +4892,7 @@ def _initialize_flag_cache(self, cache_url):
return None
else:
raise ValueError(
f"Unknown cache URL scheme: {scheme}. Supported schemes: memory, redis"
f"Unknown cache URL scheme: {scheme}. Supported schemes: memory, redis, rediss"
)

except Exception as e:
Expand Down
27 changes: 27 additions & 0 deletions posthog/test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from posthog.test.logging_helpers import capture_message_only_logs
from posthog.test.test_utils import FAKE_TEST_API_KEY
from posthog.types import FeatureFlag, FeatureFlagResult, LegacyFlagMetadata
from posthog.utils import RedisFlagCache
from posthog.version import VERSION
from posthog.contexts import tag

Expand Down Expand Up @@ -2589,6 +2590,32 @@ def test_shutdown_clears_feature_flag_called_dedupe_cache(self):

self.assertEqual(len(client.distinct_ids_feature_flags_reported), 0)

@parameterized.expand(
[
("redis://localhost:6379/0/?ttl=120", "redis://localhost:6379/0/"),
(
"rediss://user:[email protected]:6380/1?ttl=120",
"rediss://user:[email protected]:6380/1",
),
]
)
def test_flag_fallback_cache_url_accepts_redis_schemes(
self, cache_url, expected_redis_url
):
fake_redis = mock.Mock()
with mock.patch.dict(sys.modules, {"redis": fake_redis}):
client = Client(
FAKE_TEST_API_KEY,
send=False,
thread=0,
flag_fallback_cache_url=cache_url,
)

fake_redis.from_url.assert_called_once_with(expected_redis_url)
self.assertIsInstance(client.flag_cache, RedisFlagCache)
self.assertEqual(client.flag_cache.default_ttl, 120)
client.shutdown()

def test_shutdown_flushes_without_timeout(self):
client = Client(FAKE_TEST_API_KEY, flush_interval=0.01)

Expand Down
Loading