--- parser3/src/classes/amqp.C 2026/09/15 19:54:26 1.17 +++ parser3/src/classes/amqp.C 2026/09/20 11:22:08 1.22 @@ -8,25 +8,34 @@ #include "pa_vmethod_frame.h" #include "pa_request.h" +#include "pa_int.h" #include "pa_vstring.h" #include "pa_vhash.h" #include "pa_varray.h" #include "pa_vbool.h" #include "pa_vvoid.h" +#include "pa_vdouble.h" #include "pa_vamqp.h" #include "pa_os.h" #include "pa_globals.h" #ifdef WITH_AMQP + #include #include -#include #include -#include -#include + +#ifndef WITHOUT_AMQP_SSL +#include #endif -volatile const char * IDENT_AMQP_C="$Id: amqp.C,v 1.17 2026/09/15 19:54:26 moko Exp $" IDENT_PA_VAMQP_H; +#ifdef _MSC_VER +#pragma comment(lib, "rabbitmq.4.lib") +#endif + +#endif + +volatile const char * IDENT_AMQP_C="$Id: amqp.C,v 1.22 2026/09/20 11:22:08 moko Exp $" IDENT_PA_VAMQP_H; class MAmqp: public Methoded { public: // VStateless_class @@ -94,13 +103,15 @@ static void amqp_connect(VAmqp& self, Re const char* pass_c = "guest"; const char* vhost_c = "/"; const char* locale_c = "en_US"; - int heartbeat = 30; // seconds + int heartbeat_sec = 30; + int reconnect_interval_sec = 0; + bool tls_specified = false; +#ifndef WITHOUT_AMQP_SSL const char* tls_ca = 0; const char* tls_cert = 0; const char* tls_key = 0; - bool tls_specified = false; bool tls_verify = true; - int reconnect_interval_sec = 0; +#endif if(options){ for(HashStringValue::Iterator i(*options); i; i.next()){ @@ -119,10 +130,11 @@ static void amqp_connect(VAmqp& self, Re } else if(key=="locale"){ locale_c=value->as_string().cstr(); } else if(key=="heartbeat"){ - heartbeat=r.process(*value).as_int(); + heartbeat_sec=r.process(*value).as_int(); } else if(key=="auto_reconnect"){ reconnect_interval_sec=r.process(*value).as_int(); } else if(key=="tls"){ +#ifndef WITHOUT_AMQP_SSL tls_specified = true; if(HashStringValue* tls_options=value->get_hash()){ for(HashStringValue::Iterator t(*tls_options); t; t.next()){ @@ -140,6 +152,9 @@ static void amqp_connect(VAmqp& self, Re throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); } } +#else + throw Exception("amqp", 0, "AMQP SSL support is disabled"); +#endif } else throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); } @@ -148,6 +163,7 @@ static void amqp_connect(VAmqp& self, Re amqp_connection_state_t conn = amqp_new_connection(); amqp_socket_t* socket = 0; +#ifndef WITHOUT_AMQP_SSL if(tls_specified) { socket = amqp_ssl_socket_new(conn); if(!socket) @@ -170,7 +186,9 @@ static void amqp_connect(VAmqp& self, Re amqp_ssl_socket_set_verify_peer(socket, tls_verify && tls_ca); // If verify=true, enable hostname verification amqp_ssl_socket_set_verify_hostname(socket, tls_verify); - } else { + } else +#endif + { socket = amqp_tcp_socket_new(conn); if(!socket) throw Exception("amqp", 0, "failed to create TCP socket"); @@ -178,7 +196,7 @@ static void amqp_connect(VAmqp& self, Re status_check(self, amqp_socket_open(socket, host_c, port), tls_specified ? "open SSL socket " : "open TCP socket "); - amqp_rpc_reply_t rlogin = amqp_login(conn, vhost_c, 0, 131072, heartbeat, AMQP_SASL_METHOD_PLAIN, user_c, pass_c); + amqp_rpc_reply_t rlogin = amqp_login(conn, vhost_c, 0, 131072, heartbeat_sec, AMQP_SASL_METHOD_PLAIN, user_c, pass_c); if(rlogin.reply_type != AMQP_RESPONSE_NORMAL){ amqp_destroy_connection(conn); check(self, rlogin, "login "); @@ -202,7 +220,9 @@ static void amqp_connect(VAmqp& self, Re self.freconnect_interval = reconnect_interval_sec; } -// repairs a channel-level error cheaply: reopen on the same connection +// repairs a channel-level error cheaply: reopen on the same connection. Only valid when the server has +// already sent (or is about to send) channel.close - we're completing that handshake with close-ok, not +// initiating our own. static void amqp_repair_channel(VAmqp& self) { amqp_channel_close_ok_t close_ok; amqp_send_method(self.fconnection, self.fchannel, AMQP_CHANNEL_CLOSE_OK_METHOD, &close_ok); @@ -211,6 +231,13 @@ static void amqp_repair_channel(VAmqp& s self.fstate = VAmqp::ALIVE; } +// voluntarily closes and reopens an otherwise-healthy channel +static void amqp_reset_channel(VAmqp& self) { + amqp_channel_close(self.fconnection, self.fchannel, AMQP_REPLY_SUCCESS); + amqp_channel_open(self.fconnection, self.fchannel); + amqp_get_rpc_reply(self.fconnection); +} + void VAmqp::ensure_connected() { if(fstate == ALIVE) return; @@ -269,8 +296,15 @@ static amqp_table_t amqp_build_arguments entries[i].value.kind = AMQP_FIELD_KIND_BOOLEAN; entries[i].value.value.boolean = value.as_bool(); } else { - entries[i].value.kind = AMQP_FIELD_KIND_I64; - entries[i].value.value.i64 = (int64_t)value.as_wint(); + double d = value.as_double(); + double rounded = round(d); + if(ulp_eq_double(d, rounded)){ + entries[i].value.kind = AMQP_FIELD_KIND_I64; + entries[i].value.value.i64 = (int64_t)rounded; + } else { + entries[i].value.kind = AMQP_FIELD_KIND_F64; + entries[i].value.value.f64 = d; + } } i++; } @@ -403,6 +437,10 @@ static void _publish(Request& r, MethodP const char* v=value->as_string().cstr(); props.app_id=amqp_cstring_bytes(v); props._flags|=AMQP_BASIC_APP_ID_FLAG; + } else if(key=="cluster_id"){ + const char* v=value->as_string().cstr(); + props.cluster_id=amqp_cstring_bytes(v); + props._flags|=AMQP_BASIC_CLUSTER_ID_FLAG; } else if(key=="headers"){ props.headers=amqp_build_arguments_table(r, value->get_hash()); props._flags|=AMQP_BASIC_HEADERS_FLAG; @@ -773,6 +811,32 @@ static amqp_rpc_reply_t amqp_consume_mes } } +// Auto-object used for cancelling the consumer when the enclosing scope exits. A cancel failure here must +// never mask whatever error is already propagating, so its result is discarded rather than check()ed. +class Temp_amqp_consumer { +public: + Temp_amqp_consumer(VAmqp& aself, const char* aconsumer_tag, bool adrain): self(aself), consumer_tag(aconsumer_tag), drain(adrain) {} + ~Temp_amqp_consumer() { + if(!consumer_tag || self.fstate != VAmqp::ALIVE) return; + amqp_basic_cancel(self.fconnection, self.fchannel, amqp_cstring_bytes(consumer_tag)); + amqp_get_rpc_reply(self.fconnection); + if(drain){ + // The broker may have already sent a deliver frame for this (now cancelled) consumer before it even saw our cancel, + // so it can still be sitting in the library's own buffer. Its delivery tag belongs to the pre-reset channel and + // won't exist anymore afterward, so drain anything already in flight - otherwise it would surface later, misattributed. + amqp_envelope_t envelope; + struct timeval no_wait = {0,0}; + while(amqp_consume_message(self.fconnection, &envelope, &no_wait, 0).reply_type == AMQP_RESPONSE_NORMAL) + amqp_destroy_envelope(&envelope); + amqp_reset_channel(self); + } + } +private: + VAmqp& self; + const char* consumer_tag; + bool drain; +}; + static void _consume(Request& r, MethodParams& params) { VAmqp& self=GET_SELF(r, VAmqp); const char* queue_c=0; @@ -824,6 +888,7 @@ static void _consume(Request& r, MethodP // amqp_maybe_release_buffers() recycles ok->consumer_tag const char* consumer_tag_copy = ok ? pa_strdup((const char*)ok->consumer_tag.bytes, ok->consumer_tag.len) : 0; + Temp_amqp_consumer cancel_consumer(self, consumer_tag_copy, ack && callback); struct timeval tv; @@ -871,11 +936,6 @@ static void _consume(Request& r, MethodP } r.write(result); } - - if(ok){ - amqp_basic_cancel(self.connection(), self.channel(), amqp_cstring_bytes(consumer_tag_copy)); - check(self, amqp_get_rpc_reply(self.connection())); - } } static void _stop_consume(Request& r, MethodParams&) {