pistache icon indicating copy to clipboard operation
pistache copied to clipboard

Does timeoutAfter in Http::ResponseWriter really work?

Open hyperxor opened this issue 4 years ago • 1 comments

Hello,

I want to ask question about timeoutAfter method in Http::ResponseWriter. Based on documentation:

That is why Pistache provides the ability to control the timeout on a per-request basis. To arm a timeout on a response, you can use the timeoufterAfter() member function directly on the ResponseWriter object:

response.timeoutAfter(std::chrono::milliseconds(500));
This will trigger a timeout if a response has not been sent within 500 milliseconds. timeoutAfter accepts any kind of duration.

I can expect calling onTimeout method in my handler, for instance, I insert this_thread::sleep_for(chrono::seconds(5)); in ResponseWriter::putOnWire (just for delay simulation) before timeout_.disarm();. I have tried it and it doesn't work. After investigation and debug I understood that the process of handling timer file descriptor is happened in the same thread as incoming request:

void Transport::onReady(const Aio::FdSet &fds) {
...
    else if (entry.isReadable()) {
      auto tag = entry.getTag();
      if (isPeerFd(tag)) {
        auto &peer = getPeer(tag);
        handleIncoming(peer);
      } else if (isTimerFd(tag)) {
        auto it = timers.find(static_cast<decltype(timers)::key_type>(tag.value()));
        auto &entry_ = it->second;
        handleTimer(std::move(entry_));
        timers.erase(it->first);
      } else {
        throw std::runtime_error("Unknown fd");
      }
...
}

As a result timer event can be handled only after finishing onRequest method. Am I right? Did this work before? Or maybe I do something wrong.

hyperxor avatar Nov 29 '20 07:11 hyperxor

Related to https://github.com/pistacheio/pistache/issues/833

polmr avatar Dec 22 '20 07:12 polmr