I saw Laravel throttling when I was reading the document today. I searched the Internet and saw Dingo’s throttling document. So look at Laravel and Dingo’s source code and compare the similarities and differences between the two.

The same

Both use middleware to handle requests and limit traffic

The processing method is to record the cache key, set the expiration time, when not expired, increment until the limit is exceeded, or the key expires

Laravel current-limiting middlewareIlluminate\Routing\Middleware\ThrottleRequests

Dingo stream limiting Middleware Dingo Api Http Middleware RateLimit

The difference between

As you can see from the above two middleware code, Laravel does +1 on the cache only if the limit is never exceeded, while Dingo does it first and then decides

Dingo limits the key to be prefixed with the hash of the request path, and the default key is the user IP. Therefore, each URL of the user can be restricted to a finer granularity

Dingo\Api\Http\RateLimit\Handler:

Laravel uses user information or domain name + IP as the restriction key, and the restriction granularity is only at the user level

Illuminate\Routing\Middleware\ThrottleRequests code is as follows:

Dingo supports modification of limit keys, while Laravel does not support modification methods by default

Dingo supports adding multiple restriction rules, logically using the one with the smallest number of restrictions.

Suppose you have two limiters, both of which meet the constraints. Limit one to 10 times per minute and the other to 15 times per two minutes.

Dingo returns the expiration date, Laravel does not return the expiration date by default

Dingo\Api\Http\RateLimit\Handler gets the least restrictive limiter code as follows:

Dingo\Api\Http\RateLimit\Handler Gets the set return header as follows:

Illuminate\Routing\Middleware\ThrottleRequests The code for returning the set header is as follows:

conclusion

The principle is the same, but Dingo is more powerful in the details. Dingo limits granularity, limit rules, scalability, flexibility are stronger than Laravel.

Both use middleware to handle requests and limit traffic. The processing method is to record the cache key, set the expiration time, when not expired, increment until the limit is exceeded, or the key expires.

Laravel does +1 only if the limit is never exceeded, whereas Dingo does it first and then decides; Dingo limits the key to be prefixed with the hash of the request path. By default, the user’S IP address is used as the key and the restriction is more granular. Dingo supports modification of restricted keys, while Laravel does not support modification methods by default. Dingo supports adding multiple restriction rules.