Skip to content

Commit f5dd61f

Browse files
committed
simplify retry logic
1 parent 744fe5d commit f5dd61f

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

table_postgres.c

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -311,14 +311,9 @@ table_postgres_query(const char *key, int service)
311311
PGresult *res;
312312
const char *errfld;
313313
char *stmt;
314-
int i, retry_times = 2;
314+
int i, retries = 1;
315315

316316
retry:
317-
retry_times--;
318-
if (retry_times < 0) {
319-
log_warnx("warn: table-postgres: to many retries");
320-
return NULL;
321-
}
322317
stmt = NULL;
323318
for (i = 0; i < SQL_MAX; i++) {
324319
if (service == 1 << i) {
@@ -339,8 +334,10 @@ table_postgres_query(const char *key, int service)
339334
log_warnx("warn: table-postgres: trying to reconnect after error: %s",
340335
PQerrorMessage(config->db));
341336
PQclear(res);
342-
if (config_connect(config))
337+
if (config_connect(config) && --retries > 0)
343338
goto retry;
339+
if (retries <= 0)
340+
log_warnx("warn: table-postgres: too many retries");
344341
return NULL;
345342
}
346343
log_warnx("warn: PQexecPrepared: %s", PQerrorMessage(config->db));
@@ -449,17 +446,12 @@ table_postgres_fetch(int service, struct dict *params, char *dst, size_t sz)
449446
char *stmt;
450447
PGresult *res;
451448
const char *k, *errfld;
452-
int i, retry_times = 1;
449+
int i, retries = 1;
453450

454451
if (config->db == NULL && config_connect(config) == 0)
455452
return -1;
456453

457454
retry:
458-
retry_times--;
459-
if (retry_times < 0) {
460-
log_warnx("warn: table-postgres: to many retries");
461-
return -1;
462-
}
463455
if (service != K_SOURCE)
464456
return -1;
465457

@@ -481,8 +473,10 @@ table_postgres_fetch(int service, struct dict *params, char *dst, size_t sz)
481473
if (errfld == NULL || (errfld[0] == '0' && errfld[1] == '8')) {
482474
log_warnx("warn: trying to reconnect after error: %s", PQerrorMessage(config->db));
483475
PQclear(res);
484-
if (config_connect(config))
476+
if (config_connect(config) && --retries > 0)
485477
goto retry;
478+
if (retries <= 0)
479+
log_warnx("warn: table-postgres: too many retries");
486480
return -1;
487481
}
488482
log_warnx("warn: PQexecPrepared: %s", PQerrorMessage(config->db));

0 commit comments

Comments
 (0)