Skip to content

Commit

Permalink
Add support TCP session timeout for session reusage
Browse files Browse the repository at this point in the history
  • Loading branch information
mobizt committed Jun 25, 2024
1 parent 89f11b1 commit 46b811c
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 8 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/mobizt/FirebaseClient/.github%2Fworkflows%2Fcompile_library.yml?logo=github&label=compile) [![Github Stars](https://img.shields.io/github/stars/mobizt/FirebaseClient?logo=github)](https://github.com/mobizt/FirebaseClient/stargazers) ![Github Issues](https://img.shields.io/github/issues/mobizt/FirebaseClient?logo=github)

![GitHub Release](https://img.shields.io/github/v/release/mobizt/FirebaseClient) ![Arduino](https://img.shields.io/badge/Arduino-v1.2.15-57C207?logo=arduino) ![PlatformIO](https://badges.registry.platformio.org/packages/mobizt/library/FirebaseClient.svg) ![GitHub Release Date](https://img.shields.io/github/release-date/mobizt/FirebaseClient)
![GitHub Release](https://img.shields.io/github/v/release/mobizt/FirebaseClient) ![Arduino](https://img.shields.io/badge/Arduino-v1.2.16-57C207?logo=arduino) ![PlatformIO](https://badges.registry.platformio.org/packages/mobizt/library/FirebaseClient.svg) ![GitHub Release Date](https://img.shields.io/github/release-date/mobizt/FirebaseClient)

[![GitHub Sponsors](https://img.shields.io/github/sponsors/mobizt?logo=github)](https://github.com/sponsors/mobizt)

Revision `2024-06-25T01:54:21Z`
Revision `2024-06-25T05:25:05Z`

## Table of Contents

Expand Down Expand Up @@ -507,7 +507,7 @@ In case using ESP8266 without `PSRAM` and you want to reduce the memory usage, y
Note that, because the receive buffer size was set to minimum safe value, 1024, the large server response may not be able to handle.

> [!WARNING]
> In ESP32, `WiFiClient` and `WiFiClientSecure` classes are unable to detect the server disconnection in case server session time out and the TCP session was kept alive for reuse in most tasks this library. The server session timed out will not happen if data was sent or received within the server time out period. User have to close the session manually by calling `WiFiClient::stop()` or `WiFiClientSecure::stop()` when no data was sent/received within 2-3 minutes before starting the new HTTP request.
> In ESP32, `WiFiClient` and `WiFiClientSecure` classes are unable to detect the server disconnection in case server session time out and the TCP session was kept alive for reuse in most tasks this library. The server session timed out will not happen if data was sent or received within the server time out period. The TCP session timeout in seconds in this library can be set via macro or build flag `FIREBASE_SESSION_TIMEOUT`.

- ### Async Client
Expand Down Expand Up @@ -2963,6 +2963,7 @@ ENABLE_ASYNC_TCP_CLIENT // For Async TCP Client usage.
FIREBASE_ASYNC_QUEUE_LIMIT // For maximum async queue limit setting for an async client.
FIREBASE_PRINTF_PORT // For Firebase.printf debug port.
FIREBASE_PRINTF_BUFFER // Firebase.printf buffer size.
FIREBASE_SESSION_TIMEOUT // For TCP session timeout in seconds. The default TCP session timeout is 180 seconds (3 minutes)
```

You can assign the optional build options using one of the following methods.
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "FirebaseClient",
"version": "1.2.15",
"version": "1.2.16",
"keywords": "communication, REST, esp32, esp8266, arduino",
"description": "Async Firebase Client library for Arduino.",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name=FirebaseClient

version=1.2.15
version=1.2.16

author=Mobizt

Expand Down
4 changes: 4 additions & 0 deletions src/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@
*
* * 🏷️ For Firebase.printf buffer size.
* #define FIREBASE_PRINTF_BUFFER 2048
*
* * 🏷️ For TCP session timeout in seconds.
* The default TCP session timeout is 180 seconds (3 minutes)
* #define FIREBASE_SESSION_TIMEOUT
*/

#if __has_include("UserConfig.h")
Expand Down
15 changes: 13 additions & 2 deletions src/core/AsyncClient/AsyncClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase
AsyncResult *refResult = nullptr;
AsyncResult aResult;
int netErrState = 0;
uint32_t auth_ts = 0;
uint32_t auth_ts = 0, conn_ts = 0;
uint32_t cvec_addr = 0;
uint32_t result_addr = 0;
uint32_t sync_send_timeout_sec = 0, sync_read_timeout_sec = 0;
Expand Down Expand Up @@ -234,7 +234,13 @@ class AsyncClientClass : public ResultBase, RTDBResultBase

void newCon(async_data_item_t *sData, const char *host, uint16_t port)
{
if ((sse && !sData->sse) || (!sse && sData->sse) || (sData->auth_used && sData->state == async_state_undefined) ||
#if defined(FIREBASE_SESSION_TIMEOUT)
int session_timeout_msec = FIREBASE_SESSION_TIMEOUT * 60 * 1000;
#else
int session_timeout_msec = 3 * 60 * 1000;
#endif

if ((!sData->sse && conn_ts > 0 && millis() - conn_ts > session_timeout_msec) || (sse && !sData->sse) || (!sse && sData->sse) || (sData->auth_used && sData->state == async_state_undefined) ||
strcmp(this->host.c_str(), host) != 0 || this->port != port)
{
stop(sData);
Expand Down Expand Up @@ -1425,6 +1431,10 @@ class AsyncClientClass : public ResultBase, RTDBResultBase

this->host = host;
this->port = port;

if (client && !client->connected())
conn_ts = millis();

return sData->return_type;
}

Expand Down Expand Up @@ -2001,6 +2011,7 @@ class AsyncClientClass : public ResultBase, RTDBResultBase

clear(host);
port = 0;
conn_ts = 0;
}

async_data_item_t *createSlot(slot_options_t &options)
Expand Down
2 changes: 1 addition & 1 deletion src/core/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#undef FIREBASE_CLIENT_VERSION
#endif

#define FIREBASE_CLIENT_VERSION "1.2.15"
#define FIREBASE_CLIENT_VERSION "1.2.16"

static void sys_idle()
{
Expand Down

0 comments on commit 46b811c

Please sign in to comment.