|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace LaunchDarkly\Impl\BigSegments; |
| 6 | + |
| 7 | +use DateTimeImmutable; |
| 8 | +use Exception; |
| 9 | +use LaunchDarkly\BigSegmentsEvaluationStatus; |
| 10 | +use LaunchDarkly\Impl; |
| 11 | +use LaunchDarkly\Subsystems; |
| 12 | +use LaunchDarkly\Types; |
| 13 | +use Psr\Log\LoggerInterface; |
| 14 | + |
| 15 | +class StoreManager |
| 16 | +{ |
| 17 | + private Types\BigSegmentsConfig $config; |
| 18 | + private ?Subsystems\BigSegmentsStore $store; |
| 19 | + private Impl\BigSegments\StoreStatusProvider $statusProvider; |
| 20 | + private ?Types\BigSegmentsStoreStatus $lastStatus; |
| 21 | + private ?DateTimeImmutable $lastStatusPollTime; |
| 22 | + |
| 23 | + public function __construct(Types\BigSegmentsConfig $config, private readonly LoggerInterface $logger) |
| 24 | + { |
| 25 | + $this->config = $config; |
| 26 | + $this->store = $config->store; |
| 27 | + $this->statusProvider = new Impl\BigSegments\StoreStatusProvider( |
| 28 | + fn () => $this->pollAndUpdateStatus(), |
| 29 | + $logger |
| 30 | + ); |
| 31 | + $this->lastStatus = null; |
| 32 | + $this->lastStatusPollTime = null; |
| 33 | + } |
| 34 | + |
| 35 | + public function getStatusProvider(): Subsystems\BigSegmentStatusProvider |
| 36 | + { |
| 37 | + return $this->statusProvider; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Retrieves the membership of a context key in a Big Segment, if a backing |
| 42 | + * big segments store has been configured. |
| 43 | + */ |
| 44 | + public function getContextMembership(string $contextKey): ?Impl\BigSegments\MembershipResult |
| 45 | + { |
| 46 | + if ($this->store === null) { |
| 47 | + return null; |
| 48 | + } |
| 49 | + |
| 50 | + $cachedItem = null; |
| 51 | + try { |
| 52 | + $cachedItem = $this->config->cache?->getItem($contextKey); |
| 53 | + } catch (Exception $e) { |
| 54 | + $this->logger->warning("Failed to retrieve cached item for big segment", ['contextKey' => $contextKey, 'exception' => $e->getMessage()]); |
| 55 | + } |
| 56 | + /** @var ?array<string, bool> */ |
| 57 | + $membership = $cachedItem?->get(); |
| 58 | + |
| 59 | + if ($membership === null) { |
| 60 | + try { |
| 61 | + $membership = $this->store->getMembership(StoreManager::hashForContextKey($contextKey)); |
| 62 | + if ($this->config->cache !== null && $cachedItem !== null) { |
| 63 | + $cachedItem->set($membership)->expiresAfter($this->config->contextCacheTime); |
| 64 | + |
| 65 | + if (!$this->config->cache->save($cachedItem)) { |
| 66 | + $this->logger->warning("Failed to save Big Segment membership to cache", ['contextKey' => $contextKey]); |
| 67 | + } |
| 68 | + } |
| 69 | + } catch (Exception $e) { |
| 70 | + $this->logger->warning("Failed to retrieve Big Segment membership", ['contextKey' => $contextKey, 'exception' => $e->getMessage()]); |
| 71 | + return new Impl\BigSegments\MembershipResult(null, BigSegmentsEvaluationStatus::STORE_ERROR); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + $nextPollingTime = ($this->lastStatusPollTime?->getTimestamp() ?? 0) + $this->config->statusPollInterval; |
| 76 | + |
| 77 | + $status = $this->lastStatus; |
| 78 | + if ($this->lastStatusPollTime === null || $nextPollingTime < time()) { |
| 79 | + $status = $this->pollAndUpdateStatus(); |
| 80 | + } |
| 81 | + |
| 82 | + if ($status === null || !$status->isAvailable()) { |
| 83 | + return new Impl\BigSegments\MembershipResult($membership, BigSegmentsEvaluationStatus::STORE_ERROR); |
| 84 | + } |
| 85 | + |
| 86 | + return new Impl\BigSegments\MembershipResult($membership, $status->isStale() ? BigSegmentsEvaluationStatus::STALE : BigSegmentsEvaluationStatus::HEALTHY); |
| 87 | + } |
| 88 | + |
| 89 | + private function pollAndUpdateStatus(): Types\BigSegmentsStoreStatus |
| 90 | + { |
| 91 | + $newStatus = new Types\BigSegmentsStoreStatus(false, false); |
| 92 | + if ($this->store !== null) { |
| 93 | + try { |
| 94 | + $metadata = $this->store->getMetadata(); |
| 95 | + $newStatus = new Types\BigSegmentsStoreStatus( |
| 96 | + available: true, |
| 97 | + stale: $metadata->isStale($this->config->staleAfter) |
| 98 | + ); |
| 99 | + } catch (Exception $e) { |
| 100 | + $this->logger->warning("Failed to retrieve Big Segment metadata", ['exception' => $e->getMessage()]); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + $this->lastStatus = $newStatus; |
| 105 | + $this->statusProvider->updateStatus($newStatus); |
| 106 | + $this->lastStatusPollTime = new DateTimeImmutable(); |
| 107 | + |
| 108 | + return $newStatus; |
| 109 | + } |
| 110 | + |
| 111 | + private static function hashForContextKey(string $contextKey): string |
| 112 | + { |
| 113 | + return base64_encode(hash('sha256', $contextKey, true)); |
| 114 | + } |
| 115 | +} |
0 commit comments