Ticket #2087: buffer.diff

File buffer.diff, 2.7 KB (added by ghazel, 15 years ago)

buffer doubling patch for abstract.py (tab removal)

  • twisted\internet\iocpreactor\

    old new  
    1919    __metaclass__ = StateEventMachineType
    2020    implements(interfaces.ITransport, interfaces.IProducer, interfaces.IConsumer)
    2121    events = ["write", "loseConnection", "writeDone", "writeErr", "readDone", "readErr", "shutdown"]
    22     bufferSize = 2**2**2**2
     22    bufferSize = 2**9
     23    maxBufferSize = 2**16
     24    writeBufferSize = 2**16
    2325    producer = None
    2426    writing = False
    2527    reading = False
     
    4345        self.read_op = ReadFileOp(self)
    4446        self.write_op = WriteFileOp(self) # XXX: these two should be specified like before, with a class field
    4547
     48    def _resizeBuffer(self):
     49        newBufferSize = min(self.maxBufferSize, self.bufferSize * 2)
     50        if newBufferSize != self.bufferSize:
     51            from twisted.internet import reactor
     52            self.bufferSize = newBufferSize
     53            self.readbuf = reactor.AllocateReadBuffer(self.bufferSize)
     54
    4655    def addBufferCallback(self, handler, event):
    4756        self.bufferEvents[event].add(handler)
    4857
     
    5463            i(*a, **kw)
    5564
    5665    def handle_connected_write(self, data):
    57         if self.writebuf and len(self.writebuf[-1]) < self.bufferSize: # mmmhhh silly heuristics
     66        if self.writebuf and len(self.writebuf[-1]) < self.writeBufferSize: # mmmhhh silly heuristics
    5867            self.writebuf[-1] += data
    5968        else:
    6069            self.writebuf.append(data)
    6170        self.writeBufferedSize += len(data)
    62         if self.writeBufferedSize >= self.bufferSize:
     71        if self.writeBufferedSize >= self.writeBufferSize:
    6372            self.callBufferHandlers(event = "buffer full")
    6473        if not self.writing:
    6574            self.startWriting()
     
    155164        self.reading = False
    156165
    157166    def handle_connected_readDone(self, bytes):
     167        # painful copy
     168        buffer = self.readbuf[:bytes]
     169       
     170        if bytes == len(self.readbuf):
     171            self._resizeBuffer()
     172       
    158173        if self.reading:
    159             self.protocol.dataReceived(self.readbuf[:bytes])
    160             self.startReading()
     174            self.protocol.dataReceived(buffer)
     175            # we have to check again here, because we just called a handler that
     176            # might have called pauseProducing
     177            if self.reading:
     178                self.startReading()
    161179        else:
    162             self.producerBuffer.append(self.readbuf[:bytes])
     180            self.producerBuffer.append(buffer)
    163181
    164182    def handle_disconnecting_readDone(self, bytes):
    165183        pass # a leftover read op from before we began disconnecting