title: Atomic current limiter meta:

  • name: description content: Easyswoole provides a current limiter based on Atomic counters
  • name: keywords content: swoole|swoole extension|swoole framework|easyswoole|Atomic|Current limiter

AtomicLimit

Easyswoole provides a current limiter based on Atomic counters

principle

The basic current limit is achieved by limiting the total number of requests in a certain time period. For example, if the maximum number of requests allowed is 200 in 5 seconds, then the theoretical average is 40 and the peak is 200.

Installation

  1. composer require easyswoole/atomic-limit

Sample code

  1. /*
  2. * egUrl http://127.0.0.1:9501/index.html?api=1
  3. */
  4. use EasySwoole\AtomicLimit\AtomicLimit;
  5. AtomicLimit::getInstance()->addItem('default')->setMax(200);
  6. AtomicLimit::getInstance()->addItem('api')->setMax(2);
  7. $http = new swoole_http_server("127.0.0.1", 9501);
  8. AtomicLimit::getInstance()->enableProcessAutoRestore($http,10*1000);
  9. $http->on("request", function ($request, $response) {
  10. if(isset($request->get['api'])){
  11. if(AtomicLimit::isAllow('api')){
  12. $response->write('api success');
  13. }else{
  14. $response->write('api refuse');
  15. }
  16. }else{
  17. if(AtomicLimit::isAllow('default')){
  18. $response->write('default success');
  19. }else{
  20. $response->write('default refuse');
  21. }
  22. }
  23. $response->end();
  24. });
  25. $http->start();

::: warning Note that this example uses a custom process plus timer to implement the count timing reset. In fact, it is not worthwhile to use a process to do this. Therefore, the actual production can specify a worker and set a timer to implement. :::

Use

We can register the current limiter in the mainServerCreate event of the Easyswoole global.

  1. use EasySwoole\AtomicLimit\AtomicLimit;
  2. AtomicLimit::getInstance()->addItem('default')->setMax(200);
  3. AtomicLimit::getInstance()->addItem('api')->setMax(2);
  4. AtomicLimit::getInstance()->enableProcessAutoRestore(ServerManager::getInstance()->getSwooleServer(),10*1000)

::: warning The above code indicates that the default limiter allows a maximum flow of 200 in 5 seconds, while the api has a maximum flow of 2 in the limiter. :::

Subsequently, we can perform request interception in the base controller of Easyswoole. For example, in the onRequest event, the traffic check is performed first. If the check passes, the next step is performed.