fix some issue from sse
This commit is contained in:
@@ -2,8 +2,10 @@
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Contracts\Cache\Repository;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class PriceStreamService
|
||||
{
|
||||
@@ -99,6 +101,31 @@ class PriceStreamService
|
||||
$this->publishToInbox($this->customerInboxKey($customerId), $event, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inbox باید بین tick (CLI) و SSE (PHP-FPM) مشترک باشد.
|
||||
* file cache روی چند ورکر/سرور inbox را جدا میکند → فقط ping دیده میشود.
|
||||
*/
|
||||
private function store(): Repository
|
||||
{
|
||||
static $store = null;
|
||||
if ($store instanceof Repository) {
|
||||
return $store;
|
||||
}
|
||||
|
||||
try {
|
||||
$redis = Cache::store('redis');
|
||||
$redis->get('price_sse_inbox:ping');
|
||||
$store = $redis;
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning('PriceStreamService: redis unavailable, falling back to default cache', [
|
||||
'message' => $e->getMessage(),
|
||||
]);
|
||||
$store = Cache::store();
|
||||
}
|
||||
|
||||
return $store;
|
||||
}
|
||||
|
||||
private function inboxKey(): string
|
||||
{
|
||||
return self::INBOX_PREFIX.'global';
|
||||
@@ -111,7 +138,7 @@ class PriceStreamService
|
||||
|
||||
private function currentInboxSeq(string $key): int
|
||||
{
|
||||
$inbox = Cache::get($key, ['seq' => 0, 'events' => []]);
|
||||
$inbox = $this->store()->get($key, ['seq' => 0, 'events' => []]);
|
||||
|
||||
return (int) ($inbox['seq'] ?? 0);
|
||||
}
|
||||
@@ -121,7 +148,7 @@ class PriceStreamService
|
||||
*/
|
||||
private function consumeInboxSince(string $key, int $afterId): array
|
||||
{
|
||||
$inbox = Cache::get($key, ['seq' => 0, 'events' => []]);
|
||||
$inbox = $this->store()->get($key, ['seq' => 0, 'events' => []]);
|
||||
$events = is_array($inbox['events'] ?? null) ? $inbox['events'] : [];
|
||||
|
||||
return array_values(array_filter(
|
||||
@@ -135,8 +162,10 @@ class PriceStreamService
|
||||
*/
|
||||
private function publishToInbox(string $key, string $event, array $data): void
|
||||
{
|
||||
Cache::lock($key.':lock', 5)->block(3, function () use ($key, $event, $data): void {
|
||||
$inbox = Cache::get($key, ['seq' => 0, 'events' => []]);
|
||||
$store = $this->store();
|
||||
|
||||
$store->lock($key.':lock', 5)->block(3, function () use ($store, $key, $event, $data): void {
|
||||
$inbox = $store->get($key, ['seq' => 0, 'events' => []]);
|
||||
$events = is_array($inbox['events'] ?? null) ? $inbox['events'] : [];
|
||||
$lastEvent = $events !== [] ? $events[array_key_last($events)] : null;
|
||||
|
||||
@@ -157,7 +186,7 @@ class PriceStreamService
|
||||
$inbox['events'] = array_slice($inbox['events'], -self::MAX_EVENTS);
|
||||
}
|
||||
|
||||
Cache::put($key, $inbox, now()->addSeconds(self::INBOX_TTL_SECONDS));
|
||||
$store->put($key, $inbox, now()->addSeconds(self::INBOX_TTL_SECONDS));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user