Python twisted.protocols.policies.TimeoutMixin() Examples

The following are 13 code examples of twisted.protocols.policies.TimeoutMixin(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module twisted.protocols.policies , or try the search function .
Example #1
Source File: http.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def buildProtocol(self, addr):
        p = protocol.ServerFactory.buildProtocol(self, addr)
        # timeOut needs to be on the Protocol instance cause
        # TimeoutMixin expects it there
        p.timeOut = self.timeOut
        return p 
Example #2
Source File: _http2.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def timeoutConnection(self):
        """
        Called when the connection has been inactive for
        L{self.timeOut<twisted.protocols.policies.TimeoutMixin.timeOut>}
        seconds. Cleanly tears the connection down, attempting to notify the
        peer if needed.

        We override this method to add two extra bits of functionality:

         - We want to log the timeout.
         - We want to send a GOAWAY frame indicating that the connection is
           being terminated, and whether it was clean or not. We have to do this
           before the connection is torn down.
        """
        self._log.info(
            "Timing out client {client}", client=self.transport.getPeer()
        )

        # Check whether there are open streams. If there are, we're going to
        # want to use the error code PROTOCOL_ERROR. If there aren't, use
        # NO_ERROR.
        if (self.conn.open_outbound_streams > 0 or
                self.conn.open_inbound_streams > 0):
            error_code = h2.errors.PROTOCOL_ERROR
        else:
            error_code = h2.errors.NO_ERROR

        self.conn.close_connection(error_code=error_code)
        self.transport.write(self.conn.data_to_send())

        # We're done, throw the connection away.
        self.transport.loseConnection() 
Example #3
Source File: http.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def timeoutConnection(self):
        self._log.info(
            "Timing out client: {peer}",
            peer=str(self.transport.getPeer())
        )
        if self.abortTimeout is not None:
            # We use self.callLater because that's what TimeoutMixin does.
            self._abortingCall = self.callLater(
                self.abortTimeout, self.forceAbortClient
            )
        self.loseConnection() 
Example #4
Source File: http.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def callLater(self):
        """
        A value for the C{callLater} callback. This callback is used by the
        L{twisted.protocols.policies.TimeoutMixin} to handle timeouts.
        """
        return self._channel.callLater 
Example #5
Source File: http.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def callLater(self, value):
        """
        Sets the value for the C{callLater} callback. This callback is used by
        the L{twisted.protocols.policies.TimeoutMixin} to handle timeouts.

        @param value: The new callback to use.
        @type value: L{callable}
        """
        self._callLater = value
        self._channel.callLater = value 
Example #6
Source File: http.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def buildProtocol(self, addr):
        p = protocol.ServerFactory.buildProtocol(self, addr)

        # This is a bit of a hack to ensure that the HTTPChannel timeouts
        # occur on the same reactor as the one we're using here. This could
        # ideally be resolved by passing the reactor more generally to the
        # HTTPChannel, but that won't work for the TimeoutMixin until we fix
        # https://twistedmatrix.com/trac/ticket/8488
        p.callLater = self._reactor.callLater

        # timeOut needs to be on the Protocol instance cause
        # TimeoutMixin expects it there
        p.timeOut = self.timeOut
        return p 
Example #7
Source File: _http2.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def timeoutConnection(self):
        """
        Called when the connection has been inactive for
        L{self.timeOut<twisted.protocols.policies.TimeoutMixin.timeOut>}
        seconds. Cleanly tears the connection down, attempting to notify the
        peer if needed.

        We override this method to add two extra bits of functionality:

         - We want to log the timeout.
         - We want to send a GOAWAY frame indicating that the connection is
           being terminated, and whether it was clean or not. We have to do this
           before the connection is torn down.
        """
        self._log.info(
            "Timing out client {client}", client=self.transport.getPeer()
        )

        # Check whether there are open streams. If there are, we're going to
        # want to use the error code PROTOCOL_ERROR. If there aren't, use
        # NO_ERROR.
        if (self.conn.open_outbound_streams > 0 or
                self.conn.open_inbound_streams > 0):
            error_code = h2.errors.ErrorCodes.PROTOCOL_ERROR
        else:
            error_code = h2.errors.ErrorCodes.NO_ERROR

        self.conn.close_connection(error_code=error_code)
        self.transport.write(self.conn.data_to_send())

        # Don't let the client hold this connection open too long.
        if self.abortTimeout is not None:
            # We use self.callLater because that's what TimeoutMixin does, even
            # though we have a perfectly good reactor sitting around. See
            # https://twistedmatrix.com/trac/ticket/8488.
            self._abortingCall = self.callLater(
                self.abortTimeout, self.forceAbortClient
            )

        # We're done, throw the connection away.
        self.transport.loseConnection() 
Example #8
Source File: http.py    From ccs-calendarserver with Apache License 2.0 5 votes vote down vote up
def timeoutConnection(self):
        # log.info("Timing out client: %s" % str(self.transport.getPeer()))
        # Set an abort timer in case an orderly close hangs
        self._abortTimer = reactor.callLater(self.closeTimeOut, self._abortTimeout)
        policies.TimeoutMixin.timeoutConnection(self) 
Example #9
Source File: http.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def timeoutConnection(self):
        log.msg("Timing out client: %s" % str(self.transport.getPeer()))
        policies.TimeoutMixin.timeoutConnection(self) 
Example #10
Source File: http.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def buildProtocol(self, addr):
        p = protocol.ServerFactory.buildProtocol(self, addr)
        # timeOut needs to be on the Protocol instance cause
        # TimeoutMixin expects it there
        p.timeOut = self.timeOut
        return p 
Example #11
Source File: http.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def timeoutConnection(self):
        log.msg("Timing out client: %s" % str(self.transport.getPeer()))
        policies.TimeoutMixin.timeoutConnection(self) 
Example #12
Source File: http.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def buildProtocol(self, addr):
        p = protocol.ServerFactory.buildProtocol(self, addr)
        # timeOut needs to be on the Protocol instance cause
        # TimeoutMixin expects it there
        p.timeOut = self.timeOut
        return p 
Example #13
Source File: test_policies.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def testOverriddenCallLater(self):
        """
        Test that setting callLater on a subclass of TimeoutMixin causes the
        protocol to use that callable instead of C{reactor.callLater}.
        """
        calls = []
        p = TimeoutTester()
        p.callLater = lambda *a, **kw: calls.append((a, kw))
        p.setTimeout(10)
        self.assertEquals(len(calls), 1)