38 private int $lastUpdateTimeNs;
39 private int $maxBudget;
41 public function __construct(
43 private int $averagePerTick,
45 private int $updateFrequencyNs = 50_000_000,
47 $this->maxBudget = $this->averagePerTick * $maxBufferTicks;
48 $this->budget = $this->maxBudget;
49 $this->lastUpdateTimeNs = hrtime(
true);
56 if($this->budget <= 0){
58 if($this->budget <= 0){
62 $this->budget -= $amount;
65 public function update() : void{
66 $nowNs = hrtime(true);
67 $timeSinceLastUpdateNs = $nowNs - $this->lastUpdateTimeNs;
68 if($timeSinceLastUpdateNs > $this->updateFrequencyNs){
69 $ticksSinceLastUpdate = intdiv($timeSinceLastUpdateNs, $this->updateFrequencyNs);
77 $this->budget = min($this->budget, $this->maxBudget) + ($this->averagePerTick * 2 * $ticksSinceLastUpdate);
78 $this->lastUpdateTimeNs = $nowNs;