Beispiel #1
0
    def handle(self, listener, client, addr):
        self.cfg.on_connected(self, addr)
        if self.app.cfg.thrift_client_timeout is not None:
            client.settimeout(self.app.cfg.thrift_client_timeout)

        result = TSocket()
        result.set_handle(client)

        try:
            itrans = self.app.tfactory.get_transport(result)
            otrans = self.app.tfactory.get_transport(result)
            iprot = self.app.pfactory.get_protocol(itrans)
            oprot = self.app.pfactory.get_protocol(otrans)

            processor = self.get_thrift_processor()

            try:
                while True:
                    self.nr += 1
                    if self.alive and (
                            self.nr >= self.max_requests or
                        (self.cfg.worker_timeout
                         and int(self.tmp.last_update()) -
                         int(os.fstat(self.tmp.fileno()).st_mtime) >
                         self.cfg.worker_timeout)):
                        self.log.info(
                            "Autorestarting worker after current process.")
                        self.alive = False
                    processor.process(iprot, oprot)
            except TTransportException:
                pass
        except (TProtocolException, ProtocolError) as err:
            self.log.warning("Protocol error, is client(%s) correct? %s", addr,
                             err)
        except TDecodeException as err:
            self.log.exception('%r: %r', addr, err)
            self.cfg.on_tdecode_exception(err)
        except socket.timeout:
            self.log.warning('Client timeout: %r', addr)
        except socket.error as e:
            if e.args[0] == errno.ECONNRESET:
                self.log.debug('%r: %r', addr, e)
            elif e.args[0] == errno.EPIPE:
                self.log.warning('%r: %r', addr, e)
            else:
                self.log.exception('%r: %r', addr, e)
        except Exception as e:
            self.log.exception('%r: %r', addr, e)
        finally:
            itrans.close()
            otrans.close()
            self.cfg.post_connect_closed(self)
Beispiel #2
0
def test_transport_handle():
    from thriftpy2._compat import CYTHON
    if not CYTHON:
        return

    from thriftpy2.transport import TSocket
    from thriftpy2.transport.memory import TCyMemoryBuffer

    s = TSocket()
    s.set_handle('the sock')

    assert TCyBufferedTransport(s).sock == 'the sock'
    assert TCyFramedTransport(s).sock == 'the sock'
    assert TCyMemoryBuffer().sock is None
    assert TCyBufferedTransport(TCyFramedTransport(s)).sock == 'the sock'
    assert TCyBufferedTransport(TCyMemoryBuffer()).sock is None
    def handle(self, listener, client, addr):
        self.cfg.on_connected(self, addr)
        if self.app.cfg.thrift_client_timeout is not None:
            client.settimeout(self.app.cfg.thrift_client_timeout)

        result = TSocket()
        result.set_handle(client)

        try:
            itrans = self.app.tfactory.get_transport(result)
            otrans = self.app.tfactory.get_transport(result)
            iprot = self.app.pfactory.get_protocol(itrans)
            oprot = self.app.pfactory.get_protocol(otrans)

            processor = self.get_thrift_processor()

            try:
                while True:
                    processor.process(iprot, oprot)
            except TTransportException:
                pass
        except (TProtocolException, ProtocolError) as err:
            self.log.warning(
                "Protocol error, is client(%s) correct? %s", addr, err
                )
        except TDecodeException as err:
            self.log.exception('%r: %r', addr, err)
            self.cfg.on_tdecode_exception(err)
        except socket.timeout:
            self.log.warning('Client timeout: %r', addr)
        except socket.error as e:
            if e.args[0] == errno.ECONNRESET:
                self.log.debug('%r: %r', addr, e)
            elif e.args[0] == errno.EPIPE:
                self.log.warning('%r: %r', addr, e)
            else:
                self.log.exception('%r: %r', addr, e)
        except Exception as e:
            self.log.exception('%r: %r', addr, e)
        finally:
            itrans.close()
            otrans.close()
            self.cfg.post_connect_closed(self)
    def handle(self, listener, client, addr):
        self.cfg.on_connected(self, addr)
        if self.app.cfg.thrift_client_timeout is not None:
            client.settimeout(self.app.cfg.thrift_client_timeout)

        result = TSocket()
        result.set_handle(client)

        try:
            itrans = self.app.tfactory.get_transport(result)
            otrans = self.app.tfactory.get_transport(result)
            iprot = self.app.pfactory.get_protocol(itrans)
            oprot = self.app.pfactory.get_protocol(otrans)

            processor = self.get_thrift_processor()

            try:
                while True:
                    self.nr += 1
                    if self.alive and self.nr >= self.max_requests:
                        self.log.info(
                            "Autorestarting worker after current process.")
                        self.alive = False
                    processor.process(iprot, oprot)
                    self.notify()
            except TTransportException:
                pass
        except TDecodeException as err:
            self.log.exception('%r: %r', addr, err)
            self.cfg.on_tdecode_exception(err)
        except socket.timeout:
            self.log.warning('Client timeout: %r', addr)
        except socket.error as e:
            if e.args[0] == errno.ECONNRESET:
                self.log.debug(e)
            else:
                self.log.exception(e)
        except Exception as e:
            self.log.exception(e)
        finally:
            itrans.close()
            otrans.close()
            self.cfg.post_connect_closed(self)
    def handle(self, listener, client, addr):
        self.cfg.on_connected(self, addr)
        if self.app.cfg.thrift_client_timeout is not None:
            client.settimeout(self.app.cfg.thrift_client_timeout)

        result = TSocket()
        result.set_handle(client)

        try:
            itrans = self.app.tfactory.get_transport(result)
            otrans = self.app.tfactory.get_transport(result)
            iprot = self.app.pfactory.get_protocol(itrans)
            oprot = self.app.pfactory.get_protocol(otrans)

            processor = self.get_thrift_processor()

            try:
                while True:
                    processor.process(iprot, oprot)
            except TTransportException:
                pass
        except (TProtocolException, ProtocolError) as err:
            self.log.warning("Protocol error, is client(%s) correct? %s", addr,
                             err)
        except TDecodeException as err:
            self.log.exception('%r: %r', addr, err)
            self.cfg.on_tdecode_exception(err)
        except socket.timeout:
            self.log.warning('Client timeout: %r', addr)
        except socket.error as e:
            if e.args[0] == errno.ECONNRESET:
                self.log.debug('%r: %r', addr, e)
            elif e.args[0] == errno.EPIPE:
                self.log.warning('%r: %r', addr, e)
            else:
                self.log.exception('%r: %r', addr, e)
        except Exception as e:
            self.log.exception('%r: %r', addr, e)
        finally:
            itrans.close()
            otrans.close()
            self.cfg.post_connect_closed(self)