예제 #1
0
def keepalive(connection, version_msg):
    """
    Periodically sends a ping message to the specified node to maintain open
    connection. Open connections are tracked in open set with the associated
    data stored in opendata set in Redis.
    """
    node = connection.to_addr
    version = version_msg.get('version', "")
    user_agent = version_msg.get('user_agent', "")
    start_height = version_msg.get('start_height', 0)
    now = int(time.time())
    data = node + (version, user_agent, now)

    REDIS_CONN.sadd('open', node)
    REDIS_CONN.sadd('opendata', data)

    start_height_key = "start_height:{}-{}".format(node[0], node[1])
    start_height_ttl = SETTINGS['keepalive'] * 2  # > keepalive
    REDIS_CONN.setex(start_height_key, start_height_ttl, start_height)

    while True:
        gevent.sleep(SETTINGS['keepalive'])
        REDIS_CONN.setex(start_height_key, start_height_ttl, start_height)
        try:
            connection.ping()
        except socket.error as err:
            logging.debug("Closing {} ({})".format(node, err))
            break

    connection.close()

    REDIS_CONN.srem('open', node)
    REDIS_CONN.srem('opendata', data)
예제 #2
0
파일: ping.py 프로젝트: arowser/bitnodes
def keepalive(connection, version_msg):
    """
    Periodically sends a ping message to the specified node to maintain open
    connection. Open connections are tracked in open set with the associated
    data stored in opendata set in Redis.
    """
    node = connection.to_addr
    version = version_msg.get('version', "")
    user_agent = version_msg.get('user_agent', "")
    start_height = version_msg.get('start_height', 0)
    now = int(time.time())
    data = node + (version, user_agent, now)

    REDIS_CONN.sadd('open', node)
    REDIS_CONN.sadd('opendata', data)

    start_height_key = "start_height:{}-{}".format(node[0], node[1])
    start_height_ttl = SETTINGS['keepalive'] * 2  # > keepalive
    REDIS_CONN.setex(start_height_key, start_height_ttl, start_height)

    while True:
        gevent.sleep(SETTINGS['keepalive'])
        REDIS_CONN.setex(start_height_key, start_height_ttl, start_height)
        try:
            connection.ping()
        except socket.error as err:
            logging.debug("Closing {} ({})".format(node, err))
            break

    connection.close()

    REDIS_CONN.srem('open', node)
    REDIS_CONN.srem('opendata', data)
예제 #3
0
파일: ping.py 프로젝트: damui/bitnodes
def keepalive(connection, version_msg):
    """
    Periodically sends a ping message to the specified node to maintain open
    connection. Open connections are tracked in open set with the associated
    data stored in opendata set in Redis.
    """
    node = connection.to_addr
    version = version_msg.get('version', "")
    user_agent = version_msg.get('user_agent', "")
    now = int(time.time())
    data = node + (version, user_agent, now)

    REDIS_CONN.sadd('open', node)
    REDIS_CONN.sadd('opendata', data)

    redis_pipe = REDIS_CONN.pipeline()

    last_ping = now

    while True:
        try:
            wait = int(REDIS_CONN.get('elapsed'))
        except TypeError as err:
            wait = 60

        if time.time() > last_ping + wait:
            nonce = random.getrandbits(64)
            try:
                connection.ping(nonce=nonce)
            except socket.error as err:
                logging.debug("Closing {} ({})".format(node, err))
                break
            last_ping = time.time()

            key = "ping:{}-{}:{}".format(node[0], node[1], nonce)
            redis_pipe.lpush(key, int(last_ping * 1000))  # in ms
            redis_pipe.expire(key, SETTINGS['ttl'])
            redis_pipe.execute()

        # Sink received messages to flush them off socket buffer
        try:
            connection.get_messages()
        except socket.timeout as err:
            pass
        except (ProtocolError, ConnectionError, socket.error) as err:
            logging.debug("Closing {} ({})".format(node, err))
            break

        gevent.sleep(0.3)

    connection.close()

    REDIS_CONN.srem('open', node)
    REDIS_CONN.srem('opendata', data)
예제 #4
0
def keepalive(connection, version_msg):
    """
    Periodically sends a ping message to the specified node to maintain open
    connection. Open connections are tracked in open set with the associated
    data stored in opendata set in Redis.
    """
    node = connection.to_addr
    version = version_msg.get('version', "")
    user_agent = version_msg.get('user_agent', "")
    now = int(time.time())
    data = node + (version, user_agent, now)

    REDIS_CONN.sadd('open', node)
    REDIS_CONN.sadd('opendata', data)

    redis_pipe = REDIS_CONN.pipeline()

    last_ping = now

    while True:
        try:
            wait = int(REDIS_CONN.get('elapsed'))
        except TypeError as err:
            wait = 60

        if time.time() > last_ping + wait:
            nonce = random.getrandbits(64)
            try:
                connection.ping(nonce=nonce)
            except socket.error as err:
                logging.debug("Closing {} ({})".format(node, err))
                break
            last_ping = time.time()

            key = "ping:{}-{}:{}".format(node[0], node[1], nonce)
            redis_pipe.lpush(key, int(last_ping * 1000))  # in ms
            redis_pipe.expire(key, SETTINGS['ttl'])
            redis_pipe.execute()

        # Sink received messages to flush them off socket buffer
        try:
            connection.get_messages()
        except socket.timeout as err:
            pass
        except (ProtocolError, ConnectionError, socket.error) as err:
            logging.debug("Closing {} ({})".format(node, err))
            break

        gevent.sleep(0.3)

    connection.close()

    REDIS_CONN.srem('open', node)
    REDIS_CONN.srem('opendata', data)
예제 #5
0
파일: ping.py 프로젝트: Devakkalji/bitnodes
def keepalive(connection, version_msg):
    """
    Periodically sends a ping message to the specified node to maintain open
    connection. Open connections are tracked in open set with the associated
    data stored in opendata set in Redis.
    """
    node = connection.to_addr
    version = version_msg.get('version', "")
    user_agent = version_msg.get('user_agent', "")
    now = int(time.time())
    data = node + (version, user_agent, now)

    REDIS_CONN.sadd('open', node)
    REDIS_CONN.sadd('opendata', data)

    last_ping = now

    while True:
        try:
            ttl = int(REDIS_CONN.get('elapsed'))
        except TypeError as err:
            ttl = 60

        if time.time() > last_ping + ttl:
            try:
                connection.ping()
            except socket.error as err:
                logging.debug("Closing {} ({})".format(node, err))
                break
            last_ping = time.time()

        try:
            msgs = connection.get_messages(commands=["ping"])
        except socket.timeout as err:
            pass
        except (ProtocolError, socket.error) as err:
            logging.debug("Closing {} ({})".format(node, err))
            break
        else:
            for msg in msgs:
                logging.debug("Pong {} ({})".format(node, msg['nonce']))
                connection.pong(msg['nonce'])

        gevent.sleep(0.3)

    connection.close()

    REDIS_CONN.srem('open', node)
    REDIS_CONN.srem('opendata', data)
예제 #6
0
파일: ping.py 프로젝트: yutelin/bitnodes
def keepalive(connection, version_msg):
    """
    Periodically sends a ping message to the specified node to maintain open
    connection. Open connections are tracked in open set with the associated
    data stored in opendata set in Redis.
    """
    node = connection.to_addr
    version = version_msg.get('version', "")
    user_agent = version_msg.get('user_agent', "")
    now = int(time.time())
    data = node + (version, user_agent, now)

    REDIS_CONN.sadd('open', node)
    REDIS_CONN.sadd('opendata', data)

    last_ping = now

    while True:
        try:
            ttl = int(REDIS_CONN.get('elapsed'))
        except TypeError as err:
            ttl = 60

        if time.time() > last_ping + ttl:
            try:
                connection.ping()
            except socket.error as err:
                logging.debug("Closing {} ({})".format(node, err))
                break
            last_ping = time.time()

        # Sink received messages to flush them off socket buffer
        try:
            connection.get_messages()
        except socket.timeout as err:
            pass
        except (ProtocolError, socket.error) as err:
            logging.debug("Closing {} ({})".format(node, err))
            break

        gevent.sleep(0.3)

    connection.close()

    REDIS_CONN.srem('open', node)
    REDIS_CONN.srem('opendata', data)
예제 #7
0
파일: ping.py 프로젝트: 9cat/bitnodes
def keepalive(connection, version_msg):
    """
    Periodically sends a ping message to the specified node to maintain open
    connection and yields received inv messages. Open connections are tracked
    in open set with the associated data stored in opendata set in Redis.
    """
    node = connection.to_addr
    version = version_msg.get('version', "")
    user_agent = version_msg.get('user_agent', "")
    start_height = version_msg.get('start_height', 0)
    now = int(time.time())
    data = node + (version, user_agent, now)

    REDIS_CONN.sadd('open', node)
    REDIS_CONN.sadd('opendata', data)

    start_height_key = "start_height:{}-{}".format(node[0], node[1])
    start_height_ttl = SETTINGS['keepalive'] * 2  # > keepalive
    REDIS_CONN.setex(start_height_key, start_height_ttl, start_height)

    last_ping = now
    while True:
        if time.time() > last_ping + SETTINGS['keepalive']:
            REDIS_CONN.expire(start_height_key, start_height_ttl)
            try:
                logging.debug("Ping: {}".format(node))
                connection.ping()
            except socket.error as err:
                logging.debug("Closing {} ({})".format(node, err))
                break
            last_ping = time.time()
        try:
            msgs = connection.get_messages(commands=["inv"])
        except socket.timeout as err:
            logging.debug("{}: {}".format(node, err))
        except (ProtocolError, socket.error) as err:
            logging.debug("Closing {} ({})".format(node, err))
            break
        else:
            yield msgs
        gevent.sleep()
    connection.close()

    REDIS_CONN.srem('open', node)
    REDIS_CONN.srem('opendata', data)