Skip to content

Commit 87e74b3

Browse files
authored
Merge pull request #167 from tobiasschuerg/fix/disable_retry
feat: disabling retrying, added writeRecord(const char *record)
2 parents 51dbfff + e070d9d commit 87e74b3

File tree

5 files changed

+67
-5
lines changed

5 files changed

+67
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Changelog
22
## unreleased
33
### Features
4-
### Fixes
4+
- [167](https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino/pull/167) - Added `InfluxDBClient::writeRecord(const char *record)`.
5+
- [167](https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino/pull/167) - Added possibility to disable retrying by setting `maxRetryAttempts` to zero: `client.setWriteOptions(WriteOptions().maxRetryAttempts(0));`
56

67
## 3.9.0 [2021-09-17]
78
### Features

src/InfluxDbClient.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ bool InfluxDBClient::writePoint(Point & point) {
294294
return false;
295295
}
296296

297-
bool InfluxDBClient::Batch::append(String &line) {
297+
bool InfluxDBClient::Batch::append(const char *line) {
298298
if(pointer == _size) {
299299
//overwriting, clean buffer
300300
for(int i=0;i< _size; i++) {
@@ -330,6 +330,10 @@ char * InfluxDBClient::Batch::createData() {
330330
}
331331

332332
bool InfluxDBClient::writeRecord(String &record) {
333+
return writeRecord(record.c_str());
334+
}
335+
336+
bool InfluxDBClient::writeRecord(const char *record) {
333337
if(!_writeBuffer[_bufferPointer]) {
334338
_writeBuffer[_bufferPointer] = new Batch(_writeOptions._batchSize);
335339
}
@@ -411,7 +415,7 @@ bool InfluxDBClient::flushBufferInternal(bool flashOnlyFull) {
411415
int statusCode = postData(data);
412416
delete [] data;
413417
// retry on unsuccessfull connection or retryable status codes
414-
bool retry = statusCode < 0 || statusCode >= 429;
418+
bool retry = (statusCode < 0 || statusCode >= 429) && _writeOptions._maxRetryAttempts > 0;
415419
success = statusCode >= 200 && statusCode < 300;
416420
// advance even on message failure x e <300;429)
417421
if(success || !retry) {

src/InfluxDbClient.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ class InfluxDBClient {
123123
// Writes record in InfluxDB line protocol format to write buffer.
124124
// Returns true if successful, false in case of any error
125125
bool writeRecord(String &record);
126+
bool writeRecord(const char *record);
126127
// Writes record represented by Point to buffer
127128
// Returns true if successful, false in case of any error
128129
bool writePoint(Point& point);
@@ -172,7 +173,7 @@ class InfluxDBClient {
172173
uint8_t retryCount = 0;
173174
Batch(int size):_size(size) { buffer = new String[size]; }
174175
~Batch() { delete [] buffer; }
175-
bool append(String &line);
176+
bool append(const char *line);
176177
char *createData();
177178
bool isFull() const {
178179
return pointer == _size;

test/Test.cpp

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ void Test::run() {
6868
testTimestamp();
6969
testRetryOnFailedConnection();
7070
testRetryOnFailedConnectionWithFlush();
71+
testNonRetry();
7172
testBufferOverwriteBatchsize1();
7273
testBufferOverwriteBatchsize5();
7374
testServerTempDownBatchsize5();
@@ -566,7 +567,7 @@ void Test::testRepeatedInit() {
566567
} while(0);
567568
uint32_t endRAM = ESP.getFreeHeap();
568569
long diff = endRAM-startRAM;
569-
TEST_ASSERTM(diff>-100,String(diff));
570+
TEST_ASSERTM(diff>-300,String(diff));
570571
TEST_END();
571572
}
572573

@@ -2121,6 +2122,60 @@ void Test::testFlushing() {
21212122
deleteAll(Test::apiUrl);
21222123
}
21232124

2125+
#define WS_DEBUG_RAM(text) { Serial.printf_P(PSTR(text ": free_heap %d, max_alloc_heap %d, heap_fragmentation %d\n"), ESP.getFreeHeap(), ESP.getMaxFreeBlockSize(), ESP.getHeapFragmentation()); }
2126+
2127+
2128+
void Test::testNonRetry() {
2129+
TEST_INIT("testNonRetry");
2130+
const char *lines[] = {
2131+
"device_status,clientId=WS-E09806011111,Device=WS-ESP8266,Version=0.58-rc3,Location=Prague\\,CZ,WiFi=Bonitoo-ng free_heap=16568i,max_alloc_heap=11336i,heap_fragmentation=29i,uptime=28821.23,wifi_disconnects=0i",
2132+
"service_status,clientId=WS-E09806011111,Device=WS-ESP8266,Version=0.58-rc3,Location=Prague\\,CZ,WiFi=Bonitoo-ng,service=location state=3i,before_mem_free=36232i,before_mem_max_free_block=17544i,before_mem_framentation=49i,after_mem_free=35792i,after_mem_max_free_block=17544i,after_mem_framentation=48i",
2133+
"service_status,clientId=WS-E09806011111,Device=WS-ESP8266,Version=0.58-rc3,Location=Prague\\,CZ,WiFi=Bonitoo-ng,service=clock state=2i,before_mem_free=16704i,before_mem_max_free_block=11336i,before_mem_framentation=30i,after_mem_free=16704i,after_mem_max_free_block=11336i,after_mem_framentation=30i",
2134+
"service_status,clientId=WS-E09806011111,Device=WS-ESP8266,Version=0.58-rc3,Location=Prague\\,CZ,WiFi=Bonitoo-ng,service=update state=0i",
2135+
"service_status,clientId=WS-E09806011111,Device=WS-ESP8266,Version=0.58-rc3,Location=Prague\\,CZ,WiFi=Bonitoo-ng,service=astronomy state=2i,before_mem_free=16376i,before_mem_max_free_block=11336i,before_mem_framentation=28i,after_mem_free=16376i,after_mem_max_free_block=11336i,after_mem_framentation=28i",
2136+
"service_status,clientId=WS-E09806011111,Device=WS-ESP8266,Version=0.58-rc3,Location=Prague\\,CZ,WiFi=Bonitoo-ng,service=current_weather state=2i,before_mem_free=16728i,before_mem_max_free_block=11336i,before_mem_framentation=30i,after_mem_free=16376i,after_mem_max_free_block=11336i,after_mem_framentation=28i",
2137+
"service_status,clientId=WS-E09806011111,Device=WS-ESP8266,Version=0.58-rc3,Location=Prague\\,CZ,WiFi=Bonitoo-ng,service=forecast state=2i,before_mem_free=16704i,before_mem_max_free_block=11336i,before_mem_framentation=30i,after_mem_free=16376i,after_mem_max_free_block=11336i,after_mem_framentation=28i",
2138+
"service_status,clientId=WS-E09806011111,Device=WS-ESP8266,Version=0.58-rc3,Location=Prague\\,CZ,WiFi=Bonitoo-ng,service=iot_center state=0i",
2139+
};
2140+
WriteOptions wo;
2141+
WS_DEBUG_RAM("Before inst");
2142+
InfluxDBClient *client = new InfluxDBClient(Test::apiUrl, Test::orgName, Test::bucketName, Test::token);
2143+
WS_DEBUG_RAM("after inst");
2144+
2145+
//TEST not keeping batch for retry
2146+
Serial.println("Stop server");
2147+
TEST_ASSERT(waitServer(Test::managementUrl, false));
2148+
client->setHTTPOptions(HTTPOptions().httpReadTimeout(500));
2149+
TEST_ASSERT(!client->validateConnection());
2150+
// Disable retry
2151+
wo.maxRetryAttempts(0);
2152+
client->setWriteOptions(wo);
2153+
client->setHTTPOptions(HTTPOptions().connectionReuse(true));
2154+
TEST_ASSERT(!client->writeRecord(lines[0]));
2155+
TEST_ASSERT(!client->_writeBuffer[0]);
2156+
2157+
TEST_ASSERT(waitServer(Test::managementUrl, true));
2158+
TEST_ASSERT(client->validateConnection());
2159+
2160+
uint8_t size = sizeof(lines)/sizeof(lines[0]);
2161+
uint16_t batchSize = size +1;
2162+
wo.batchSize(batchSize).bufferSize(batchSize);
2163+
client->setWriteOptions(wo);
2164+
2165+
WS_DEBUG_RAM("Before");
2166+
for(int i=0;i<size;i++) {
2167+
TEST_ASSERTM(client->writeRecord(lines[i]), client->getLastErrorMessage());
2168+
WS_DEBUG_RAM("After write Line");
2169+
}
2170+
TEST_ASSERTM(client->flushBuffer(), client->getLastErrorMessage());
2171+
WS_DEBUG_RAM("After flush");
2172+
delete client;
2173+
WS_DEBUG_RAM("After delete");
2174+
TEST_END();
2175+
deleteAll(Test::apiUrl);
2176+
}
2177+
2178+
21242179
void Test::setServerUrl(InfluxDBClient &client, String serverUrl) {
21252180
client._connInfo.serverUrl = serverUrl;
21262181
client._service->_apiURL = serverUrl + "/api/v2/";

test/Test.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class Test : public TestBase {
7171
static void testIsValidID();
7272
static void testBuckets();
7373
static void testFlushing();
74+
static void testNonRetry();
7475
};
7576

7677
#endif //_TEST_H_

0 commit comments

Comments
 (0)