Add rate limiting

This commit is contained in:
philipp lang 2024-07-09 12:13:37 +02:00
parent e924be87df
commit ba403c5f59
1 changed files with 15 additions and 13 deletions

View File

@ -7,6 +7,7 @@ use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Redis;
class FillCoordsJob implements ShouldQueue class FillCoordsJob implements ShouldQueue
{ {
@ -31,20 +32,21 @@ class FillCoordsJob implements ShouldQueue
*/ */
public function handle() public function handle()
{ {
$address = $this->model->getAddressForGeolocation(); Redis::throttle('osm')->block(0)->allow(1)->every(2)->then(function () {
$address = $this->model->getAddressForGeolocation();
if (!$address) {
$this->model->destroyCoordinate();
if (!$address) { return;
$this->model->destroyCoordinate(); }
$coordinate = $address->getCoordinate();
return; if (!$coordinate) {
} return;
}
$coordinate = $address->getCoordinate(); $this->model->fillCoordinate($coordinate);
}, function () {
if (!$coordinate) { $this->release(5);
return; });
}
$this->model->fillCoordinate($coordinate);
} }
} }