| 428 | self.ratelimit = None |
| 429 | |
| 430 | def write(self, fd, to_send): |
| 431 | if self.ratelimit: |
| 432 | now = time.monotonic() |
| 433 | if self.ratelimit_last + RATELIMIT_PERIOD <= now: |
| 434 | self.ratelimit_quota += self.ratelimit |
| 435 | if self.ratelimit_quota > 2 * self.ratelimit: |
| 436 | self.ratelimit_quota = 2 * self.ratelimit |
| 437 | self.ratelimit_last = now |
| 438 | if self.ratelimit_quota == 0: |
| 439 | tosleep = self.ratelimit_last + RATELIMIT_PERIOD - now |
| 440 | time.sleep(tosleep) |
| 441 | self.ratelimit_quota += self.ratelimit |
| 442 | self.ratelimit_last = time.monotonic() |
| 443 | if len(to_send) > self.ratelimit_quota: |
| 444 | to_send = to_send[: self.ratelimit_quota] |
| 445 | try: |
| 446 | written = os.write(fd, to_send) |
| 447 | except BrokenPipeError: |
| 448 | raise ConnectionBrokenWithHint("Broken Pipe") from None |
| 449 | if self.ratelimit: |
| 450 | self.ratelimit_quota -= written |
| 451 | return written |
| 452 | |
| 453 | |
| 454 | def api(*, since, **kwargs_decorator): |