Skip to content

Commit 93fb7f0

Browse files
committed
RedisQueue : error handling
1 parent 9cdefc1 commit 93fb7f0

File tree

1 file changed

+37
-19
lines changed

1 file changed

+37
-19
lines changed

src/AudioTools/Communication/RedisBuffer.h

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ class RedisBuffer : public BaseBuffer<T> {
281281
*/
282282
RedisResult readResponse() {
283283
RedisResult result;
284+
result.ok = true;
284285
uint8_t buffer[1024] = {};
285286
int n = 0;
286287
while (n <= 0) {
@@ -299,28 +300,41 @@ class RedisBuffer : public BaseBuffer<T> {
299300
tail.trim();
300301
nl_pos = tail.indexOf("\r\n");
301302
}
302-
303+
303304
if (tail.length() > 0) {
304305
result.strValues.push_back(tail);
305-
}
306+
}
306307

307-
// Get int value
308-
StrView line((char*)buffer, sizeof(buffer), n);
308+
// if we have more then 2 lines this is a success
309+
if (result.strValues.size() > 2) {
310+
result.intValue = result.strValues.size();
311+
} else {
312+
// Else try to determine an int value
313+
StrView line((char*)buffer, sizeof(buffer), n);
309314

310-
if (line.startsWith("$")) {
311-
int end = line.indexOf("\n");
312-
line.substring(line.c_str(), end, line.length());
313-
}
315+
/// this is a length prefix line
316+
if (line.startsWith("$")) {
317+
int end = line.indexOf("\n");
318+
line.substring(line.c_str(), end, line.length());
319+
}
314320

315-
/// Remove any leading or trailing whitespace
316-
if (line.startsWith(":")) {
317-
line.replace(":", "");
318-
}
321+
/// Remove any leading or trailing whitespace
322+
if (line.startsWith(":")) {
323+
line.replace(":", "");
324+
}
319325

320-
if (line.isEmpty())
321-
line = -1;
322-
else
323-
result.intValue = line.toInt();
326+
if (line.startsWith("-")){
327+
result.ok = false;
328+
}
329+
330+
if (line.isEmpty()){
331+
result.intValue = -1; // no data available
332+
result.ok = false;
333+
}
334+
else {
335+
result.intValue = line.toInt();
336+
}
337+
}
324338

325339
return result;
326340
}
@@ -363,14 +377,18 @@ class RedisBuffer : public BaseBuffer<T> {
363377
// Read up to local_buf_size items from Redis
364378
String cmd = redisCommand("LPOP", key, String(read_buf.size()));
365379
auto rc = sendCommand(cmd);
366-
for (auto &str : rc.strValues) {
380+
if (!rc.ok) {
381+
LOGE("Redis LPOP failed: %s", cmd.c_str());
382+
return; // no data available
383+
}
384+
for (auto& str : rc.strValues) {
367385
if (str.startsWith("*")) continue;
368386
if (str.startsWith("$")) continue;
369-
if (str.length()==0) continue;
387+
if (str.length() == 0) continue;
370388
LOGI("Redis LPOP: %s", str.c_str());
371389
T value = (T)str.toInt();
372390
read_buf.write(value);
373-
}
391+
}
374392
}
375393
};
376394

0 commit comments

Comments
 (0)