Skip to content

Commit

Permalink
Throttle requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Baspa committed Aug 16, 2024
1 parent e3e6cc8 commit b19fe46
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Easily configure which routes to scan, exclude or include specific checks or eve
- [Scanning routes](#scanning-routes)
- [Scanning a single route](#scanning-a-single-route)
- [Scanning routes in an SPA application](#scanning-routes-in-an-spa-application)
- [Throttling](#throttling)
- [Scan model urls](#scan-model-urls)
- [Saving scans into the database](#saving-scans-into-the-database)
- [Listening to events](#listening-to-events)
Expand Down Expand Up @@ -186,6 +187,17 @@ php artisan seo:scan-url https://vormkracht10.nl --javascript

> Note: This command will use Puppeteer to render the page. Make sure that you have Puppeteer installed on your system. You can install Puppeteer by running the following command: `npm install puppeteer`. **At this moment it's only available when scanning single routes.**
### Throttling

If you want to throttle the requests, you can set the `throttle` option to `true` in the config file. You can also set the amount of requests per minute by setting the `requests_per_minute` option in the config file.

```php
'throttle' => [
'enabled' => false,
'requests_per_minute' => 10,
],
```

### Scan model urls

When you have an application where you have a lot of pages which are related to a model, you can save the SEO score to the model. This way you can check the SEO score of a specific page and show it in your application.
Expand Down
7 changes: 6 additions & 1 deletion config/seo.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@
'vapor-ui/*',
],

'throttle' => [
'enabled' => false,
'requests_per_minute' => null,
],

/*
|--------------------------------------------------------------------------
| Domains (DNS resolving)
Expand Down Expand Up @@ -195,4 +200,4 @@
//
],
],
];
];
46 changes: 36 additions & 10 deletions src/Commands/SeoScan.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,52 @@ public function handle(): int
private function calculateScoreForRoutes(): void
{
$routes = self::getRoutes();
$throttleEnabled = config('seo.throttle.enabled');
$maxRequests = config('seo.throttle.requests_per_minute') ?? 'N/A';
$requestCount = 0;
$startTime = time();

if ($throttleEnabled) {
$this->line('<fg=yellow>Throttling enabled. Maximum requests per minute: ' . $maxRequests . '</>');
sleep(5);
}

$routes->each(function ($path, $name) {
$routes->each(function ($path, $name) use ($throttleEnabled, $maxRequests, &$requestCount, &$startTime) {
$this->progress->start();

$seo = Seo::check(url: route($name), progress: $this->progress, useJavascript: config('seo.javascript'));

$this->failed += count($seo->getFailedChecks());
$this->success += count($seo->getSuccessfulChecks());
$this->routeCount++;
if ($throttleEnabled) {

if (config('seo.database.save')) {
$this->saveScoreToDatabase(seo: $seo, url: route($name));
if ($requestCount >= $maxRequests) {
$elapsedTime = time() - $startTime;
if ($elapsedTime < 60) {
sleep(60 - $elapsedTime);
}
$requestCount = 0;
$startTime = time();
}
$requestCount++;
}

$this->performSeoCheck($name);
$this->progress->finish();

$this->logResultToConsole($seo, route($name));
});
}

private function performSeoCheck($name): void
{
$seo = Seo::check(url: route($name), progress: $this->progress, useJavascript: config('seo.javascript'));

$this->failed += count($seo->getFailedChecks());
$this->success += count($seo->getSuccessfulChecks());
$this->routeCount++;

if (config('seo.database.save')) {
$this->saveScoreToDatabase(seo: $seo, url: route($name));
}

$this->logResultToConsole($seo, route($name));
}

private static function getRoutes(): Collection
{
$routes = collect(app('router')->getRoutes()->getRoutesByName())
Expand Down

0 comments on commit b19fe46

Please sign in to comment.