res_pjsip: Deny requests when threadpool queue is backed up.
[asterisk/asterisk.git] / res / res_pjsip / pjsip_distributor.c
index 9b05260..1d39e0f 100644 (file)
@@ -246,6 +246,8 @@ static pjsip_module endpoint_mod = {
        .on_rx_request = endpoint_lookup,
 };
 
+#define SIP_MAX_QUEUE 500L
+
 static pj_bool_t distributor(pjsip_rx_data *rdata)
 {
        pjsip_dialog *dlg = find_dialog(rdata);
@@ -280,7 +282,17 @@ static pj_bool_t distributor(pjsip_rx_data *rdata)
                clone->endpt_info.mod_data[endpoint_mod.id] = ao2_bump(dist->endpoint);
        }
 
-       ast_sip_push_task(serializer, distribute, clone);
+       if (ast_sip_threadpool_queue_size() > SIP_MAX_QUEUE) {
+               /* When the threadpool is backed up this much, there is a good chance that we have encountered
+                * some sort of terrible condition and don't need to be adding more work to the threadpool.
+                * It's in our best interest to send back a 503 response and be done with it.
+                */
+               pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 503, NULL, NULL, NULL);
+               ao2_cleanup(clone->endpt_info.mod_data[endpoint_mod.id]);
+               pjsip_rx_data_free_cloned(clone);
+       } else {
+               ast_sip_push_task(serializer, distribute, clone);
+       }
 
 end:
        if (dlg) {