Laravel queue rate limiting or throttling

后端 未结 6 1319
南方客
南方客 2020-12-11 17:19

I am working on an app that requires fetching data from a third-party server and that server allows max 1 request per seconds.

Now, all request send as job and I am

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-11 17:30

    You could use this package to use rate limiting with Redis or another source, like a file. Uses settings to set bucket size and rate as fractions of the time limit, so very small storage.

    composer require bandwidth-throttle/token-bucket
    

    https://github.com/bandwidth-throttle/token-bucket

    It allows you to wrap the check in an if, so it will wait for a free token to be available, 1 a minute in your example. In effect, it makes the service sleep for the required amount of time until a new minute.

提交回复
热议问题