- if self.writtenThisSecond > self.writeLimit:
- self.throttleWrites()
- throttleTime = (float(self.writtenThisSecond) / self.writeLimit) - 1.0
- self.unthrottleWritesID = reactor.callLater(throttleTime,
- self.unthrottleWrites)
- # reset for next round
- self.writtenThisSecond = 0
+ """Add some new available bandwidth, and check for protocols to unthrottle."""
+ # Increase the available write bytes, but not higher than the limit
+ self.writeAvailable = min(self.writeLimit, self.writeAvailable + self.writeLimit)
+
+ # Write from the queue until it's empty or we're throttled again
+ while self.writeAvailable > 0 and self._writeQueue:
+ # Get the first queued protocol
+ p = self._writeQueue.pop(0)
+ _tempWriteAvailable = self.writeAvailable
+ bytesLeft = 1
+
+ # Unthrottle writes until CHUNK_SIZE is reached or the protocol is unbuffered
+ while self.writeAvailable > 0 and _tempWriteAvailable - self.writeAvailable < self.CHUNK_SIZE and bytesLeft > 0:
+ # Unthrottle a single write (from the protocol's buffer)
+ bytesLeft = p.unthrottleWrites()
+
+ # If the protocol is not done, requeue it
+ if bytesLeft > 0:
+ self._writeQueue.append(p)
+