Rolling Time Window Throttling example in Python

udaiveer singh
Code Wave
Published in
2 min readSep 25, 2020

In the rolling time window, each user has their own throttle rates and start times. For Example, one can configure a max of 2 requests per 6 seconds, then when the 6 seconds have elapsed reset the request count to 0 and update the time window.

The sample program for the simulation is below

Below a sample simulation that sets an API call limit to 2 calls every 6 seconds. We simulate a person trying to call the API randomly every 1–3 seconds.

[Time window 1] 
Call was made at ==============================> 24-09-2020 20:49:51
Call was made at ==============================> 24-09-2020 20:49:52
Max call limit of 2 every 6 Seconds reached> 24-09-2020 20:49:54
Max call limit of 2 every 6 Seconds reached> 24-09-2020 20:49:56
[Time window 2] - we reset all limits here
Call was made at ==============================> 24-09-2020 20:49:57
Call was made at ==============================> 24-09-2020 20:49:59
Max call limit of 2 every 6 Seconds reached> 24-09-2020 20:50:00
Max call limit of 2 every 6 Seconds reached> 24-09-2020 20:50:01
Max call limit of 2 every 6 Seconds reached> 24-09-2020 20:50:02
[Time window 3] - we reset all limits here
Call was made at ==============================> 24-09-2020 20:50:03
Call was made at ==============================> 24-09-2020 20:50:05
Max call limit of 2 every 6 Seconds reached> 24-09-2020 20:50:06
Max call limit of 2 every 6 Seconds reached> 24-09-2020 20:50:08
[Time window 4] - we reset all limits here
Call was made at ==============================> 24-09-2020 20:50:09
Call was made at ==============================> 24-09-2020 20:50:11
Max call limit of 2 every 6 Seconds reached> 24-09-2020 20:50:12
Max call limit of 2 every 6 Seconds reached> 24-09-2020 20:50:14

--

--