|
@@ -109,106 +109,6 @@ esp_err_t dispatch_camera_http_request_async(httpd_req_t *req,
|
|
|
return ESP_OK;
|
|
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;
|
|
|
|
|
-// 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
|
|
|
|
|
-{
|
|
|
|
|
- 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);
|
|
|
|
|
- if (httpAsyncRequestSlots != NULL) {
|
|
|
|
|
- xSemaphoreGive(httpAsyncRequestSlots);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- 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 || 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;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- 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);
|
|
|
|
|
- xSemaphoreGive(httpAsyncRequestSlots);
|
|
|
|
|
- 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
|
|
// 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
|
|
// 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.
|
|
// and image handlers. Time spent waiting in the ESP-IDF server queue is not included.
|
|
@@ -277,29 +177,6 @@ void init_basic_auth() {
|
|
|
"Failed to start asynchronous camera request worker");
|
|
"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 || 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) {
|
|
|
|
|
- if (httpAsyncRequestQueue != NULL) {
|
|
|
|
|
- vQueueDelete(httpAsyncRequestQueue);
|
|
|
|
|
- httpAsyncRequestQueue = NULL;
|
|
|
|
|
- }
|
|
|
|
|
- if (httpAsyncRequestSlots != NULL) {
|
|
|
|
|
- vSemaphoreDelete(httpAsyncRequestSlots);
|
|
|
|
|
- httpAsyncRequestSlots = 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)
|
|
static char *http_auth_basic(const char *username, const char *password)
|