From 2cbc41d29efef1805c1829cc44e5e849e7e19f18 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Mon, 11 Mar 2024 01:40:05 +0100 Subject: [PATCH] Fixed topic gc bug --- Makefile | 4 ++-- README.md | 2 +- src/Broker/TopicManager.php | 2 +- src/Http/Middleware/ResponseMiddleware.php | 1 + src/Http/Server.php | 3 +++ 5 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 5b5a49d..6973e36 100755 --- a/Makefile +++ b/Makefile @@ -13,8 +13,8 @@ phar: ## Build .phar using pharlite git clone $(REPODIR) $(BUILDDIR) && \ cd $(BUILDDIR) && \ composer install --no-dev && \ - pharlite && \ - mv $(BUILDDIR)/*.phar . && \ + pharlite + mv $(BUILDDIR)/*.phar $(REPODIR) && \ rm -rf $(BUILDDIR) .PHONY: phpstan diff --git a/README.md b/README.md index 18f3b22..1ae286b 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ $ ./mercureact.phar -c mercureact.conf * [x] Subscription/Topic manager * [x] Unify distribution * [ ] Enumerate subscriptions and topics -* [ ] Publish events +* [x] Publish events * [x] Server-Side Events distributor * [x] Distribute events over SSE * [ ] WebSocket distributor diff --git a/src/Broker/TopicManager.php b/src/Broker/TopicManager.php index 45ea6ee..2d4e41f 100644 --- a/src/Broker/TopicManager.php +++ b/src/Broker/TopicManager.php @@ -69,7 +69,7 @@ class TopicManager $this->topics, function (Topic $topic) { $topic->garbageCollect(); - return ($topic->getHistorySize() > 0 && $topic->getSubscriberCount() > 0) || ($topic->getAge() < 60); + return ($topic->getHistorySize() > 0 || $topic->getSubscriberCount() > 0) || ($topic->getAge() < 60); } ); } diff --git a/src/Http/Middleware/ResponseMiddleware.php b/src/Http/Middleware/ResponseMiddleware.php index ad19d29..c501d04 100644 --- a/src/Http/Middleware/ResponseMiddleware.php +++ b/src/Http/Middleware/ResponseMiddleware.php @@ -55,6 +55,7 @@ class ResponseMiddleware if ($t instanceof SecurityException) { return Response::plaintext("Access Denied")->withStatus(Response::STATUS_UNAUTHORIZED); } + $this->logger->warning(get_class($t).": ".$t->getMessage(), [ 'file'=>$t->getFile(), 'line'=>$t->getLine() ]); return Response::plaintext("500: Internal Server Error (".$t->getMessage().")\n")->withStatus(500); } )->then( diff --git a/src/Http/Server.php b/src/Http/Server.php index 377bc39..8f0a15c 100644 --- a/src/Http/Server.php +++ b/src/Http/Server.php @@ -54,6 +54,9 @@ class Server $this->logger = $this->createLogger(); $this->topicManager = new TopicManager(); + $this->loop->addPeriodicTimer(30, function () { + $this->topicManager->garbageCollect(); + }); $this->webSocketClients = new SplObjectStorage();