django-ratelimit icon indicating copy to clipboard operation
django-ratelimit copied to clipboard

Pass along usage data with ratelimited requests and exceptions

Open cb109 opened this issue 1 year ago • 1 comments

I would like to be able to give feedback to users of our API once they run into the ratelimit, about how long they will have to wait until the limitation is lifted. That information is available already in the result of get_usage(...)["time_left"]", but using your @ratelimited decorator it is not passed along, all we get is either request.limited or we can handle the Ratelimited exception that doesn't carry the information either.

From what I understand I could write my own decorator or view logic from your building blocks to implement this, but I thought it might be worth thinking about this being a default behavior. I have used 3rd party APIs in the past that responded in such a way ("Your request was throttled, please try again in 0h 0m 59s") and found that very helpful as a user.

To illustrate: https://github.com/jsocol/django-ratelimit/pull/324

It may be more complicated than it appears to me or you may have additional concerns about backwards compatibility that I am not aware of. In that case please feel free to dismiss this.

Anyways: Thank you for creating/maintaining and sharing this wonderful library!

cb109 avatar Dec 20 '24 12:12 cb109

Reading a little more about it would make sense primarily to populate the value for this response header https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After

cb109 avatar Dec 20 '24 13:12 cb109