django-rest-framework icon indicating copy to clipboard operation
django-rest-framework copied to clipboard

X-Throttled: Expires in x sec

Open markotibold opened this issue 15 years ago • 5 comments

This is an idea Tom came up with some time ago which I remembered just now.

Add an X-Throttled field to to the response of a throttled request.

This can be implemented with less then 3 lines of code I think, because the ErrorResponse class takes header fields as kwargs.

markotibold avatar Jun 12 '11 23:06 markotibold

Ideally any throttling header ought to indicate 2 things.

  1. Success or failure
  2. A recommended delay before the next request. (Issued both on success and failure)

Eg. If the throttle is '3/sec':

First request is made: 'X-Throttle: status=SUCCESS; next=0.3333 sec' Next request is immediately made: 'X-Throttle: status=SUCCESS; next=0.5 sec' Next request is immediately made: 'X-Throttle: status=SUCCESS; next=1.0 sec' Next request is immediately made: 'X-Throttle: status=FAILURE; next=1.0 sec'

lovelydinosaur avatar Jun 13 '11 08:06 lovelydinosaur

Cool! I'll pick up this one. First write some tests, then do the implementation.

markotibold avatar Jun 13 '11 13:06 markotibold

I'm not really sure where the logic would live for responses to requests which have a throttle but were successfull. As I see it now; the dispatch() method of the View class would have to be tweaked. So that check_permissions() can return a value. Or the check_permissions() method can preset a header field on the View object or so. So that later on when the response will be rendered, the header can be inserted. But that would make things less obvious I guess.

markotibold avatar Jun 13 '11 15:06 markotibold

Yeah. Prob need something like a .headers attribute on the View instance, and an add_header() method too.

On 13 June 2011 16:07, markotibold < [email protected]>wrote:

I'm not really sure where he logic would live for responses to requests which have a throttle but were successfull. As I see it now; the dispath() method of the View class would have to be tweaked. So that check_permissions() can return a value. Or the check_permissions() method can preset a header field on the View object or so. So that later on when the response will be rendered, the header can be inserted. But that would make things less obvious I guess.

Reply to this email directly or view it on GitHub:

https://github.com/tomchristie/django-rest-framework/issues/28#issuecomment-1359412

lovelydinosaur avatar Jun 13 '11 15:06 lovelydinosaur

Implemented. The commits can be found on my repo at BitBucket.

markotibold avatar Jun 13 '11 18:06 markotibold