|
Revision 24441, 1.3 KB
(checked in by thijs, 2 years ago)
|
|
Merge maintainer-email-2438: Get rid of references to maintainer email addresses from code.
Author: thijs
Reviewer: exarkun
Fixes: #2438
|
| Line | |
|---|
| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | """ |
|---|
| 6 | Experimental optimization |
|---|
| 7 | |
|---|
| 8 | This module provides a single mixin class which allows protocols to |
|---|
| 9 | collapse numerous small writes into a single larger one. |
|---|
| 10 | |
|---|
| 11 | @author: Jp Calderone |
|---|
| 12 | """ |
|---|
| 13 | |
|---|
| 14 | from twisted.internet import reactor |
|---|
| 15 | |
|---|
| 16 | class BufferingMixin: |
|---|
| 17 | """Mixin which adds write buffering. |
|---|
| 18 | """ |
|---|
| 19 | _delayedWriteCall = None |
|---|
| 20 | bytes = None |
|---|
| 21 | |
|---|
| 22 | DELAY = 0.0 |
|---|
| 23 | |
|---|
| 24 | def schedule(self): |
|---|
| 25 | return reactor.callLater(self.DELAY, self.flush) |
|---|
| 26 | |
|---|
| 27 | def reschedule(self, token): |
|---|
| 28 | token.reset(self.DELAY) |
|---|
| 29 | |
|---|
| 30 | def write(self, bytes): |
|---|
| 31 | """Buffer some bytes to be written soon. |
|---|
| 32 | |
|---|
| 33 | Every call to this function delays the real write by C{self.DELAY} |
|---|
| 34 | seconds. When the delay expires, all collected bytes are written |
|---|
| 35 | to the underlying transport using L{ITransport.writeSequence}. |
|---|
| 36 | """ |
|---|
| 37 | if self._delayedWriteCall is None: |
|---|
| 38 | self.bytes = [] |
|---|
| 39 | self._delayedWriteCall = self.schedule() |
|---|
| 40 | else: |
|---|
| 41 | self.reschedule(self._delayedWriteCall) |
|---|
| 42 | self.bytes.append(bytes) |
|---|
| 43 | |
|---|
| 44 | def flush(self): |
|---|
| 45 | """Flush the buffer immediately. |
|---|
| 46 | """ |
|---|
| 47 | self._delayedWriteCall = None |
|---|
| 48 | self.transport.writeSequence(self.bytes) |
|---|
| 49 | self.bytes = None |
|---|