1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-13 22:54:25 +01:00

Updates for PRedis

This commit is contained in:
David Bomba 2024-08-04 18:22:00 +10:00
parent 2214455364
commit 0edb03943b
5 changed files with 45 additions and 35 deletions

View File

@ -4,15 +4,15 @@ namespace App\Http\Middleware;
use Closure; use Closure;
use Illuminate\Cache\RateLimiter; use Illuminate\Cache\RateLimiter;
use Illuminate\Contracts\Redis\Factory as Redis;
use Illuminate\Redis\Limiters\DurationLimiter; use Illuminate\Redis\Limiters\DurationLimiter;
use Illuminate\Routing\Middleware\ThrottleRequests;
class ThrottleRequestsWithPredis extends ThrottleRequests class ThrottleRequestsWithPredis extends \Illuminate\Routing\Middleware\ThrottleRequests
{ {
/** /**
* The Redis factory implementation. * The Redis factory implementation.
* *
* @var \Illuminate\Redis\Connections\Connection * @var \Illuminate\Contracts\Redis\Factory
*/ */
protected $redis; protected $redis;
@ -34,13 +34,17 @@ class ThrottleRequestsWithPredis extends ThrottleRequests
* Create a new request throttler. * Create a new request throttler.
* *
* @param \Illuminate\Cache\RateLimiter $limiter * @param \Illuminate\Cache\RateLimiter $limiter
* @param \Illuminate\Contracts\Redis\Factory $redis
* @return void * @return void
*/ */
public function __construct(RateLimiter $limiter) public function __construct(RateLimiter $limiter, Redis $redis)
{ {
parent::__construct($limiter); parent::__construct($limiter);
$this->redis = \Illuminate\Support\Facades\Redis::connection('sentinel-cache'); $this->redis = \Illuminate\Support\Facades\Redis::connection('sentinel-cache');
// $this->redis = $redis;
} }
/** /**
@ -56,7 +60,7 @@ class ThrottleRequestsWithPredis extends ThrottleRequests
protected function handleRequest($request, Closure $next, array $limits) protected function handleRequest($request, Closure $next, array $limits)
{ {
foreach ($limits as $limit) { foreach ($limits as $limit) {
if ($this->tooManyAttempts($limit->key, $limit->maxAttempts, $limit->decayMinutes)) { if ($this->tooManyAttempts($limit->key, $limit->maxAttempts, $limit->decaySeconds)) {
throw $this->buildException($request, $limit->key, $limit->maxAttempts, $limit->responseCallback); throw $this->buildException($request, $limit->key, $limit->maxAttempts, $limit->responseCallback);
} }
} }
@ -79,16 +83,13 @@ class ThrottleRequestsWithPredis extends ThrottleRequests
* *
* @param string $key * @param string $key
* @param int $maxAttempts * @param int $maxAttempts
* @param int $decayMinutes * @param int $decaySeconds
* @return mixed * @return mixed
*/ */
protected function tooManyAttempts($key, $maxAttempts, $decayMinutes) protected function tooManyAttempts($key, $maxAttempts, $decaySeconds)
{ {
$limiter = new DurationLimiter( $limiter = new DurationLimiter(
$this->redis, $this->getRedisConnection(), $key, $maxAttempts, $decaySeconds
$key,
$maxAttempts,
$decayMinutes * 60
); );
return tap(! $limiter->acquire(), function () use ($key, $limiter) { return tap(! $limiter->acquire(), function () use ($key, $limiter) {
@ -121,4 +122,14 @@ class ThrottleRequestsWithPredis extends ThrottleRequests
{ {
return $this->decaysAt[$key] - $this->currentTime(); return $this->decaysAt[$key] - $this->currentTime();
} }
/**
* Get the Redis connection that should be used for throttling.
*
* @return \Illuminate\Redis\Connections\Connection
*/
protected function getRedisConnection()
{
return $this->redis;
}
} }

View File

@ -176,8 +176,8 @@ class SendRecurring implements ShouldQueue
private function createRecurringInvitations($invoice): Invoice private function createRecurringInvitations($invoice): Invoice
{ {
if ($this->recurring_invoice->invitations->count() == 0) { if ($this->recurring_invoice->invitations->count() == 0) {
$this->recurring_invoice->service()->createInvitations()->save(); $this->recurring_invoice = $this->recurring_invoice->service()->createInvitations()->save();
$this->recurring_invoice = $this->recurring_invoice->fresh(); // $this->recurring_invoice = $this->recurring_invoice->fresh();
} }
$this->recurring_invoice->invitations->each(function ($recurring_invitation) use ($invoice) { $this->recurring_invoice->invitations->each(function ($recurring_invitation) use ($invoice) {

View File

@ -11,17 +11,16 @@
namespace App\Providers; namespace App\Providers;
use App\Http\Middleware\ThrottleRequestsWithPredis;
use App\Models\Scheduler;
use App\Utils\Ninja; use App\Utils\Ninja;
use App\Models\Scheduler;
use Illuminate\Http\Request;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
use Illuminate\Support\Facades\Route;
use Illuminate\Cache\RateLimiting\Limit; use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Support\Facades\RateLimiter;
use App\Http\Middleware\ThrottleRequestsWithPredis;
use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException; use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Routing\Middleware\ThrottleRequests;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider class RouteServiceProvider extends ServiceProvider
{ {
@ -36,11 +35,11 @@ class RouteServiceProvider extends ServiceProvider
{ {
parent::boot(); parent::boot();
if (Ninja::isHosted() && !config('ninja.testvars.travis')) { // if (Ninja::isHosted() && !config('ninja.testvars.travis')) {
app('router')->aliasMiddleware('throttle', ThrottleRequestsWithPredis::class); app('router')->aliasMiddleware('throttle', ThrottleRequestsWithPredis::class);
} else { // } else {
app('router')->aliasMiddleware('throttle', ThrottleRequests::class); // app('router')->aliasMiddleware('throttle', ThrottleRequests::class);
} // }
Route::bind('task_scheduler', function ($value) { Route::bind('task_scheduler', function ($value) {
if (is_numeric($value)) { if (is_numeric($value)) {

View File

@ -482,7 +482,7 @@ class TemplateService
default => $processed = [], default => $processed = [],
}; };
nlog(json_encode($processed)); // nlog(json_encode($processed));
return $processed; return $processed;

22
composer.lock generated
View File

@ -16665,23 +16665,23 @@
}, },
{ {
"name": "nunomaduro/collision", "name": "nunomaduro/collision",
"version": "v8.3.0", "version": "v8.4.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/nunomaduro/collision.git", "url": "https://github.com/nunomaduro/collision.git",
"reference": "b49f5b2891ce52726adfd162841c69d4e4c84229" "reference": "e7d1aa8ed753f63fa816932bbc89678238843b4a"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/nunomaduro/collision/zipball/b49f5b2891ce52726adfd162841c69d4e4c84229", "url": "https://api.github.com/repos/nunomaduro/collision/zipball/e7d1aa8ed753f63fa816932bbc89678238843b4a",
"reference": "b49f5b2891ce52726adfd162841c69d4e4c84229", "reference": "e7d1aa8ed753f63fa816932bbc89678238843b4a",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"filp/whoops": "^2.15.4", "filp/whoops": "^2.15.4",
"nunomaduro/termwind": "^2.0.1", "nunomaduro/termwind": "^2.0.1",
"php": "^8.2.0", "php": "^8.2.0",
"symfony/console": "^7.1.2" "symfony/console": "^7.1.3"
}, },
"conflict": { "conflict": {
"laravel/framework": "<11.0.0 || >=12.0.0", "laravel/framework": "<11.0.0 || >=12.0.0",
@ -16689,13 +16689,13 @@
}, },
"require-dev": { "require-dev": {
"larastan/larastan": "^2.9.8", "larastan/larastan": "^2.9.8",
"laravel/framework": "^11.16.0", "laravel/framework": "^11.19.0",
"laravel/pint": "^1.16.2", "laravel/pint": "^1.17.1",
"laravel/sail": "^1.30.2", "laravel/sail": "^1.31.0",
"laravel/sanctum": "^4.0.2", "laravel/sanctum": "^4.0.2",
"laravel/tinker": "^2.9.0", "laravel/tinker": "^2.9.0",
"orchestra/testbench-core": "^9.2.1", "orchestra/testbench-core": "^9.2.3",
"pestphp/pest": "^2.34.9 || ^3.0.0", "pestphp/pest": "^2.35.0 || ^3.0.0",
"sebastian/environment": "^6.1.0 || ^7.0.0" "sebastian/environment": "^6.1.0 || ^7.0.0"
}, },
"type": "library", "type": "library",
@ -16758,7 +16758,7 @@
"type": "patreon" "type": "patreon"
} }
], ],
"time": "2024-07-16T22:41:01+00:00" "time": "2024-08-03T15:32:23+00:00"
}, },
{ {
"name": "phar-io/manifest", "name": "phar-io/manifest",