summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Adam Chlipala <adam@chlipala.net>2010-10-14 11:35:56 -0400
committerGravatar Adam Chlipala <adam@chlipala.net>2010-10-14 11:35:56 -0400
commitcfffcb997d0345f90444725248c7c85b035c30b4 (patch)
tree3e839cc2566526f3e4a294229c41f88e60e8d24e /src
parent7bf0a0124a6c8a834983a660af53d8789ac0a8ac (diff)
-limit for running time
Diffstat (limited to 'src')
-rw-r--r--src/c/request.c25
-rw-r--r--src/c/urweb.c2
-rw-r--r--src/settings.sml8
3 files changed, 30 insertions, 5 deletions
diff --git a/src/c/request.c b/src/c/request.c
index f72a3199..190a327f 100644
--- a/src/c/request.c
+++ b/src/c/request.c
@@ -70,6 +70,15 @@ uw_context uw_request_new_context(uw_app *app, void *logger_data, uw_logger log_
return ctx;
}
+static void *ticker(void *data) {
+ while (1) {
+ usleep(100000);
+ ++uw_time;
+ }
+
+ return NULL;
+}
+
void uw_request_init(uw_app *app, void *logger_data, uw_logger log_error, uw_logger log_debug) {
uw_context ctx;
failure_kind fk;
@@ -77,6 +86,15 @@ void uw_request_init(uw_app *app, void *logger_data, uw_logger log_error, uw_log
uw_global_init();
uw_app_init(app);
+ {
+ pthread_t thread;
+
+ if (uw_time_max && pthread_create(&thread, NULL, ticker, NULL)) {
+ fprintf(stderr, "Error creating ticker thread\n");
+ exit(1);
+ }
+ }
+
ctx = uw_request_new_context(app, logger_data, log_error, log_debug);
if (!ctx)
@@ -348,15 +366,18 @@ request_result uw_request(uw_request_context rc, uw_context ctx,
rc->path_copy = realloc(rc->path_copy, rc->path_copy_size);
}
strcpy(rc->path_copy, path);
+
+ uw_set_deadline(ctx, uw_time + uw_time_max);
fk = uw_begin(ctx, rc->path_copy);
- } else
+ } else {
+ uw_set_deadline(ctx, uw_time + uw_time_max);
fk = uw_begin_onError(ctx, errmsg);
+ }
if (fk == SUCCESS || fk == RETURN_INDIRECTLY) {
uw_commit(ctx);
if (uw_has_error(ctx) && !had_error) {
log_error(logger_data, "Fatal error: %s\n", uw_error_message(ctx));
-
uw_reset_keep_error_message(ctx);
on_failure(ctx);
diff --git a/src/c/urweb.c b/src/c/urweb.c
index 747f62b6..bdaa877f 100644
--- a/src/c/urweb.c
+++ b/src/c/urweb.c
@@ -348,7 +348,7 @@ void uw_app_init(uw_app *app) {
app->client_init();
}
-int uw_time = 0;
+int uw_time = 0, uw_time_max = 0;
// Single-request state
diff --git a/src/settings.sml b/src/settings.sml
index 4512b7d8..b5ba4f9b 100644
--- a/src/settings.sml
+++ b/src/settings.sml
@@ -492,12 +492,16 @@ fun getOnError () = !onError
val limits = ["messages", "clients", "headers", "page", "heap", "script",
"inputs", "subinputs", "cleanup", "deltas", "transactionals",
- "globals", "database"]
+ "globals", "database", "time"]
val limitsList = ref ([] : (string * int) list)
fun addLimit (v as (name, _)) =
if List.exists (fn name' => name' = name) limits then
- limitsList := v :: !limitsList
+ (limitsList := v :: !limitsList;
+ if name = "time" then
+ setDeadlines true
+ else
+ ())
else
raise Fail ("Unknown limit category '" ^ name ^ "'")
fun limits () = !limitsList