comparison src/c/request.c @ 1327:1cc67fdac4d3

New argument to transactional free functions, to specify whether we are about to retry
author Adam Chlipala <adam@chlipala.net>
date Sat, 04 Dec 2010 11:15:20 -0500
parents add5ae41969e
children a1aa62b472cf
comparison
equal deleted inserted replaced
1326:d91f84599693 1327:1cc67fdac4d3
17 17
18 #define MAX_RETRIES 5 18 #define MAX_RETRIES 5
19 19
20 void *memmem(const void *b1, size_t len1, const void *b2, size_t len2); 20 void *memmem(const void *b1, size_t len1, const void *b2, size_t len2);
21 21
22 static int try_rollback(uw_context ctx, void *logger_data, uw_logger log_error) { 22 static int try_rollback(uw_context ctx, int will_retry, void *logger_data, uw_logger log_error) {
23 int r = uw_rollback(ctx); 23 int r = uw_rollback(ctx, will_retry);
24 24
25 if (r) { 25 if (r) {
26 log_error(logger_data, "Error running SQL ROLLBACK\n"); 26 log_error(logger_data, "Error running SQL ROLLBACK\n");
27 uw_reset(ctx); 27 uw_reset(ctx);
28 uw_write(ctx, "HTTP/1.1 500 Internal Server Error\r\n"); 28 uw_write(ctx, "HTTP/1.1 500 Internal Server Error\r\n");
100 if (!ctx) 100 if (!ctx)
101 exit(1); 101 exit(1);
102 102
103 for (fk = uw_initialize(ctx); fk == UNLIMITED_RETRY; fk = uw_initialize(ctx)) { 103 for (fk = uw_initialize(ctx); fk == UNLIMITED_RETRY; fk = uw_initialize(ctx)) {
104 log_debug(logger_data, "Unlimited retry during init: %s\n", uw_error_message(ctx)); 104 log_debug(logger_data, "Unlimited retry during init: %s\n", uw_error_message(ctx));
105 uw_rollback(ctx); 105 uw_rollback(ctx, 1);
106 uw_reset(ctx); 106 uw_reset(ctx);
107 } 107 }
108 108
109 if (fk != SUCCESS) { 109 if (fk != SUCCESS) {
110 log_error(logger_data, "Failed to initialize database! %s\n", uw_error_message(ctx)); 110 log_error(logger_data, "Failed to initialize database! %s\n", uw_error_message(ctx));
111 uw_rollback(ctx); 111 uw_rollback(ctx, 0);
112 exit(1); 112 exit(1);
113 } 113 }
114 114
115 uw_free(ctx); 115 uw_free(ctx);
116 } 116 }
401 --retries_left; 401 --retries_left;
402 } 402 }
403 else { 403 else {
404 log_error(logger_data, "Fatal error (out of retries): %s\n", uw_error_message(ctx)); 404 log_error(logger_data, "Fatal error (out of retries): %s\n", uw_error_message(ctx));
405 405
406 try_rollback(ctx, logger_data, log_error); 406 try_rollback(ctx, 0, logger_data, log_error);
407 407
408 if (!had_error && uw_get_app(ctx)->on_error) { 408 if (!had_error && uw_get_app(ctx)->on_error) {
409 had_error = 1; 409 had_error = 1;
410 strcpy(errmsg, uw_error_message(ctx)); 410 strcpy(errmsg, uw_error_message(ctx));
411 } else { 411 } else {
422 } else if (fk == UNLIMITED_RETRY) 422 } else if (fk == UNLIMITED_RETRY)
423 log_debug(logger_data, "Error triggers unlimited retry: %s\n", uw_error_message(ctx)); 423 log_debug(logger_data, "Error triggers unlimited retry: %s\n", uw_error_message(ctx));
424 else if (fk == FATAL) { 424 else if (fk == FATAL) {
425 log_error(logger_data, "Fatal error: %s\n", uw_error_message(ctx)); 425 log_error(logger_data, "Fatal error: %s\n", uw_error_message(ctx));
426 426
427 try_rollback(ctx, logger_data, log_error); 427 try_rollback(ctx, 0, logger_data, log_error);
428 428
429 if (uw_get_app(ctx)->on_error && !had_error) { 429 if (uw_get_app(ctx)->on_error && !had_error) {
430 had_error = 1; 430 had_error = 1;
431 strcpy(errmsg, uw_error_message(ctx)); 431 strcpy(errmsg, uw_error_message(ctx));
432 } else { 432 } else {
441 return FAILED; 441 return FAILED;
442 } 442 }
443 } else { 443 } else {
444 log_error(logger_data, "Unknown uw_handle return code!\n"); 444 log_error(logger_data, "Unknown uw_handle return code!\n");
445 445
446 try_rollback(ctx, logger_data, log_error); 446 try_rollback(ctx, 0, logger_data, log_error);
447 447
448 if (uw_get_app(ctx)->on_error && !had_error) { 448 if (uw_get_app(ctx)->on_error && !had_error) {
449 had_error = 1; 449 had_error = 1;
450 strcpy(errmsg, "Unknown uw_handle return code"); 450 strcpy(errmsg, "Unknown uw_handle return code");
451 } else { 451 } else {
456 456
457 return FAILED; 457 return FAILED;
458 } 458 }
459 } 459 }
460 460
461 if (try_rollback(ctx, logger_data, log_error)) 461 if (try_rollback(ctx, 1, logger_data, log_error))
462 return FAILED; 462 return FAILED;
463 463
464 uw_reset_keep_request(ctx); 464 uw_reset_keep_request(ctx);
465 } 465 }
466 } 466 }