@@ -281,6 +281,7 @@ class RedisBuffer : public BaseBuffer<T> {
281
281
*/
282
282
RedisResult readResponse () {
283
283
RedisResult result;
284
+ result.ok = true ;
284
285
uint8_t buffer[1024 ] = {};
285
286
int n = 0 ;
286
287
while (n <= 0 ) {
@@ -299,28 +300,41 @@ class RedisBuffer : public BaseBuffer<T> {
299
300
tail.trim ();
300
301
nl_pos = tail.indexOf (" \r\n " );
301
302
}
302
-
303
+
303
304
if (tail.length () > 0 ) {
304
305
result.strValues .push_back (tail);
305
- }
306
+ }
306
307
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);
309
314
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
+ }
314
320
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
+ }
319
325
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
+ }
324
338
325
339
return result;
326
340
}
@@ -363,14 +377,18 @@ class RedisBuffer : public BaseBuffer<T> {
363
377
// Read up to local_buf_size items from Redis
364
378
String cmd = redisCommand (" LPOP" , key, String (read_buf.size ()));
365
379
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 ) {
367
385
if (str.startsWith (" *" )) continue ;
368
386
if (str.startsWith (" $" )) continue ;
369
- if (str.length ()== 0 ) continue ;
387
+ if (str.length () == 0 ) continue ;
370
388
LOGI (" Redis LPOP: %s" , str.c_str ());
371
389
T value = (T)str.toInt ();
372
390
read_buf.write (value);
373
- }
391
+ }
374
392
}
375
393
};
376
394
0 commit comments