Closed whatvn closed 9 years ago
I'm not sure why you're using cache for this in the first place. What you're trying to do is closer to what needs to be done in #36; with that implemented, you would need only something like this in your handler:
lwan_http_status_t delay_handler(...) {
long time = parse_long(lwan_request_get_query_param(request, "delay"), 0);
if (time <= 0 || time > 5000) return HTTP_BAD_REQUEST;
lwan_delay(request, time);
strbuf_set_static(response->buffer, "ok", 2);
return HTTP_OK;
}
The mentioned ticket talks about timerfd
which would be the most efficient way to implement this. But, for testing purposes, you could just implement lwan_delay()
as (or something similar, I haven't tested this):
void lwan_delay(request, how_much) {
long stop_when = get_current_time_somehow() + how_much;
while (get_current_time_somehow() < stop_when)
coro_yield(request->conn->coro, CORO_MAY_RESUME);
}
This isn't as efficient (will yield a lot until it's time to complete), but will be fair as in it'll let other requests to be handled in the time it's not executing.
I follow your guide and it works as expect.
Thanks
Hello,
I write a simple handler with lwan, this handler do a simple thing: when client make a request which ask server to delay x miliseconds then response, server sleeps x miliseconds then response ok to client. The reason I do this is we are trying to demonstrate real time bidding issue, when server have to response within 120ms for a bidding request, and client must close request after 120ms even if it does not receive response.
Write a handler which does not use coroutine is easy, but it causes blocking when many concurrent delay client make requests to lwan, it just can reply only some first requests, then cannot response at all.
Using coroutine is better, but I dont know how to use it without passing it through cache. Source code below:
As you can see, I try to make uniq cache key by current request time in order to disable caching, but lwan still cache request. If I make 2 request to server: http://localhost/d?delay=1000, 1 request will receive response in 1012 ms, the second one receive response in 10 ms (did not sleep)
So, the issue is: how to disable the cache in this case? I also try to remove assert(cache->time_to_live > 0) in lwan_cache.c to create cache with time_to_live = 0, but request is still cached.
Thanks