Skip to content

Commit 5baf307

Browse files
committed
Optimize rate limit
1 parent 874a1bc commit 5baf307

File tree

3 files changed

+46
-24
lines changed

3 files changed

+46
-24
lines changed

config/exception-notify.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,23 @@
6666
|
6767
*/
6868
'rate_limiter' => [
69-
// Limit the number of exceptions.
70-
'limit' => (int) env('EXCEPTION_NOTIFY_LIMIT', 1),
71-
72-
// Time interval.
73-
'interval' => env('EXCEPTION_NOTIFY_INTERVAL', '1 minutes'),
69+
// Config.
70+
'config' => [
71+
'limit' => (int) env('EXCEPTION_NOTIFY_LIMIT', 1),
72+
'rate' => [
73+
// https://www.php.net/manual/en/datetime.formats.php
74+
'interval' => env('EXCEPTION_NOTIFY_INTERVAL', '1 minutes'),
75+
],
76+
],
7477

75-
// Cache adapter.
76-
'cache_adapter' => \Symfony\Component\Cache\Adapter\FilesystemAdapter::class,
78+
// Storage.
79+
'storage' => [
80+
// \Psr\Cache\CacheItemPoolInterface::class
81+
'class' => \Symfony\Component\Cache\Adapter\PhpFilesAdapter::class,
82+
'parameters' => [
83+
'directory' => storage_path('framework/cache/exception-notify'),
84+
],
85+
],
7786
],
7887

7988
/*

src/ExceptionNotifyManager.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,10 @@ public function shouldntReport(Throwable $e): bool
113113
}
114114
}
115115

116-
/* @var RateLimiterFactory $rateLimiterFactory */
117-
$rateLimiterFactory = $this->container->make(RateLimiterFactory::class);
118-
119-
return ! $rateLimiterFactory
120-
->create(md5($e->getFile().$e->getLine().$e->getCode().$e->getMessage()))
116+
return ! $this
117+
->container
118+
->make(RateLimiterFactory::class)
119+
->create(md5($e->getMessage().$e->getCode().$e->getFile().$e->getLine()))
121120
->consume()
122121
->isAccepted();
123122
}

src/ExceptionNotifyServiceProvider.php

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function register()
5151
$this->setupConfig();
5252
$this->registerExceptionNotifyManager();
5353
$this->registerCollectorManager();
54-
$this->registerRateLimiterFactory();
54+
$this->registerRateLimiter();
5555
}
5656

5757
/**
@@ -99,20 +99,34 @@ protected function registerExceptionNotifyManager()
9999
$this->app->alias(ExceptionNotifyManager::class, 'exception.notifier');
100100
}
101101

102-
protected function registerRateLimiterFactory()
102+
protected function registerRateLimiter()
103103
{
104-
$this->app->singleton(RateLimiterFactory::class, function (Container $app) {
105-
return new RateLimiterFactory([
106-
'id' => 'exception_notify',
107-
'policy' => 'token_bucket',
108-
'limit' => $app['config']['exception-notify.rate_limiter.limit'],
109-
'rate' => [
110-
'interval' => $app['config']['exception-notify.rate_limiter.interval'],
111-
],
112-
], new CacheStorage($app->make($app['config']['exception-notify.rate_limiter.cache_adapter'])));
104+
$this->app->singleton(CacheStorage::class, function (Container $app) {
105+
return new CacheStorage(
106+
$app->make(
107+
$app['config']['exception-notify.rate_limiter.storage.class'],
108+
$app['config']['exception-notify.rate_limiter.storage.parameters']
109+
)
110+
);
113111
});
112+
$this->app->alias(CacheStorage::class, 'exception.rate-limiter.storage');
114113

115-
$this->app->alias(RateLimiterFactory::class, 'exception.rate-limiter-factory');
114+
$this->app->singleton(RateLimiterFactory::class, function (Container $app) {
115+
return new RateLimiterFactory(
116+
array_merge([
117+
'id' => 'exception-notify',
118+
'policy' => 'token_bucket',
119+
'limit' => 6,
120+
'interval' => '1 minutes',
121+
'rate' => [
122+
'amount' => 1,
123+
'interval' => '1 minutes',
124+
],
125+
], $app['config']['exception-notify.rate_limiter.config']),
126+
$this->app->make(CacheStorage::class)
127+
);
128+
});
129+
$this->app->alias(RateLimiterFactory::class, 'exception.rate-limiter.factory');
116130
}
117131

118132
protected function registerReportingEvent()

0 commit comments

Comments
 (0)