static amqp_connection_state_t setup_amqp_connection() { amqp_connection_state_t connection = amqp_new_connection(); amqp_socket_t *socket = amqp_tcp_socket_new(connection); #ifdef FIX_SIG_PIPE // why doesn't librabbitmq do this, eh? int sockfd = amqp_get_sockfd(connection); int one = 1; // setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one)); #endif int status = amqp_socket_open(socket, rabbit_host, rabbit_port); assert_x(!status, "Opening RabbitMQ socket", __FILE__, __LINE__); die_on_amqp_error(amqp_login(connection, "/", 0, OUR_FRAME_MAX, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"), "Logging in to RabbitMQ"); amqp_channel_open(connection, 1); die_on_amqp_error(amqp_get_rpc_reply(connection), "Opening AMQP channel"); return connection; }
int main(int argc, char const * const *argv) { int socket_count = argc>1 ? atoi(argv[1]) : 1; int message_count = argc>2 ? atoi(argv[2]) : 100000; int rc; zctx_t *context = zctx_new(); assert(context); zctx_set_sndhwm(context, 1); zctx_set_linger(context, 100); // zmq 2.2 only supports up to 512 sockets assert_x(ZMQ_VERSION <= 20200 && socket_count > 512, "zeromq < 3.2 only supports up to 512 sockets"); void **sockets = zmalloc(socket_count * sizeof(void*)); int j; for (j=0; j<socket_count; j++) { void *socket = zsocket_new(context, ZMQ_PUSH); if (NULL==socket) { printf("Error occurred during %dth call to zsocket_new: %s\n", j, zmq_strerror (errno)); exit(1); } // zsocket_set_sndtimeo(socket, 10); zsocket_set_sndhwm(socket, 10); zsocket_set_linger(socket, 50); zsocket_set_reconnect_ivl(socket, 100); // 100 ms zsocket_set_reconnect_ivl_max(socket, 10 * 1000); // 10 s // printf("hwm %d\n", zsocket_hwm(socket)); // printf("sndbuf %d\n", zsocket_sndbuf(socket)); // printf("swap %d\n", zsocket_swap(socket)); // printf("linger %d\n", zsocket_linger(socket)); // printf("sndtimeo %d\n", zsocket_sndtimeo(socket)); // printf("reconnect_ivl %d\n", zsocket_reconnect_ivl(socket)); // printf("reconnect_ivl_max %d\n", zsocket_reconnect_ivl_max(socket)); rc = zsocket_connect(socket, "tcp://localhost:12345"); assert(rc==0); sockets[j] = socket; zclock_sleep(10); // sleep 10 ms } char data[MESSAGE_BODY_SIZE]; memset(data, 'a', MESSAGE_BODY_SIZE); char *exchanges[2] = {"zmq-device-1", "zmq-device-2"}; // char *keys[2] = {"1","2"}; int i = 0, queued = 0, rejected = 0; for (i=0; i<message_count; i++) { if (zsys_interrupted) break; zmsg_t *message = zmsg_new(); zmsg_addstr(message, exchanges[i%2]); zmsg_addstrf(message, "logjam.zmq.test.%d.%d", i%2, i); zmsg_addmem(message, data, MESSAGE_BODY_SIZE); // zmsg_dump(message); rc = zmsg_send_dont_wait(&message, sockets[i%socket_count]); assert(message == NULL); if (rc == 0) { queued++; } else { rejected++; } // usleep(20); usleep(1000); } printf("queued: %8d\n", queued); printf("rejected: %8d\n", rejected); for (i=0;i<socket_count;i++) { zsocket_destroy(context, sockets[i]); } free(sockets); zctx_destroy(&context); return 0; }
int main(int argc, char * const *argv) { int rc = 0; process_arguments(argc, argv); setvbuf(stdout, NULL, _IOLBF, 0); setvbuf(stderr, NULL, _IOLBF, 0); if (!quiet) printf("[I] started %s\n" "[I] sub-port: %d\n" "[I] push-port: %d\n" "[I] io-threads: %lu\n" "[I] rcv-hwm: %d\n" "[I] snd-hwm: %d\n" , argv[0], pull_port, pub_port, io_threads, rcv_hwm, snd_hwm); // load config config_file_exists = zsys_file_exists(config_file_name); if (config_file_exists) { config_file_init(); config = zconfig_load((char*)config_file_name); } // set global config zsys_init(); zsys_set_rcvhwm(10000); zsys_set_sndhwm(10000); zsys_set_pipehwm(1000); zsys_set_linger(100); zsys_set_io_threads(io_threads); // create socket to receive messages on zsock_t *receiver = zsock_new(ZMQ_SUB); assert_x(receiver != NULL, "sub socket creation failed", __FILE__, __LINE__); zsock_set_rcvhwm(receiver, rcv_hwm); // bind externally char* host = zlist_first(hosts); while (host) { if (!quiet) printf("[I] connecting to: %s\n", host); rc = zsock_connect(receiver, "%s", host); assert_x(rc == 0, "sub socket connect failed", __FILE__, __LINE__); host = zlist_next(hosts); } tracker = device_tracker_new(hosts, receiver); // create socket for publishing zsock_t *publisher = zsock_new(ZMQ_PUSH); assert_x(publisher != NULL, "pub socket creation failed", __FILE__, __LINE__); zsock_set_sndhwm(publisher, snd_hwm); rc = zsock_bind(publisher, "tcp://%s:%d", "*", pub_port); assert_x(rc == pub_port, "pub socket bind failed", __FILE__, __LINE__); // create compressor sockets zsock_t *compressor_input = zsock_new(ZMQ_PUSH); assert_x(compressor_input != NULL, "compressor input socket creation failed", __FILE__, __LINE__); rc = zsock_bind(compressor_input, "inproc://compressor-input"); assert_x(rc==0, "compressor input socket bind failed", __FILE__, __LINE__); zsock_t *compressor_output = zsock_new(ZMQ_PULL); assert_x(compressor_output != NULL, "compressor output socket creation failed", __FILE__, __LINE__); rc = zsock_bind(compressor_output, "inproc://compressor-output"); assert_x(rc==0, "compressor output socket bind failed", __FILE__, __LINE__); // create compressor agents zactor_t *compressors[MAX_COMPRESSORS]; for (size_t i = 0; i < num_compressors; i++) compressors[i] = message_decompressor_new(i); // set up event loop zloop_t *loop = zloop_new(); assert(loop); zloop_set_verbose(loop, 0); // calculate statistics every 1000 ms int timer_id = zloop_timer(loop, 1000, 0, timer_event, NULL); assert(timer_id != -1); // setup handler for the receiver socket publisher_state_t publisher_state = { .receiver = zsock_resolve(receiver), .publisher = zsock_resolve(publisher), .compressor_input = zsock_resolve(compressor_input), .compressor_output = zsock_resolve(compressor_output), }; // setup handler for compression results rc = zloop_reader(loop, compressor_output, read_zmq_message_and_forward, &publisher_state); assert(rc == 0); zloop_reader_set_tolerant(loop, compressor_output); // setup handdler for messages incoming from the outside or rabbit_listener rc = zloop_reader(loop, receiver, read_zmq_message_and_forward, &publisher_state); assert(rc == 0); zloop_reader_set_tolerant(loop, receiver); // initialize clock global_time = zclock_time(); // setup subscriptions if (subscriptions == NULL || zlist_size(subscriptions) == 0) { if (!quiet) printf("[I] subscribing to all log messages\n"); zsock_set_subscribe(receiver, ""); } else { char *subscription = zlist_first(subscriptions); while (subscription) { if (!quiet) printf("[I] subscribing to %s\n", subscription); zsock_set_subscribe(receiver, subscription); subscription = zlist_next(subscriptions); } zsock_set_subscribe(receiver, "heartbeat"); } // run the loop if (!zsys_interrupted) { if (verbose) printf("[I] starting main event loop\n"); bool should_continue_to_run = getenv("CPUPROFILE") != NULL; do { rc = zloop_start(loop); should_continue_to_run &= errno == EINTR && !zsys_interrupted; log_zmq_error(rc, __FILE__, __LINE__); } while (should_continue_to_run); if (verbose) printf("[I] main event zloop terminated with return code %d\n", rc); } zloop_destroy(&loop); assert(loop == NULL); if (!quiet) { printf("[I] received %zu messages\n", received_messages_count); printf("[I] shutting down\n"); } zlist_destroy(&hosts); zlist_destroy(&subscriptions); zsock_destroy(&receiver); zsock_destroy(&publisher); zsock_destroy(&compressor_input); zsock_destroy(&compressor_output); device_tracker_destroy(&tracker); for (size_t i = 0; i < num_compressors; i++) zactor_destroy(&compressors[i]); zsys_shutdown(); if (!quiet) printf("[I] terminated\n"); return rc; }