Ejemplo n.º 1
0
 def writeToStream(self):
     #forward data to receiving socket
     self.buffer.flip()
     while self.buffer.remaining:
         if not self.writeStream.write(self.buffer, Timeout.current()):
             return False
     return True                   
Ejemplo n.º 2
0
    def connect(self):
        """get a connection from the pool, will wait for maxWaitTime for connection to become
        available, or will create a new connection if connectioncount < max_connections"""
        with Timeout.push(self._connect_timeout):
            if (not self._pool) and (self.connection_count < self._max_connections):
                #none available, but still allowed to create new connection
                try:
                    return (True, self._new())
                except TaskletExit:
                    raise #server exiting
                except TimeoutError:
                    raise
                except:
                    self.log.exception("%s: could not create new connection for pool", self)
                    #we will continue from here waiting for idle connection

            #if we are here, either connection is available, not available but no more connections are allowed,
            #or there was some exception creating a new connection
            self.log.debug("waiting for connection")
            with self._queue_wait_timer_statistic.time():
                #keep track off the amount of other tasks waiting for a connection
                balance = self._pool.channel.balance
                waiters = -balance if balance < 0 else 0
                self._queue_wait_tasks_statistic.set_count(waiters)
                self._queue_wait_tasks_statistic.update_avg(waiters)
                connection = self._get_connection_from_pool()
                self.log.debug("got connection")
                return (False, connection)
Ejemplo n.º 3
0
 def connect(self, addr):
     assert self._stream is None, "must not be disconneted before connecting"
     self._stream = BufferedStream(Socket.connect(addr, Timeout.current()))
     self._command_queue = Deque()
     self._response_queue = Deque()
     self._command_writer_task = Tasklet.new(self._command_writer)()
     self._response_reader_task = Tasklet.new(self._response_reader)()
Ejemplo n.º 4
0
    def connect(self):
        """get a connection from the pool, will wait for maxWaitTime for connection to become
        available, or will create a new connection if connectioncount < max_connections"""
        with Timeout.push(self._connect_timeout):
            if (not self._pool) and (self.connection_count <
                                     self._max_connections):
                #none available, but still allowed to create new connection
                try:
                    return (True, self._new())
                except TimeoutError:
                    raise
                except Exception:
                    self.log.exception(
                        "%s: could not create new connection for pool", self)
                    #we will continue from here waiting for idle connection

            #if we are here, either connection is available, not available but no more connections are allowed,
            #or there was some exception creating a new connection
            self.log.debug("waiting for connection")
            with self._queue_wait_timer_statistic.time():
                #keep track off the amount of other tasks waiting for a connection
                balance = self._pool.channel.balance
                waiters = -balance if balance < 0 else 0
                self._queue_wait_tasks_statistic.set_count(waiters)
                self._queue_wait_tasks_statistic.update_avg(waiters)
                connection = self._get_connection_from_pool()
                self.log.debug("got connection")
                return (False, connection)
Ejemplo n.º 5
0
 def connect(self, addr):
     assert self._stream is None, "must not be disconneted before connecting"
     self._stream = BufferedStream(Socket.connect(addr, Timeout.current()))
     self._command_queue = Deque()
     self._response_queue = Deque()
     self._command_writer_task = Tasklet.new(self._command_writer)()
     self._response_reader_task = Tasklet.new(self._response_reader)()
Ejemplo n.º 6
0
 def testPushPop4(self):
     self.assertEquals(TIMEOUT_NEVER, Timeout.current())
     Tasklet.set_current_timeout(10.0)
     self.assertAlmostEqual(10.0, Timeout.current(), places=1)
     Timeout.push(TIMEOUT_CURRENT)
     self.assertAlmostEqual(10.0, Timeout.current(), places=1)
     Timeout.pop()
     self.assertAlmostEqual(10.0, Timeout.current(), places=1)
     Tasklet.set_current_timeout(TIMEOUT_NEVER)
Ejemplo n.º 7
0
 def testPushPop4(self):
     self.assertEquals(TIMEOUT_NEVER, Timeout.current())
     Tasklet.set_current_timeout(10.0)
     self.assertAlmostEqual(10.0, Timeout.current(), places = 1)
     Timeout.push(TIMEOUT_CURRENT)
     self.assertAlmostEqual(10.0, Timeout.current(), places = 1)
     Timeout.pop()
     self.assertAlmostEqual(10.0, Timeout.current(), places = 1)
     Tasklet.set_current_timeout(TIMEOUT_NEVER)
Ejemplo n.º 8
0
 def connect(cls, endpoint):
     if isinstance(endpoint, Connector):
         assert False, "TODO"
     else:
         #default is to connect to Socket and endpoint is address
         from concurrence.io.socket import Socket
         from concurrence.timer import Timeout
         return Socket.connect(endpoint, Timeout.current())
Ejemplo n.º 9
0
 def connect(cls, endpoint):
     if isinstance(endpoint, Connector):
         assert False, "TODO"
     else:
         #default is to connect to Socket and endpoint is address
         from concurrence.io.socket import Socket
         from concurrence.timer import Timeout
         return Socket.connect(endpoint, Timeout.current())
Ejemplo n.º 10
0
 def readFromStream(self):
     #read some data from stream into buffer
     if self.remaining:
         #some leftover partially read packet from previous read, put it in front of buffer
         self.buffer.limit = self.buffer.position + self.remaining
         self.buffer.compact()
     else:
         #normal clear, position = 0, limit = capacity
         self.buffer.clear()
     #read data from socket
     return self.readStream.read(self.buffer, Timeout.current())
Ejemplo n.º 11
0
    def connect(self,
                host="localhost",
                port=3306,
                user="",
                passwd="",
                db="",
                autocommit=None,
                charset=None):
        """connects to the given host and port with user and passwd"""
        #self.log.debug("connect mysql client %s %s %s %s %s", id(self), host, port, user, passwd)
        try:
            #print 'connect', host, user, passwd, db
            #parse addresses of form str <host:port>
            if type(host) == str:
                if host[0] == '/':  #assume unix domain socket
                    addr = host
                elif ':' in host:
                    host, port = host.split(':')
                    port = int(port)
                    addr = (host, port)
                else:
                    addr = (host, port)

            assert self.state == self.STATE_INIT, "make sure connection is not already connected or closed"

            self.state = self.STATE_CONNECTING
            self.socket = Socket.connect(addr, timeout=Timeout.current())
            self.reader = BufferedPacketReader(self.socket, self.buffer)
            self.writer = BufferedPacketWriter(self.socket, self.buffer)
            self._handshake(user, passwd, db)
            #handshake complete client can now send commands
            self.state = self.STATE_CONNECTED

            if autocommit == False:
                self.set_autocommit(False)
            elif autocommit == True:
                self.set_autocommit(True)
            else:
                pass  #whatever is the default of the db (ON in the case of mysql)

            if charset is not None:
                self.set_charset(charset)

            return self
        except TimeoutError:
            self.state = self.STATE_INIT
            raise
        except ClientLoginError:
            self.state = self.STATE_INIT
            raise
        except:
            self.state = self.STATE_ERROR
            raise
Ejemplo n.º 12
0
    def testTimer(self):

        ch = Channel()

        def sender(times):
            for i in range(times):
                Tasklet.sleep(1.0)
                ch.send(True)

        with Timeout.push(10):
            Tasklet.new(sender)(4)
            for i in range(4):
                ch.receive(Timeout.current())

        start = time.time()
        try:
            with Timeout.push(2.5):
                Tasklet.new(sender)(4)
                for i in range(4):
                    ch.receive(Timeout.current())
                self.fail('expected timeout')
        except TimeoutError, e:
            end = time.time()
            self.assertAlmostEqual(2.5, end - start, places = 1)
Ejemplo n.º 13
0
    def testTimer(self):

        ch = Channel()

        def sender(times):
            for i in range(times):
                Tasklet.sleep(1.0)
                ch.send(True)

        with Timeout.push(10):
            Tasklet.new(sender)(4)
            for i in range(4):
                ch.receive(Timeout.current())

        start = time.time()
        try:
            with Timeout.push(2.5):
                Tasklet.new(sender)(4)
                for i in range(4):
                    ch.receive(Timeout.current())
                self.fail('expected timeout')
        except TimeoutError, e:
            end = time.time()
            self.assertAlmostEqual(2.5, end - start, places=1)
Ejemplo n.º 14
0
    def connect(self, host = "localhost", port = 3306, user = "", passwd = "", db = "", autocommit = None, charset = None):
        """connects to the given host and port with user and passwd"""
        #self.log.debug("connect mysql client %s %s %s %s %s", id(self), host, port, user, passwd)
        try:
            #print 'connect', host, user, passwd, db
            #parse addresses of form str <host:port>
            if type(host) == str:
                if host[0] == '/': #assume unix domain socket
                    addr = host 
                elif ':' in host:
                    host, port = host.split(':')
                    port = int(port)
                    addr = (host, port)
                else:
                    addr = (host, port)

            assert self.state == self.STATE_INIT, "make sure connection is not already connected or closed"

            self.state = self.STATE_CONNECTING
            self.socket = Socket.connect(addr, timeout = Timeout.current())
            self.reader = BufferedPacketReader(self.socket, self.buffer)
            self.writer = BufferedPacketWriter(self.socket, self.buffer)
            self._handshake(user, passwd, db)
            #handshake complete client can now send commands
            self.state = self.STATE_CONNECTED
            
            if autocommit == False:
                self.set_autocommit(False)
            elif autocommit == True:
                self.set_autocommit(True)
            else:
                pass #whatever is the default of the db (ON in the case of mysql)

            if charset is not None:
                self.set_charset(charset)
            
            return self
        except TimeoutError:
            self.state = self.STATE_INIT
            raise
        except ClientLoginError:
            self.state = self.STATE_INIT
            raise
        except:
            self.state = self.STATE_ERROR
            raise
Ejemplo n.º 15
0
    def testMySQLTimeout(self):
        cnn = client.connect(host = DB_HOST, user = DB_USER, 
                             passwd = DB_PASSWD, db = DB_DB)

        rs = cnn.query("select sleep(2)")
        list(rs)
        rs.close()
        
        from concurrence import TimeoutError
        from concurrence.timer import Timeout

        start = time.time()
        try:
            with Timeout.push(2.0):
                cnn.query("select sleep(4)")
                self.fail('expected timeout')
        except TimeoutError, e:
            end = time.time()
            self.assertAlmostEqual(2.0, end - start, places = 1)
Ejemplo n.º 16
0
    def testMySQLTimeout(self):
        cnn = client.connect(host = DB_HOST, user = DB_USER,
                             passwd = DB_PASSWD, db = DB_DB)

        rs = cnn.query("select sleep(2)")
        list(rs)
        rs.close()

        from concurrence import TimeoutError
        from concurrence.timer import Timeout

        start = time.time()
        try:
            with Timeout.push(2.0):
                cnn.query("select sleep(4)")
                self.fail('expected timeout')
        except TimeoutError, e:
            end = time.time()
            self.assertAlmostEqual(2.0, end - start, places = 1)
Ejemplo n.º 17
0
 def loop(self):
     "Infinitely read serial input and parse packets"
     with self.stream.get_reader() as reader:
         while True:
             try:
                 # wait for preamble
                 while True:
                     data = ord(reader.read_bytes(1))
                     if data == 0x5a:
                         break
                 # read packet length
                 _crc = 0
                 pktlen = ord(reader.read_bytes(1))
                 _crc = crc(_crc, pktlen)
                 # read packet data
                 if pktlen > 0:
                     data = []
                     try:
                         with Timeout.push(1):
                             for d in reader.read_bytes(pktlen):
                                 d = ord(d)
                                 data.append(d)
                                 _crc = crc(_crc, d)
                             # read CRC
                             _crc = crc(_crc, ord(reader.read_bytes(1)))
                     except TimeoutError:
                         pass
                     else:
                         # check CRC
                         if _crc == 0:
                             #print "%s recv: %s" % (self._devname, ", ".join(["0x%02x" % d for d in data]))
                             if self._calibrated:
                                 if len(data) == 1 and (data[0] == ord('I')
                                                        or data[0] == 0xf2):
                                     self._calibrated = False
                                     Tasklet.new(
                                         self.auto_calibrate_baudrate)()
                             if self._calibrated or data[0] == 0xf0 or data[
                                     0] == ord('E'):
                                 self.dispatcher.receive(data)
             except Exception as e:
                 logging.exception(e)
Ejemplo n.º 18
0
 def loop(self):
     "Infinitely read serial input and parse packets"
     with self.stream.get_reader() as reader:
         while True:
             try:
                 # wait for preamble
                 while True:
                     data = ord(reader.read_bytes(1))
                     if data == 0x5a:
                         break
                 # read packet length
                 _crc = 0
                 pktlen = ord(reader.read_bytes(1))
                 _crc = crc(_crc, pktlen)
                 # read packet data
                 if pktlen > 0:
                     data = []
                     try:
                         with Timeout.push(1):
                             for d in reader.read_bytes(pktlen):
                                 d = ord(d)
                                 data.append(d)
                                 _crc = crc(_crc, d)
                             # read CRC
                             _crc = crc(_crc, ord(reader.read_bytes(1)))
                     except TimeoutError:
                         pass
                     else:
                         # check CRC
                         if _crc == 0:
                             #print "%s recv: %s" % (self._devname, ", ".join(["0x%02x" % d for d in data]))
                             if self._calibrated:
                                 if len(data) == 1 and (data[0] == ord('I') or data[0] == 0xf2):
                                     self._calibrated = False
                                     Tasklet.new(self.auto_calibrate_baudrate)()
                             if self._calibrated or data[0] == 0xf0 or data[0] == ord('E'):
                                 self.dispatcher.receive(data)
             except Exception as e:
                 logging.exception(e)
Ejemplo n.º 19
0
 def __call__(self, next, *args, **kwargs):
     timeout = float(self.request.environ.get('HTTP_TIMEOUT', '-1'))
     with Timeout.push(timeout):
         return next(*args, **kwargs)
Ejemplo n.º 20
0
 def _read_more(self):
     #any partially read data will be put in front, otherwise normal clear:
     self.buffer.compact()
     if not self.stream.read(self.buffer, Timeout.current()): 
         raise EOFError("while reading")
     self.buffer.flip() #prepare to read from buffer
Ejemplo n.º 21
0
 def _get_connection_from_pool(self):
     self.log.debug("get conn from pool")
     return self._pool.pop(True, Timeout.current())
Ejemplo n.º 22
0
 def read_request(self, reader):
     with Timeout.push(self._server.read_timeout):
         self._read_request(reader)
Ejemplo n.º 23
0
 def __call__(self, next, *args, **kwargs):
     timeout = float(self.request.environ.get('HTTP_TIMEOUT', '-1'))
     with Timeout.push(timeout):
         return next(*args, **kwargs)
Ejemplo n.º 24
0
    def testPushPop2(self):

        self.assertEquals(TIMEOUT_NEVER, Timeout.current())
        Timeout.push(TIMEOUT_NEVER)
        self.assertEquals(TIMEOUT_NEVER, Timeout.current())
        Timeout.pop()
        self.assertEquals(TIMEOUT_NEVER, Timeout.current())

        Timeout.push(10)
        self.assertAlmostEqual(10, Timeout.current(), places = 1)
        Timeout.push(5)
        self.assertAlmostEqual(5, Timeout.current(), places = 1)
        Timeout.pop()
        self.assertAlmostEqual(10, Timeout.current(), places = 1)
        Timeout.pop()
        self.assertEquals(TIMEOUT_NEVER, Timeout.current())
Ejemplo n.º 25
0
    def testPushPop(self):

        self.assertEquals(TIMEOUT_NEVER, Timeout.current())

        Timeout.push(30)
        self.assertAlmostEqual(30, Timeout.current(), places=1)
        Timeout.pop()
        self.assertEquals(TIMEOUT_NEVER, Timeout.current())
        Timeout.push(30)
        self.assertAlmostEqual(30, Timeout.current(), places=1)
        Tasklet.sleep(1.0)
        self.assertAlmostEqual(29, Timeout.current(), places=1)
        #push a temporary short timeout
        Timeout.push(5)
        self.assertAlmostEqual(5, Timeout.current(), places=1)
        Timeout.pop()
        self.assertAlmostEqual(29, Timeout.current(), places=1)

        #try to push a new longer timeout than the parent timeout
        #this should fail, e.g. it will keep the parent timeout
        Timeout.push(60)
        self.assertAlmostEqual(29, Timeout.current(), places=1)
        Timeout.pop()
        self.assertAlmostEqual(29, Timeout.current(), places=1)
        Timeout.pop()
        self.assertEquals(TIMEOUT_NEVER, Timeout.current())
Ejemplo n.º 26
0
    def testPushPop2(self):

        self.assertEquals(-1, Timeout.current())
        Timeout.push(-1)
        self.assertEquals(-1, Timeout.current())
        Timeout.pop()
        self.assertEquals(-1, Timeout.current())

        Timeout.push(10)
        self.assertAlmostEqual(10, Timeout.current(), places=1)
        Timeout.push(5)
        self.assertAlmostEqual(5, Timeout.current(), places=1)
        Timeout.pop()
        self.assertAlmostEqual(10, Timeout.current(), places=1)
        Timeout.pop()
        self.assertEquals(-1, Timeout.current())
Ejemplo n.º 27
0
 def flush(self):
     self.buffer.flip()
     while self.buffer.remaining:
         if not self.stream.write(self.buffer, Timeout.current()):
             raise EOFError("while writing")
     self.buffer.clear()
Ejemplo n.º 28
0
 def _get_connection_from_pool(self):
     self.log.debug("get conn from pool")
     return self._pool.pop(True, Timeout.current())
Ejemplo n.º 29
0
 def read_request(self, reader):
     with Timeout.push(self._server.read_timeout):
         self._read_request(reader)
Ejemplo n.º 30
0
    def testPushPop(self):

        self.assertEquals(TIMEOUT_NEVER, Timeout.current())

        Timeout.push(30)
        self.assertAlmostEqual(30, Timeout.current(), places = 1)
        Timeout.pop()
        self.assertEquals(TIMEOUT_NEVER, Timeout.current())
        Timeout.push(30)
        self.assertAlmostEqual(30, Timeout.current(), places = 1)
        Tasklet.sleep(1.0)
        self.assertAlmostEqual(29, Timeout.current(), places = 1)
        #push a temporary short timeout
        Timeout.push(5)
        self.assertAlmostEqual(5, Timeout.current(), places = 1)
        Timeout.pop()
        self.assertAlmostEqual(29, Timeout.current(), places = 1)

        #try to push a new longer timeout than the parent timeout
        #this should fail, e.g. it will keep the parent timeout
        Timeout.push(60)
        self.assertAlmostEqual(29, Timeout.current(), places = 1)
        Timeout.pop()
        self.assertAlmostEqual(29, Timeout.current(), places = 1)
        Timeout.pop()
        self.assertEquals(TIMEOUT_NEVER, Timeout.current())
Ejemplo n.º 31
0
 def connect(self):
     self.log.debug("connect: %s", self)
     with Timeout.push(self._connect_timeout):
         return (True, self._new())
Ejemplo n.º 32
0
    def testPushPop2(self):

        self.assertEquals(TIMEOUT_NEVER, Timeout.current())
        Timeout.push(TIMEOUT_NEVER)
        self.assertEquals(TIMEOUT_NEVER, Timeout.current())
        Timeout.pop()
        self.assertEquals(TIMEOUT_NEVER, Timeout.current())

        Timeout.push(10)
        self.assertAlmostEqual(10, Timeout.current(), places=1)
        Timeout.push(5)
        self.assertAlmostEqual(5, Timeout.current(), places=1)
        Timeout.pop()
        self.assertAlmostEqual(10, Timeout.current(), places=1)
        Timeout.pop()
        self.assertEquals(TIMEOUT_NEVER, Timeout.current())
Ejemplo n.º 33
0
 def connect(self):
     self.log.debug("connect: %s", self)
     with Timeout.push(self._connect_timeout):
         return (True, self._new())
Ejemplo n.º 34
0
 def testPushPop2(self):
     
     self.assertEquals(-1, Timeout.current())
     Timeout.push(-1)
     self.assertEquals(-1, Timeout.current())
     Timeout.pop()
     self.assertEquals(-1, Timeout.current())
     
     Timeout.push(10)
     self.assertAlmostEqual(10, Timeout.current(), places = 1)        
     Timeout.push(5)
     self.assertAlmostEqual(5, Timeout.current(), places = 1)
     Timeout.pop()
     self.assertAlmostEqual(10, Timeout.current(), places = 1)
     Timeout.pop()
     self.assertEquals(-1, Timeout.current())