Mercurial > p > roundup > code
comparison roundup/rate_limit.py @ 5937:5d0873a4de4a
fix rate limit headers - were ints/floats need to be strings
Running under gunicorn rest requests were crashing. Not all of the
values for the rate limit headers were strings. Some were
numbers. This caused the header generation for wsgi to fail. Now the
values are all strings.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Sun, 20 Oct 2019 20:56:56 -0400 |
| parents | e225f403cc35 |
| children | 69a35d164a69 |
comparison
equal
deleted
inserted
replaced
| 5936:ed5c19fca083 | 5937:5d0873a4de4a |
|---|---|
| 77 will be at least 1 available call to be consumed. | 77 will be at least 1 available call to be consumed. |
| 78 ''' | 78 ''' |
| 79 | 79 |
| 80 ret = {} | 80 ret = {} |
| 81 tat = self.get_tat(key) | 81 tat = self.get_tat(key) |
| 82 | |
| 83 # static defined headers according to limit | 82 # static defined headers according to limit |
| 84 ret['X-RateLimit-Limit'] = limit.count | 83 # all values are strings as that is required when used as headers |
| 85 ret['X-RateLimit-Limit-Period'] = int(limit.period.total_seconds()) | 84 ret['X-RateLimit-Limit'] = str(limit.count) |
| 85 ret['X-RateLimit-Limit-Period'] = str( | |
| 86 int( | |
| 87 limit.period.total_seconds()) | |
| 88 ) | |
| 86 | 89 |
| 87 # status of current limit as of now | 90 # status of current limit as of now |
| 88 now = datetime.utcnow() | 91 now = datetime.utcnow() |
| 89 | 92 |
| 90 ret['X-RateLimit-Remaining'] = min(int( | 93 current_count = int((limit.period - (tat - now)).total_seconds()\ |
| 91 (limit.period - (tat - now)).total_seconds() \ | 94 /limit.inverse) |
| 92 /limit.inverse),ret['X-RateLimit-Limit']) | 95 ret['X-RateLimit-Remaining'] = str(min(current_count,limit.count)) |
| 93 | 96 |
| 94 # tat_in_epochsec = (tat - datetime(1970, 1, 1)).total_seconds() | 97 # tat_in_epochsec = (tat - datetime(1970, 1, 1)).total_seconds() |
| 95 seconds_to_tat = (tat - now).total_seconds() | 98 seconds_to_tat = (tat - now).total_seconds() |
| 96 ret['X-RateLimit-Reset'] = max(seconds_to_tat, 0) | 99 ret['X-RateLimit-Reset'] = str(max(seconds_to_tat, 0)) |
| 97 ret['X-RateLimit-Reset-date'] = "%s"%tat | 100 ret['X-RateLimit-Reset-date'] = "%s"%tat |
| 98 ret['Now'] = (now - datetime(1970,1,1)).total_seconds() | 101 ret['Now'] = str((now - datetime(1970,1,1)).total_seconds()) |
| 99 ret['Now-date'] = "%s"%now | 102 ret['Now-date'] = "%s"%now |
| 100 | 103 |
| 101 if self.update(key, limit, testonly=True): | 104 if self.update(key, limit, testonly=True): |
| 102 # A new request would be rejected if it was processes. | 105 # A new request would be rejected if it was processes. |
| 103 # The user has to wait until an item is dequeued. | 106 # The user has to wait until an item is dequeued. |
| 104 # One item is dequeued every limit.inverse seconds. | 107 # One item is dequeued every limit.inverse seconds. |
| 105 ret['Retry-After'] = limit.inverse | 108 ret['Retry-After'] = str(int(limit.inverse)) |
| 106 ret['Retry-After-Timestamp'] = "%s"%(now + timedelta(seconds=limit.inverse)) | 109 ret['Retry-After-Timestamp'] = "%s"%(now + timedelta(seconds=limit.inverse)) |
| 107 else: | 110 else: |
| 108 # if we are not rejected, the user can post another | 111 # if we are not rejected, the user can post another |
| 109 # attempt immediately. | 112 # attempt immediately. |
| 110 # Do we even need this header if not rejected? | 113 # Do we even need this header if not rejected? |
| 111 # RFC implies this is used with a 503 (or presumably | 114 # RFC implies this is used with a 503 (or presumably |
| 112 # 429 which may postdate the rfc). So if no error, no header? | 115 # 429 which may postdate the rfc). So if no error, no header? |
| 113 # ret['Retry-After'] = 0 | 116 # ret['Retry-After'] = '0' |
| 114 # ret['Retry-After-Timestamp'] = ret['Now-date'] | 117 # ret['Retry-After-Timestamp'] = str(ret['Now-date']) |
| 115 pass | 118 pass |
| 116 | 119 |
| 117 return ret | 120 return ret |
