From badf0dd57f6c14a444a59caa5d33b4af4164e223 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 28 Sep 2023 08:04:52 +0000 Subject: [PATCH 1/2] There is now a time limit when fetching AP endpoints --- src/Core/Worker.php | 4 ++-- src/Protocol/ActivityPub.php | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/Core/Worker.php b/src/Core/Worker.php index 7075b4b23..c84b59e2b 100644 --- a/src/Core/Worker.php +++ b/src/Core/Worker.php @@ -601,7 +601,7 @@ class Worker $rest = round(max(0, $up_duration - (self::$db_duration + self::$lock_duration)), 2); $exec = round($duration, 2); - Logger::info('Performance:', ['state' => self::$state, 'count' => $dbcount, 'stat' => $dbstat, 'write' => $dbwrite, 'lock' => $dblock, 'total' => $dbtotal, 'rest' => $rest, 'exec' => $exec]); + Logger::info('Performance:', ['function' => $funcname, 'state' => self::$state, 'count' => $dbcount, 'stat' => $dbstat, 'write' => $dbwrite, 'lock' => $dblock, 'total' => $dbtotal, 'rest' => $rest, 'exec' => $exec]); self::coolDown(); @@ -622,7 +622,7 @@ class Worker Logger::info('Longer than 2 minutes.', ['priority' => $queue['priority'], 'id' => $queue['id'], 'duration' => round($duration/60, 3)]); } - Logger::info('Process done.', ['priority' => $queue['priority'], 'id' => $queue['id'], 'duration' => round($duration, 3)]); + Logger::info('Process done.', ['function' => $funcname, 'priority' => $queue['priority'], 'retrial' => $queue['retrial'], 'id' => $queue['id'], 'duration' => round($duration, 3)]); DI::profiler()->saveLog(DI::logger(), 'ID ' . $queue['id'] . ': ' . $funcname); } diff --git a/src/Protocol/ActivityPub.php b/src/Protocol/ActivityPub.php index 4be88b341..11359b339 100644 --- a/src/Protocol/ActivityPub.php +++ b/src/Protocol/ActivityPub.php @@ -241,12 +241,20 @@ class ActivityPub /** * Fetch items from AP endpoints * - * @param string $url Address of the endpoint - * @param integer $uid Optional user id + * @param string $url Address of the endpoint + * @param integer $uid Optional user id + * @param integer $timestamp Internally used parameter to stop fetching after some time * @return array Endpoint items */ - public static function fetchItems(string $url, int $uid = 0): array + public static function fetchItems(string $url, int $uid = 0, int $timestamp = 0): array { + $timestamp = $timestamp ?: time(); + + if ((time() - $timestamp) > 60) { + Logger::info('Fetch time limit reached', ['url' => $url, 'uid' => $uid]); + return []; + } + $data = self::fetchContent($url, $uid); if (empty($data)) { return []; @@ -257,13 +265,13 @@ class ActivityPub } elseif (!empty($data['first']['orderedItems'])) { $items = $data['first']['orderedItems']; } elseif (!empty($data['first']) && is_string($data['first']) && ($data['first'] != $url)) { - return self::fetchItems($data['first'], $uid); + return self::fetchItems($data['first'], $uid, $timestamp); } else { return []; } if (!empty($data['next']) && is_string($data['next'])) { - $items = array_merge($items, self::fetchItems($data['next'], $uid)); + $items = array_merge($items, self::fetchItems($data['next'], $uid, $timestamp)); } return $items; From b54005c540e5fe1bca53e363a57edc93762ea3c7 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 29 Sep 2023 04:50:36 +0000 Subject: [PATCH 2/2] Changed parameter name --- src/Protocol/ActivityPub.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Protocol/ActivityPub.php b/src/Protocol/ActivityPub.php index 11359b339..522a874fe 100644 --- a/src/Protocol/ActivityPub.php +++ b/src/Protocol/ActivityPub.php @@ -241,16 +241,16 @@ class ActivityPub /** * Fetch items from AP endpoints * - * @param string $url Address of the endpoint - * @param integer $uid Optional user id - * @param integer $timestamp Internally used parameter to stop fetching after some time + * @param string $url Address of the endpoint + * @param integer $uid Optional user id + * @param integer $start_timestamp Internally used parameter to stop fetching after some time * @return array Endpoint items */ - public static function fetchItems(string $url, int $uid = 0, int $timestamp = 0): array + public static function fetchItems(string $url, int $uid = 0, int $start_timestamp = 0): array { - $timestamp = $timestamp ?: time(); + $start_timestamp = $start_timestamp ?: time(); - if ((time() - $timestamp) > 60) { + if ((time() - $start_timestamp) > 60) { Logger::info('Fetch time limit reached', ['url' => $url, 'uid' => $uid]); return []; } @@ -265,13 +265,13 @@ class ActivityPub } elseif (!empty($data['first']['orderedItems'])) { $items = $data['first']['orderedItems']; } elseif (!empty($data['first']) && is_string($data['first']) && ($data['first'] != $url)) { - return self::fetchItems($data['first'], $uid, $timestamp); + return self::fetchItems($data['first'], $uid, $start_timestamp); } else { return []; } if (!empty($data['next']) && is_string($data['next'])) { - $items = array_merge($items, self::fetchItems($data['next'], $uid, $timestamp)); + $items = array_merge($items, self::fetchItems($data['next'], $uid, $start_timestamp)); } return $items;