|
|
@@ -109,6 +109,86 @@ esp_err_t dispatch_camera_http_request_async(httpd_req_t *req,
|
|
|
return ESP_OK;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+// General bounded worker for responses that may block on socket transmission.
|
|
|
+// 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;
|
|
|
+
|
|
|
+struct AsyncHttpRequest
|
|
|
+{
|
|
|
+ httpd_req_t *request;
|
|
|
+ basic_auth_async_handler_t handler;
|
|
|
+ char handlerName[32];
|
|
|
+ char uri[128];
|
|
|
+ int64_t startUs;
|
|
|
+};
|
|
|
+
|
|
|
+static void async_http_request_task(void *argument)
|
|
|
+{
|
|
|
+ (void)argument;
|
|
|
+ AsyncHttpRequest work = {};
|
|
|
+
|
|
|
+ while (httpAsyncRequestQueue != NULL &&
|
|
|
+ xQueueReceive(httpAsyncRequestQueue, &work, portMAX_DELAY) == pdTRUE)
|
|
|
+ {
|
|
|
+ const esp_err_t result = work.handler(work.request);
|
|
|
+ const int64_t elapsedUs = esp_timer_get_time() - work.startUs;
|
|
|
+
|
|
|
+ if (elapsedUs >= 100000)
|
|
|
+ {
|
|
|
+ LogFile.WriteToFile(ESP_LOG_WARN, TAG,
|
|
|
+ std::string("Async HTTP response: ") + work.handlerName +
|
|
|
+ " " + work.uri + " took " +
|
|
|
+ std::to_string(static_cast<long long>(elapsedUs / 1000)) +
|
|
|
+ " ms (result=" + std::to_string(static_cast<int>(result)) + ")");
|
|
|
+ }
|
|
|
+
|
|
|
+ httpd_req_async_handler_complete(work.request);
|
|
|
+ }
|
|
|
+ vTaskDelete(NULL);
|
|
|
+}
|
|
|
+
|
|
|
+esp_err_t dispatch_http_request_async(httpd_req_t *req,
|
|
|
+ basic_auth_async_handler_t handler,
|
|
|
+ const char *handler_name)
|
|
|
+{
|
|
|
+ if (req == NULL || handler == NULL) {
|
|
|
+ return ESP_ERR_INVALID_ARG;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Preserve normal serving behaviour if worker initialization failed.
|
|
|
+ if (httpAsyncRequestQueue == NULL) {
|
|
|
+ return handler(req);
|
|
|
+ }
|
|
|
+
|
|
|
+ httpd_req_t *asyncRequest = NULL;
|
|
|
+ const esp_err_t copyResult = httpd_req_async_handler_begin(req, &asyncRequest);
|
|
|
+ if (copyResult != ESP_OK) {
|
|
|
+ httpd_resp_set_status(req, "503 Service Unavailable");
|
|
|
+ httpd_resp_sendstr(req, "Could not queue HTTP response");
|
|
|
+ return ESP_OK;
|
|
|
+ }
|
|
|
+
|
|
|
+ AsyncHttpRequest work = {};
|
|
|
+ work.request = asyncRequest;
|
|
|
+ work.handler = handler;
|
|
|
+ strlcpy(work.handlerName, handler_name != NULL ? handler_name : "http",
|
|
|
+ sizeof(work.handlerName));
|
|
|
+ strlcpy(work.uri, asyncRequest->uri, sizeof(work.uri));
|
|
|
+ work.startUs = esp_timer_get_time();
|
|
|
+
|
|
|
+ if (xQueueSend(httpAsyncRequestQueue, &work, 0) != pdTRUE) {
|
|
|
+ // 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);
|
|
|
+ httpd_resp_set_status(req, "503 Service Unavailable");
|
|
|
+ httpd_resp_sendstr(req, "HTTP response worker is busy");
|
|
|
+ }
|
|
|
+
|
|
|
+ return ESP_OK;
|
|
|
+}
|
|
|
+
|
|
|
// All URI handlers are registered through APPLY_BASIC_AUTH_FILTER. Measuring here
|
|
|
// gives us a common duration for every HTTP endpoint, including static HTML/CSS/JS
|
|
|
// and image handlers. Time spent waiting in the ESP-IDF server queue is not included.
|
|
|
@@ -177,6 +257,23 @@ void init_basic_auth() {
|
|
|
"Failed to start asynchronous camera request worker");
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ // 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 ||
|
|
|
+ xTaskCreatePinnedToCore(async_http_request_task, "http_io",
|
|
|
+ 8 * 1024, NULL, tskIDLE_PRIORITY + 3,
|
|
|
+ NULL, 1) != pdPASS) {
|
|
|
+ if (httpAsyncRequestQueue != NULL) {
|
|
|
+ vQueueDelete(httpAsyncRequestQueue);
|
|
|
+ httpAsyncRequestQueue = NULL;
|
|
|
+ }
|
|
|
+ LogFile.WriteToFile(ESP_LOG_ERROR, TAG,
|
|
|
+ "Failed to start asynchronous HTTP response worker");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
static char *http_auth_basic(const char *username, const char *password)
|