@@ -76,21 +76,22 @@ int sr_consume_cleanup(struct sr_context *sr_c)
76
76
return (1 );
77
77
}
78
78
79
- int sr_consume_setup (struct sr_context * sr_c )
79
+ signed int sr_consume_queue_declare (struct sr_context * sr_c , amqp_boolean_t passive )
80
80
/*
81
- declare a queue and bind it to the configured exchange.
81
+ declare a queue it to the configured exchange.
82
+
83
+ passive means don't actually declare the queue, just pretend, used to get the message count
82
84
83
85
*/
84
86
{
85
87
amqp_rpc_reply_t reply ;
86
- amqp_boolean_t passive = 0 ;
87
88
amqp_boolean_t exclusive = 0 ;
88
89
amqp_boolean_t auto_delete = 0 ;
89
90
amqp_queue_declare_ok_t * r ;
90
- struct sr_binding_s * t ;
91
91
static amqp_basic_properties_t props ;
92
92
static amqp_table_t table ;
93
93
static amqp_table_entry_t table_entries [2 ];
94
+ signed int message_count ;
94
95
95
96
int tecnt = 0 ;
96
97
@@ -121,6 +122,7 @@ int sr_consume_setup(struct sr_context *sr_c)
121
122
msg .user_headers = NULL ;
122
123
123
124
//amqp_queue_declare_ok_t *r =
125
+ message_count = -2 ;
124
126
if (sr_c -> cfg -> queueDeclare ) {
125
127
r = amqp_queue_declare (sr_c -> cfg -> broker -> conn ,
126
128
2 ,
@@ -131,13 +133,29 @@ int sr_consume_setup(struct sr_context *sr_c)
131
133
if (r ) {
132
134
sr_log_msg (LOG_INFO , "queue declared: %p messages in queue: %d\n" ,
133
135
sr_c -> cfg -> queuename , r -> message_count );
136
+ message_count = r -> message_count ;
137
+ sr_c -> metrics .brokerQueuedMessageCount = message_count ;
134
138
}
135
139
reply = amqp_get_rpc_reply (sr_c -> cfg -> broker -> conn );
136
140
if (reply .reply_type != AMQP_RESPONSE_NORMAL ) {
137
141
sr_amqp_reply_print (reply , "queue declare failed" );
138
- return ( 0 ) ;
142
+ message_count = -1 ;
139
143
}
140
144
}
145
+ return (message_count );
146
+ }
147
+
148
+ int sr_consume_setup (struct sr_context * sr_c )
149
+ {
150
+ struct sr_binding_s * t ;
151
+ amqp_rpc_reply_t reply ;
152
+ int messageCount ;
153
+
154
+ messageCount = sr_consume_queue_declare (sr_c , 0 );
155
+
156
+ if (messageCount < 0 ) {
157
+ return (0 );
158
+ }
141
159
142
160
/*
143
161
FIXME: topic bindings are not working properly...
@@ -698,6 +716,8 @@ struct sr_message_s *sr_consume(struct sr_context *sr_c)
698
716
amqp_rpc_reply_t reply ;
699
717
amqp_frame_t frame ;
700
718
int result ;
719
+ static time_t next_qdeclare_time = 0 ;
720
+ static time_t now = 0 ;
701
721
static char buf [SR_SARRAC_MAXIMUM_MESSAGE_LEN ];
702
722
amqp_basic_deliver_t * d ;
703
723
amqp_basic_properties_t * p ;
@@ -709,6 +729,31 @@ struct sr_message_s *sr_consume(struct sr_context *sr_c)
709
729
char tag [AMQP_MAX_SS ];
710
730
char value [AMQP_MAX_SS ];
711
731
struct sr_header_s * tmph ;
732
+ static time_t this_second = 0 ;
733
+ static int consumed_this_second = 0 ;
734
+
735
+
736
+
737
+ if (now == 0 ) {
738
+ time (& now );
739
+ }
740
+
741
+ // rate limiting.
742
+ sr_log_msg ( LOG_INFO , "rateMax: %d, consumed_this_second: %d\n" ,
743
+ sr_c -> cfg -> messageRateMax , consumed_this_second );
744
+ if (sr_c -> cfg -> messageRateMax > 0 ) {
745
+ if (consumed_this_second >= sr_c -> cfg -> messageRateMax ) {
746
+ sr_log_msg (LOG_INFO , "messageRateMax %d per second sleeping for a second.\n" ,
747
+ sr_c -> cfg -> messageRateMax );
748
+ sleep (1 );
749
+ time (& now );
750
+ }
751
+ if (now > this_second ) {
752
+ this_second = now ;
753
+ consumed_this_second = 0 ;
754
+ }
755
+ consumed_this_second ++ ;
756
+ }
712
757
713
758
while (msg .user_headers ) {
714
759
tmph = msg .user_headers ;
@@ -717,6 +762,14 @@ struct sr_message_s *sr_consume(struct sr_context *sr_c)
717
762
msg .user_headers = tmph -> next ;
718
763
free (tmph );
719
764
}
765
+ time (& now );
766
+ if (next_qdeclare_time == 0 ) {
767
+ next_qdeclare_time = now + 20 ;
768
+ } else if ( now > next_qdeclare_time )
769
+ {
770
+ sr_consume_queue_declare (sr_c , 1 );
771
+ next_qdeclare_time += 20 ;
772
+ }
720
773
721
774
/*
722
775
basic_ack added as per michel's algorithm prior to consuming next.
0 commit comments