Пример #1
0
def main():
    """Main function, in charge of make the connection to the server
    """

    context = zmq.Context()
    archiver = context.socket(zmq.PULL)
    archiver.connect("tcp://localhost:12001")

    try:
        with MessageProfiler(True) as mp:
            while True:
                rkey, message = archiver.recv_multipart()
                mp.msg_received(sys.getsizeof(rkey + message))
                # print("Received message: [%s] RKEY: [%s]" % (message, rkey))
                handle_message(message, rkey)

    except Exception, e:
        archiver.close()
        context.term()
        raise e
Пример #2
0
from message_profiler import MessageProfiler

# Getting context and defining bindings
context = zmq.Context()

queue = context.socket(zmq.SUB)
pub = context.socket(zmq.PUSH)

queue.connect("tcp://localhost:11001")
queue.setsockopt(zmq.SUBSCRIBE, 'routing_key.example')

pub.bind("tcp://*:12001")

try:
    with MessageProfiler(True) as mp:
        # Broker (receive and deliver)
        while True:
            rkey, message = queue.recv_multipart()
            bytes = sys.getsizeof(rkey + message)
            mp.msg_received(bytes)

            # print("Received and sending message [%s] RKEY: [%s]" % (message, rkey))
            # processing, persistence?, ACK handling ?

            pub.send_multipart([rkey, message])
            mp.msg_sent(bytes)
except:
    queue.close()
    pub.close()
    context.term()
    # Getting context and defining bindings
    context = zmq.Context()

    rcv = context.socket(getattr(
        zmq,
        config['incoming']['socket_type']))
    pub = context.socket(getattr(
        zmq,
        config['outgoing']['socket_type']))

    rcv.bind("tcp://{host}:{port}".format(**config['incoming']))
    pub.bind("tcp://{host}:{port}".format(**config['outgoing']))

    try:
        with MessageProfiler(CONFIG_SECTION, True) as mp:

            while True:
                rkey, message = rcv.recv_multipart()
                print("Received message [%s] RKEY: [%s]" % (message, rkey))

                if rkey != 'loanServiceReply':
                    print("[WARNING] Wrong rkey for message [%s] RKEY: [%s]" % (message, rkey))
                    continue
                
                message = json.loads(message)

                size_str = sys.getsizeof(rkey + str(message))
                mp.msg_received(size_str)

                message['profiler']['loanServiceReplyPT_ts'] = time.time()
Пример #4
0
from message_profiler import MessageProfiler

with MessageProfiler('test', True) as mp:
    for i in xrange(100):
        mp.msg_sent(100)

        mp.msg_received(100)

import pdb
pdb.set_trace()

print mp

print 1

print 2
Пример #5
0
def run(config,
        message_patterns,
        port,
        message_type,
        client_id,
        app_time=APP_TIME,
        limit_amount=0,
        limit_time=0,
        start_time=time.time()):

    # Getting context and defining bindings
    context = zmq.Context()

    queue_loan = context.socket(
        getattr(zmq, config['incoming']['loanService']['socket_type']))
    # queue_risk = context.socket(getattr(
    #     zmq,
    #     config['incoming']['risk']['socket_type']))
    pub = context.socket(getattr(zmq, config['outgoing']['socket_type']))

    queue_loan.connect(
        "tcp://{host}:{port}".format(**config['incoming']['loanService']))
    queue_loan.setsockopt(
        zmq.SUBSCRIBE, str(config['incoming']['loanService']['routing_key']))
    client_binding = "{0}_{1}".format(
        str(client_id), config['incoming']['loanService']['routing_key'])
    queue_loan.setsockopt(zmq.SUBSCRIBE, str(client_binding))

    # queue_risk.bind("tcp://{host}:{port}".format(**config['incoming']['risk']))
    # queue_risk.connect("tcp://{host}:{port}".format(**config['incoming']['risk']))
    # queue_risk.setsockopt(zmq.SUBSCRIBE, config['incoming']['risk']['routing_key'])

    pub.connect("tcp://{host}:{port}".format(**config['outgoing']))

    # poller = zmq.Poller()
    # poller.register(queue_loan, zmq.POLLIN)
    # poller.register(queue_risk, zmq.POLLIN)

    timeout = (APP_TIME - (time.time() - start_time)) + 30

    try:
        with MessageProfiler(CONFIG_SECTION, True) as mp:

            with Timeout(timeout):
                while ((limit_amount == 0 or mp.count_in <= limit_amount)
                       and (limit_time == 0
                            or time.time() - start_time <= limit_time)):
                    # socks = dict(poller.poll())

                    # if socks.get(queue_loan) == zmq.POLLIN:
                    #     rkey, message = queue_loan.recv_multipart()
                    # elif socks.get(queue_risk) == zmq.POLLIN:
                    # rkey, message = queue_risk.recv_multipart()
                    # else:
                    #     print("[WARNING] Wrong socket for message [%s] RKEY: [%s]" % (message, rkey))
                    #     continue

                    try:
                        app_time = (APP_TIME - (time.time() - start_time))
                        rkey, message = queue_loan.recv_multipart()

                        print("NORMAL - Received message [%s] RKEY: [%s]" %
                              (message, rkey))

                        if 'loanApproval' not in rkey:
                            print(
                                "[WARNING] Wrong rkey for message [%s] RKEY: [%s]"
                                % (message, rkey))
                            continue

                        size_str = sys.getsizeof(rkey + str(message))
                        mp.msg_received(size_str)

                        message = json.loads(message)
                        message['profiler']['loanApprovalPT_ts'] = time.time()
                        message['accept'] = 'yes'
                        rkey = str(config['outgoing']['routing_key'])
                        time.sleep(app_time)

                        pub.send_multipart([rkey, json.dumps(message)])
                        print("NORMAL - Sent message [%s] RKEY: [%s]" %
                              (message, rkey))

                        size_str = sys.getsizeof(rkey + str(message))
                        mp.msg_sent(size_str)
                    except Timeout.Timeout:
                        message['accept'] = 'no'
                        size_str = sys.getsizeof(rkey + str(message))
                        mp.msg_received(size_str)

                        pub.send_multipart([rkey, json.dumps(message)])
                        print("TIMEOUT - Sent message [%s] RKEY: [%s]" %
                              (message, rkey))

                        size_str = sys.getsizeof(rkey + str(message))
                        mp.msg_sent(size_str)
                        return mp.stats

                    except AttributeError:
                        pass
                        return None
                    except Exception, e:
                        pass
                        print "ERROR: ", e
                        return None

            return mp.stats

    except:
        # raise
        queue_loan.close()
        # queue_risk.close()
        pub.close()
        context.term()
        # raise
        return None