Esempio n. 1
0
    def on_connect(self, conn):
        conn.heartbeat_count = 0

        def clear_heartbeat_counter():
            conn.heartbeat_count = 0
            heartbeat_timer.again(heartbeat_timeout)

        def heartbeat_timeout():
            conn.heartbeat_count += 1
            if conn.heartbeat_count >= self.heartbeat_count:
                conn.close(MonitorError.HeartbeatTimeout())

        heartbeat_timer = timer(0, self.heartbeat_interval, use_greenlet=True)
        heartbeat_timer.again(heartbeat_timeout)
        conn.heartbeat_timer = heartbeat_timer
        conn.clear_heartbeat_counter = clear_heartbeat_counter
Esempio n. 2
0
 def readline(self, size, timeout=None):
     assert not self.r_gr, 'read conflict'
     assert getcurrent() != hub, 'could not call block func in main loop'
     self.r_gr = getcurrent()
     if not timeout:
         try:
             return self._readline(size)
         finally:
             self.r_gr = None
     else:
         assert not self.r_timer, 'duplicated r_timer'
         self.r_timer = timer(timeout)
         self.r_timer.start(self.r_gr.throw, Timeout)
         try:
             return self._readline(size)
         finally:
             self.r_timer.stop()
             self.r_gr = self.r_timer = None
Esempio n. 3
0
 def connect(cls, address, client_side=False, timeout=None, family=AF_INET,
             type_=SOCK_STREAM, proto=0):
     assert getcurrent() != hub, 'could not call block func in main loop'
     sock = realsocket(family, type_, proto)
     errno = sock.connect_ex(address)
     if errno in connecting_error:
         rw_io = io(sock.fileno(), READ | WRITE)
         rw_gr = getcurrent()
         rw_io.start(rw_gr.switch)
         if timeout:
             rw_timer = timer(timeout)
             rw_timer.start(rw_gr.throw, Timeout)
         try:
             rw_gr.parent.switch()
         finally:
             if timeout:
                 rw_timer.stop()
             rw_io.stop()
             rw_timer = rw_io = None
         errno = sock.connect_ex(address)
     if errno != 0 and errno != EISCONN:
         raise socket_error(errno, strerror(errno))
     return cls(sock, client_side)