RejectedSoftware Forums

Sign up

REST API long polling

How to determine (on the server side) that the remote client disconnects during a very long polling REST request?

something like this:

Data waitForUpdatedData(...) {
  Nullable!bool ready;
  Data result;
  
  auto disconnectionTask = runTask({
    waitForClientDisconnection(); /// ????
    ready = false;
    endCondition.notify();
  });

  auto workerTask = runTask({
    result = waitingForUpdates();
    ready = true;
    endCondition.notify();
  });

  while(ready.isNull) endCondition.wait();

  if(ready) disconnectionTask.terminate();
  else workerTask.terminate();

  return result;
}

Re: REST API long polling

On Mon, 20 Oct 2014 09:16:06 GMT, Jack Applegame wrote:

How to determine (on the server side) that the remote client disconnects during a very long polling REST request?

something like this:

Data waitForUpdatedData(...) {
  Nullable!bool ready;
  Data result;
  
  auto disconnectionTask = runTask({
    waitForClientDisconnection(); /// ????
    ready = false;
    endCondition.notify();
  });

  auto workerTask = runTask({
    result = waitingForUpdates();
    ready = true;
    endCondition.notify();
  });

  while(ready.isNull) endCondition.wait();

  if(ready) disconnectionTask.terminate();
  else workerTask.terminate();

  return result;
}

I've added a new method HTTPServerResponse.waitForConnectionClose in 6e198bc. Not sure if this is a good API, but it should do the job for now.

BTW, Task.terminate is currently not implemented, because it is unsafe functionality (doesn't unwind the stack). Task.interrupt should work, though. Using vibe.core.concurrency it could also be made a bit shorter:

@before!returnResponse("_res")
Data waitForUpdatedData(HTTPServerResponse _res, ...)
{
  auto parent = thisTid;
  Data result;

  auto disctask = runTask({
    try {
      _res.waitForConnectionClose();
      parent.send(false);
    } catch (InterruptException) return;
  });

  auto workertask = runTask({
    try {
      result = waitingForUpdates();
      parent.send(true);
    } catch (InterruptException) return;
  });

  if (receiveOnly!bool) disctask.interrupt();
  else workertask.interrupt();

  return data;
}

Re: REST API long polling

On Fri, 24 Oct 2014 07:40:07 GMT, Sönke Ludwig wrote:
Task.interrupt should work, though. Using vibe.core.concurrency it could also be made a bit shorter:

@before!returnResponse("_res")
Data waitForUpdatedData(HTTPServerResponse _res, ...)
{
  auto parent = thisTid;
  Data result;

  auto disctask = runTask({
    try {
      _res.waitForConnectionClose();
      parent.send(false);
    } catch (InterruptException) return;
  });

  auto workertask = runTask({
    try {
      result = waitingForUpdates();
      parent.send(true);
    } catch (InterruptException) return;
  });

  if (receiveOnly!bool) disctask.interrupt();
  else workertask.interrupt();

  return data;
}

Thanks. This is exactly what I need.