How do I throttle my site's API users?

后端 未结 6 1166
无人共我
无人共我 2020-11-28 01:35

The legitimate users of my site occasionally hammer the server with API requests that cause undesirable results. I want to institute a limit of no more than say one API call

6条回答
  •  星月不相逢
    2020-11-28 01:46

    You can control the rate with the token bucket algorithm, which is comparable to the leaky bucket algorithm. Note that you will have to share the state of the bucket (i.e. the amount of tokens) over processes (or whatever scope you want to control). So you might want to think about locking to avoid race conditions.

    The good news: I did all of that for you: bandwidth-throttle/token-bucket

    use bandwidthThrottle\tokenBucket\Rate;
    use bandwidthThrottle\tokenBucket\TokenBucket;
    use bandwidthThrottle\tokenBucket\storage\FileStorage;
    
    $storage = new FileStorage(__DIR__ . "/api.bucket");
    $rate    = new Rate(10, Rate::SECOND);
    $bucket  = new TokenBucket(10, $rate, $storage);
    $bucket->bootstrap(10);
    
    if (!$bucket->consume(1, $seconds)) {
        http_response_code(429);
        header(sprintf("Retry-After: %d", floor($seconds)));
        exit();
    }
    

提交回复
热议问题