I'm wondering if there are better algorithms than this, especially if you drastically change the allowed rate this will take a while to adjust instead of actually computing a sleep time that matches the desired rate. I never really gave it much thought, it's an interesting topic. I guess this works fine for now, maybe you can add a comment mentioning this could be improved?