fix: Add config option to set timeout for posting events

Event posting to the event loop should not hinder the working of
HTTP Client or HTTP Server. This commit add a config option to set
the timeout for posting the events to the loop.

Closes https://github.com/espressif/esp-idf/issues/13641
This commit is contained in:
Harshit Malpani 2024-05-02 15:27:19 +05:30
parent 40ec44473c
commit 1ac2ebbeb9
No known key found for this signature in database
GPG Key ID: 441A8ACC7853D493
9 changed files with 59 additions and 9 deletions

View File

@ -29,4 +29,10 @@ menu "ESP HTTP client"
This option will enable injection of a custom tcp_transport handle, so the http operation
will be performed on top of the user defined transport abstraction (if configured)
config ESP_HTTP_CLIENT_EVENT_POST_TIMEOUT
int "Time in millisecond to wait for posting event"
default 2000
help
This config option helps in setting the time in millisecond to wait for event to be posted to the
system default event loop. Set it to -1 if you need to set timeout to portMAX_DELAY.
endmenu

View File

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -34,6 +34,11 @@ static const char *TAG = "HTTP_CLIENT";
ESP_STATIC_ASSERT((int)ESP_HTTP_CLIENT_TLS_VER_ANY == (int)ESP_TLS_VER_ANY, "Enum mismatch in esp_http_client and esp-tls");
ESP_STATIC_ASSERT((int)ESP_HTTP_CLIENT_TLS_VER_MAX <= (int)ESP_TLS_VER_TLS_MAX, "HTTP client supported TLS is not supported in esp-tls");
#if CONFIG_ESP_HTTP_CLIENT_EVENT_POST_TIMEOUT == -1
#define ESP_HTTP_CLIENT_EVENT_POST_TIMEOUT portMAX_DELAY
#else
#define ESP_HTTP_CLIENT_EVENT_POST_TIMEOUT pdMS_TO_TICKS(CONFIG_ESP_HTTP_CLIENT_EVENT_POST_TIMEOUT)
#endif
/**
* HTTP Buffer
*/
@ -202,7 +207,7 @@ static esp_err_t http_dispatch_event(esp_http_client_t *client, esp_http_client_
static void http_dispatch_event_to_event_loop(int32_t event_id, const void* event_data, size_t event_data_size)
{
esp_err_t err = esp_event_post(ESP_HTTP_CLIENT_EVENT, event_id, event_data, event_data_size, portMAX_DELAY);
esp_err_t err = esp_event_post(ESP_HTTP_CLIENT_EVENT, event_id, event_data, event_data_size, ESP_HTTP_CLIENT_EVENT_POST_TIMEOUT);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to post http_client event: %"PRId32", error: %s", event_id, esp_err_to_name(err));
}

View File

@ -52,4 +52,10 @@ menu "HTTP Server"
It internally uses a counting semaphore with count set to `LWIP_UDP_RECVMBOX_SIZE` to achieve this.
This config will slightly change API behavior to block until message gets delivered on control socket.
config HTTPD_SERVER_EVENT_POST_TIMEOUT
int "Time in millisecond to wait for posting event"
default 2000
help
This config option helps in setting the time in millisecond to wait for event to be posted to the
system default event loop. Set it to -1 if you need to set timeout to portMAX_DELAY.
endmenu

View File

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2018-2021 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2018-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -510,7 +510,7 @@ int httpd_default_recv(httpd_handle_t hd, int sockfd, char *buf, size_t buf_len,
* @param[in] req Pointer to handshake request that will be handled
* @param[in] supported_subprotocol Pointer to the subprotocol supported by this URI
* @return
* - ESP_OK : When handshake is sucessful
* - ESP_OK : When handshake is successful
* - ESP_ERR_NOT_FOUND : When some headers (Sec-WebSocket-*) are not found
* - ESP_ERR_INVALID_VERSION : The WebSocket version is not "13"
* - ESP_ERR_INVALID_STATE : Handshake was done beforehand
@ -525,7 +525,7 @@ esp_err_t httpd_ws_respond_server_handshake(httpd_req_t *req, const char *suppor
*
* @param[in] req Pointer to handshake request that will be handled
* @return
* - ESP_OK : When handshake is sucessful
* - ESP_OK : When handshake is successful
* - ESP_ERR_INVALID_ARG : Argument is invalid (null or non-WebSocket)
* - ESP_ERR_INVALID_STATE : Received only some parts of a control frame
* - ESP_FAIL : Socket failures
@ -553,6 +553,12 @@ esp_err_t httpd_sess_trigger_close_(httpd_handle_t handle, struct sock_db *sessi
* @}
*/
#if CONFIG_HTTPD_SERVER_EVENT_POST_TIMEOUT == -1
#define ESP_HTTP_SERVER_EVENT_POST_TIMEOUT portMAX_DELAY
#else
#define ESP_HTTP_SERVER_EVENT_POST_TIMEOUT pdMS_TO_TICKS(CONFIG_HTTPD_SERVER_EVENT_POST_TIMEOUT)
#endif
/**
* @brief Function to dispatch events in default event loop
*

View File

@ -42,7 +42,7 @@ ESP_EVENT_DEFINE_BASE(ESP_HTTP_SERVER_EVENT);
void esp_http_server_dispatch_event(int32_t event_id, const void* event_data, size_t event_data_size)
{
esp_err_t err = esp_event_post(ESP_HTTP_SERVER_EVENT, event_id, event_data, event_data_size, portMAX_DELAY);
esp_err_t err = esp_event_post(ESP_HTTP_SERVER_EVENT, event_id, event_data, event_data_size, ESP_HTTP_SERVER_EVENT_POST_TIMEOUT);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to post esp_http_server event: %s", esp_err_to_name(err));
}
@ -55,7 +55,7 @@ static esp_err_t httpd_accept_conn(struct httpd_data *hd, int listen_fd)
if (!httpd_is_sess_available(hd)) {
/* Queue asynchronous closure of the least recently used session */
return httpd_sess_close_lru(hd);
/* Returning from this allowes the main server thread to process
/* Returning from this allows the main server thread to process
* the queued asynchronous control message for closing LRU session.
* Since connection request hasn't been addressed yet using accept()
* therefore httpd_accept_conn() will be called again, but this time

View File

@ -18,4 +18,11 @@ menu "ESP HTTPS OTA"
- Non-encrypted communication channel with server
- Accepting firmware upgrade image from server with fake identity
config ESP_HTTPS_OTA_EVENT_POST_TIMEOUT
int "Time in millisecond to wait for posting event"
default 2000
help
This config option helps in setting the time in millisecond to wait for event to be posted to the
system default event loop. Set it to -1 if you need to set timeout to portMAX_DELAY.
endmenu

View File

@ -194,9 +194,15 @@ static const char* ota_event_name_table[] = {
"ESP_HTTPS_OTA_ABORT",
};
#if CONFIG_ESP_HTTPS_OTA_EVENT_POST_TIMEOUT == -1
#define ESP_HTTPS_OTA_EVENT_POST_TIMEOUT portMAX_DELAY
#else
#define ESP_HTTPS_OTA_EVENT_POST_TIMEOUT pdMS_TO_TICKS(CONFIG_ESP_HTTPS_OTA_EVENT_POST_TIMEOUT)
#endif
static void esp_https_ota_dispatch_event(int32_t event_id, const void* event_data, size_t event_data_size)
{
if (esp_event_post(ESP_HTTPS_OTA_EVENT, event_id, event_data, event_data_size, portMAX_DELAY) != ESP_OK) {
if (esp_event_post(ESP_HTTPS_OTA_EVENT, event_id, event_data, event_data_size, ESP_HTTPS_OTA_EVENT_POST_TIMEOUT) != ESP_OK) {
ESP_LOGE(TAG, "Failed to post https_ota event: %s", ota_event_name_table[event_id]);
}
}

View File

@ -6,4 +6,11 @@ menu "ESP HTTPS server"
help
Enable ESP HTTPS server component
config ESP_HTTPS_SERVER_EVENT_POST_TIMEOUT
int "Time in millisecond to wait for posting event"
default 2000
help
This config option helps in setting the time in millisecond to wait for event to be posted to the
system default event loop. Set it to -1 if you need to set timeout to portMAX_DELAY.
endmenu

View File

@ -25,9 +25,16 @@ typedef struct httpd_ssl_transport_ctx {
ESP_EVENT_DEFINE_BASE(ESP_HTTPS_SERVER_EVENT);
#if CONFIG_ESP_HTTPS_SERVER_EVENT_POST_TIMEOUT == -1
#define ESP_HTTPS_SERVER_EVENT_POST_TIMEOUT portMAX_DELAY
#else
#define ESP_HTTPS_SERVER_EVENT_POST_TIMEOUT pdMS_TO_TICKS(CONFIG_ESP_HTTPS_SERVER_EVENT_POST_TIMEOUT)
#endif
static void http_dispatch_event_to_event_loop(int32_t event_id, const void* event_data, size_t event_data_size)
{
esp_err_t err = esp_event_post(ESP_HTTPS_SERVER_EVENT, event_id, event_data, event_data_size, portMAX_DELAY);
esp_err_t err = esp_event_post(ESP_HTTPS_SERVER_EVENT, event_id, event_data, event_data_size, ESP_HTTPS_SERVER_EVENT_POST_TIMEOUT);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to post http_client event: %"PRId32", error: %s", event_id, esp_err_to_name(err));
}