-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rethink the pulse:check command (#314)
- Loading branch information
1 parent
e6d53d9
commit 5048e24
Showing
8 changed files
with
306 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Laravel\Pulse\Recorders\Concerns; | ||
|
||
use Carbon\CarbonImmutable; | ||
use DateInterval; | ||
use Illuminate\Support\Facades\App; | ||
use Illuminate\Support\InteractsWithTime; | ||
use Laravel\Pulse\Events\IsolatedBeat; | ||
use Laravel\Pulse\Events\SharedBeat; | ||
use Laravel\Pulse\Support\CacheStoreResolver; | ||
|
||
trait Throttling | ||
{ | ||
use InteractsWithTime; | ||
|
||
/** | ||
* Determine if the recorder is ready to record another snapshot. | ||
*/ | ||
protected function throttle(DateInterval|int $interval, SharedBeat|IsolatedBeat $event, callable $callback, ?string $key = null): void | ||
{ | ||
$key ??= static::class; | ||
|
||
if ($event instanceof SharedBeat) { | ||
$key = $event->instance.":{$key}"; | ||
} | ||
|
||
$cache = App::make(CacheStoreResolver::class); | ||
|
||
$key = 'laravel:pulse:throttle:'.$key; | ||
|
||
$lastRunAt = $cache->store()->get($key); | ||
|
||
if ($lastRunAt !== null && CarbonImmutable::createFromTimestamp($lastRunAt)->addSeconds($this->secondsUntil($interval))->isFuture()) { | ||
return; | ||
} | ||
|
||
$callback($event); | ||
|
||
$cache->store()->put($key, $event->time->getTimestamp(), $interval); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.