kopia lustrzana https://github.com/espressif/esp-idf
esp_http_server/lru_counter_for_timestamp:Added LRU counter for timer
Replaced the os timer in http server by LRU counter. The timestamp is unnecessary for LRU.pull/3110/head
rodzic
5ec58c316d
commit
4c8e997289
|
@ -623,7 +623,7 @@ esp_err_t httpd_sess_set_pending_override(httpd_handle_t hd, int sockfd, httpd_p
|
||||||
* session socket fd, from within a URI handler, ie. :
|
* session socket fd, from within a URI handler, ie. :
|
||||||
* httpd_sess_get_ctx(),
|
* httpd_sess_get_ctx(),
|
||||||
* httpd_sess_trigger_close(),
|
* httpd_sess_trigger_close(),
|
||||||
* httpd_sess_update_timestamp().
|
* httpd_sess_update_lru_counter().
|
||||||
*
|
*
|
||||||
* @note This API is supposed to be called only from the context of
|
* @note This API is supposed to be called only from the context of
|
||||||
* a URI handler where httpd_req_t* request pointer is valid.
|
* a URI handler where httpd_req_t* request pointer is valid.
|
||||||
|
@ -1215,15 +1215,15 @@ void *httpd_get_global_transport_ctx(httpd_handle_t handle);
|
||||||
esp_err_t httpd_sess_trigger_close(httpd_handle_t handle, int sockfd);
|
esp_err_t httpd_sess_trigger_close(httpd_handle_t handle, int sockfd);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Update timestamp for a given socket
|
* @brief Update LRU counter for a given socket
|
||||||
*
|
*
|
||||||
* Timestamps are internally associated with each session to monitor
|
* LRU Counters are internally associated with each session to monitor
|
||||||
* how recently a session exchanged traffic. When LRU purge is enabled,
|
* how recently a session exchanged traffic. When LRU purge is enabled,
|
||||||
* if a client is requesting for connection but maximum number of
|
* if a client is requesting for connection but maximum number of
|
||||||
* sockets/sessions is reached, then the session having the earliest
|
* sockets/sessions is reached, then the session having the earliest
|
||||||
* timestamp is closed automatically.
|
* LRU counter is closed automatically.
|
||||||
*
|
*
|
||||||
* Updating the timestamp manually prevents the socket from being purged
|
* Updating the LRU counter manually prevents the socket from being purged
|
||||||
* due to the Least Recently Used (LRU) logic, even though it might not
|
* due to the Least Recently Used (LRU) logic, even though it might not
|
||||||
* have received traffic for some time. This is useful when all open
|
* have received traffic for some time. This is useful when all open
|
||||||
* sockets/session are frequently exchanging traffic but the user specifically
|
* sockets/session are frequently exchanging traffic but the user specifically
|
||||||
|
@ -1234,15 +1234,15 @@ esp_err_t httpd_sess_trigger_close(httpd_handle_t handle, int sockfd);
|
||||||
* is enabled.
|
* is enabled.
|
||||||
*
|
*
|
||||||
* @param[in] handle Handle to server returned by httpd_start
|
* @param[in] handle Handle to server returned by httpd_start
|
||||||
* @param[in] sockfd The socket descriptor of the session for which timestamp
|
* @param[in] sockfd The socket descriptor of the session for which LRU counter
|
||||||
* is to be updated
|
* is to be updated
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
* - ESP_OK : Socket found and timestamp updated
|
* - ESP_OK : Socket found and LRU counter updated
|
||||||
* - ESP_ERR_NOT_FOUND : Socket not found
|
* - ESP_ERR_NOT_FOUND : Socket not found
|
||||||
* - ESP_ERR_INVALID_ARG : Null arguments
|
* - ESP_ERR_INVALID_ARG : Null arguments
|
||||||
*/
|
*/
|
||||||
esp_err_t httpd_sess_update_timestamp(httpd_handle_t handle, int sockfd);
|
esp_err_t httpd_sess_update_lru_counter(httpd_handle_t handle, int sockfd);
|
||||||
|
|
||||||
/** End of Session
|
/** End of Session
|
||||||
* @}
|
* @}
|
||||||
|
|
|
@ -125,7 +125,7 @@ struct sock_db {
|
||||||
httpd_send_func_t send_fn; /*!< Send function for this socket */
|
httpd_send_func_t send_fn; /*!< Send function for this socket */
|
||||||
httpd_recv_func_t recv_fn; /*!< Receive function for this socket */
|
httpd_recv_func_t recv_fn; /*!< Receive function for this socket */
|
||||||
httpd_pending_func_t pending_fn; /*!< Pending function for this socket */
|
httpd_pending_func_t pending_fn; /*!< Pending function for this socket */
|
||||||
int64_t timestamp; /*!< Timestamp indicating when the socket was last used */
|
uint64_t lru_counter; /*!< LRU Counter indicating when the socket was last used */
|
||||||
char pending_data[PARSER_BLOCK_SIZE]; /*!< Buffer for pending data to be received */
|
char pending_data[PARSER_BLOCK_SIZE]; /*!< Buffer for pending data to be received */
|
||||||
size_t pending_len; /*!< Length of pending data to be received */
|
size_t pending_len; /*!< Length of pending data to be received */
|
||||||
};
|
};
|
||||||
|
|
|
@ -195,6 +195,12 @@ static int fd_is_valid(int fd)
|
||||||
return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
|
return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline uint64_t httpd_sess_get_lru_counter()
|
||||||
|
{
|
||||||
|
static uint64_t lru_counter = 0;
|
||||||
|
return lru_counter++;
|
||||||
|
}
|
||||||
|
|
||||||
void httpd_sess_delete_invalid(struct httpd_data *hd)
|
void httpd_sess_delete_invalid(struct httpd_data *hd)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < hd->config.max_open_sockets; i++) {
|
for (int i = 0; i < hd->config.max_open_sockets; i++) {
|
||||||
|
@ -297,11 +303,11 @@ esp_err_t httpd_sess_process(struct httpd_data *hd, int newfd)
|
||||||
return ESP_FAIL;
|
return ESP_FAIL;
|
||||||
}
|
}
|
||||||
ESP_LOGD(TAG, LOG_FMT("success"));
|
ESP_LOGD(TAG, LOG_FMT("success"));
|
||||||
sd->timestamp = httpd_os_get_timestamp();
|
sd->lru_counter = httpd_sess_get_lru_counter();
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t httpd_sess_update_timestamp(httpd_handle_t handle, int sockfd)
|
esp_err_t httpd_sess_update_lru_counter(httpd_handle_t handle, int sockfd)
|
||||||
{
|
{
|
||||||
if (handle == NULL) {
|
if (handle == NULL) {
|
||||||
return ESP_ERR_INVALID_ARG;
|
return ESP_ERR_INVALID_ARG;
|
||||||
|
@ -312,7 +318,7 @@ esp_err_t httpd_sess_update_timestamp(httpd_handle_t handle, int sockfd)
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < hd->config.max_open_sockets; i++) {
|
for (i = 0; i < hd->config.max_open_sockets; i++) {
|
||||||
if (hd->hd_sd[i].fd == sockfd) {
|
if (hd->hd_sd[i].fd == sockfd) {
|
||||||
hd->hd_sd[i].timestamp = httpd_os_get_timestamp();
|
hd->hd_sd[i].lru_counter = httpd_sess_get_lru_counter();
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -321,7 +327,7 @@ esp_err_t httpd_sess_update_timestamp(httpd_handle_t handle, int sockfd)
|
||||||
|
|
||||||
esp_err_t httpd_sess_close_lru(struct httpd_data *hd)
|
esp_err_t httpd_sess_close_lru(struct httpd_data *hd)
|
||||||
{
|
{
|
||||||
int64_t timestamp = INT64_MAX;
|
uint64_t lru_counter = UINT64_MAX;
|
||||||
int lru_fd = -1;
|
int lru_fd = -1;
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < hd->config.max_open_sockets; i++) {
|
for (i = 0; i < hd->config.max_open_sockets; i++) {
|
||||||
|
@ -332,8 +338,8 @@ esp_err_t httpd_sess_close_lru(struct httpd_data *hd)
|
||||||
if (hd->hd_sd[i].fd == -1) {
|
if (hd->hd_sd[i].fd == -1) {
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
if (hd->hd_sd[i].timestamp < timestamp) {
|
if (hd->hd_sd[i].lru_counter < lru_counter) {
|
||||||
timestamp = hd->hd_sd[i].timestamp;
|
lru_counter = hd->hd_sd[i].lru_counter;
|
||||||
lru_fd = hd->hd_sd[i].fd;
|
lru_fd = hd->hd_sd[i].fd;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,11 +52,6 @@ static inline void httpd_os_thread_sleep(int msecs)
|
||||||
vTaskDelay(msecs / portTICK_RATE_MS);
|
vTaskDelay(msecs / portTICK_RATE_MS);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int64_t httpd_os_get_timestamp()
|
|
||||||
{
|
|
||||||
return esp_timer_get_time();
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline othread_t httpd_os_thread_handle()
|
static inline othread_t httpd_os_thread_handle()
|
||||||
{
|
{
|
||||||
return xTaskGetCurrentTaskHandle();
|
return xTaskGetCurrentTaskHandle();
|
||||||
|
|
Ładowanie…
Reference in New Issue