|
|
@@ -114,6 +114,10 @@ esp_err_t dispatch_camera_http_request_async(httpd_req_t *req,
|
|
|
// Unlike camera requests, normal asset/image responses may arrive concurrently,
|
|
|
// so they are queued instead of rejected while another transfer is active.
|
|
|
static QueueHandle_t httpAsyncRequestQueue = NULL;
|
|
|
+// Keep one of the five HTTP server sockets available for short control/status requests.
|
|
|
+// The independent camera worker can occupy one additional socket.
|
|
|
+static SemaphoreHandle_t httpAsyncRequestSlots = NULL;
|
|
|
+static constexpr UBaseType_t MAX_ASYNC_HTTP_REQUESTS = 3;
|
|
|
|
|
|
struct AsyncHttpRequest
|
|
|
{
|
|
|
@@ -145,6 +149,9 @@ static void async_http_request_task(void *argument)
|
|
|
}
|
|
|
|
|
|
httpd_req_async_handler_complete(work.request);
|
|
|
+ if (httpAsyncRequestSlots != NULL) {
|
|
|
+ xSemaphoreGive(httpAsyncRequestSlots);
|
|
|
+ }
|
|
|
}
|
|
|
vTaskDelete(NULL);
|
|
|
}
|
|
|
@@ -158,13 +165,25 @@ esp_err_t dispatch_http_request_async(httpd_req_t *req,
|
|
|
}
|
|
|
|
|
|
// Preserve normal serving behaviour if worker initialization failed.
|
|
|
- if (httpAsyncRequestQueue == NULL) {
|
|
|
+ if (httpAsyncRequestQueue == NULL || httpAsyncRequestSlots == NULL) {
|
|
|
return handler(req);
|
|
|
}
|
|
|
|
|
|
+ // Do not let slow assets consume every HTTP server socket. With five sockets
|
|
|
+ // configured and one reserved for camera work, at most three general async
|
|
|
+ // responses may be queued or active at the same time.
|
|
|
+ if (xSemaphoreTake(httpAsyncRequestSlots, 0) != pdTRUE) {
|
|
|
+ LogFile.WriteToFile(ESP_LOG_WARN, TAG,
|
|
|
+ std::string("Async HTTP response capacity reached for ") + req->uri);
|
|
|
+ httpd_resp_set_status(req, "503 Service Unavailable");
|
|
|
+ httpd_resp_sendstr(req, "HTTP response worker is busy");
|
|
|
+ return ESP_OK;
|
|
|
+ }
|
|
|
+
|
|
|
httpd_req_t *asyncRequest = NULL;
|
|
|
const esp_err_t copyResult = httpd_req_async_handler_begin(req, &asyncRequest);
|
|
|
if (copyResult != ESP_OK) {
|
|
|
+ xSemaphoreGive(httpAsyncRequestSlots);
|
|
|
httpd_resp_set_status(req, "503 Service Unavailable");
|
|
|
httpd_resp_sendstr(req, "Could not queue HTTP response");
|
|
|
return ESP_OK;
|
|
|
@@ -182,6 +201,7 @@ esp_err_t dispatch_http_request_async(httpd_req_t *req,
|
|
|
// Queue full: complete the async copy and return an explicit overload
|
|
|
// response on the original request rather than blocking the HTTP task.
|
|
|
httpd_req_async_handler_complete(asyncRequest);
|
|
|
+ xSemaphoreGive(httpAsyncRequestSlots);
|
|
|
httpd_resp_set_status(req, "503 Service Unavailable");
|
|
|
httpd_resp_sendstr(req, "HTTP response worker is busy");
|
|
|
}
|
|
|
@@ -260,9 +280,11 @@ void init_basic_auth() {
|
|
|
|
|
|
// File and image transfers can block in httpd_resp_send_chunk() when the
|
|
|
// client/network is slow. Keep them out of the single HTTP server task.
|
|
|
- if (httpAsyncRequestQueue == NULL) {
|
|
|
- httpAsyncRequestQueue = xQueueCreate(4, sizeof(AsyncHttpRequest));
|
|
|
- if (httpAsyncRequestQueue == NULL ||
|
|
|
+ if (httpAsyncRequestQueue == NULL || httpAsyncRequestSlots == NULL) {
|
|
|
+ httpAsyncRequestQueue = xQueueCreate(MAX_ASYNC_HTTP_REQUESTS, sizeof(AsyncHttpRequest));
|
|
|
+ httpAsyncRequestSlots = xSemaphoreCreateCounting(MAX_ASYNC_HTTP_REQUESTS,
|
|
|
+ MAX_ASYNC_HTTP_REQUESTS);
|
|
|
+ if (httpAsyncRequestQueue == NULL || httpAsyncRequestSlots == NULL ||
|
|
|
xTaskCreatePinnedToCore(async_http_request_task, "http_io",
|
|
|
8 * 1024, NULL, tskIDLE_PRIORITY + 3,
|
|
|
NULL, 1) != pdPASS) {
|
|
|
@@ -270,6 +292,10 @@ void init_basic_auth() {
|
|
|
vQueueDelete(httpAsyncRequestQueue);
|
|
|
httpAsyncRequestQueue = NULL;
|
|
|
}
|
|
|
+ if (httpAsyncRequestSlots != NULL) {
|
|
|
+ vSemaphoreDelete(httpAsyncRequestSlots);
|
|
|
+ httpAsyncRequestSlots = NULL;
|
|
|
+ }
|
|
|
LogFile.WriteToFile(ESP_LOG_ERROR, TAG,
|
|
|
"Failed to start asynchronous HTTP response worker");
|
|
|
}
|