mlh758

mlh758

Reacting to client disconnect

If I have a long running http request, say a search in an admin UI or a report on an analytics dashboard and the user takes an action on the front end that makes that request stale I would abort it with an AbortSignal. This immediately terminates the request from the client’s perspective. If I have this code in C# as an example:

[HttpGet]
public async Task<string> Timeout(CancellationToken abort, int seconds)
{
    var tokenSource = CancellationTokenSource.CreateLinkedTokenSource(abort);
    var duration = new SqlParameter("duration", $"00:00:{seconds:00}");
    tokenSource.CancelAfter(2000);  
    await _context.Users.FromSqlRaw($"WAITFOR DELAY @duration;select * from AspNetUsers", duration).ToListAsync(tokenSource.Token);
    return "Done";
}

And I execute this in the browser:

fetch('https://localhost:7117/api/registration?seconds=2', { signal: AbortSignal.timeout(100)})

The C# code immediately aborts through the token, raising a System.Threading.Tasks.TaskCanceledException that the controller can handle and clean up with. Go does something very similar through a Context value in the request which can be forwarded to the SQL interface and used to abort queries immediately.

A similar function in Phoenix:

def index(conn, %{"seconds" => seconds}) do
  seconds = String.to_integer(seconds)
  query = "SELECT pg_sleep($1)"
  params = [seconds]
  t = Task.async(fn ->
    SQL.query(Timeouts.Repo, query, params)
  end)
  case Task.yield(t, 2_000) do
    {:ok, {:ok, _result}} ->
      send_resp(conn, 200, "Slept for #{seconds} seconds")
    {:ok, {:error, _reason}} ->
      send_resp(conn, 500, "Failed to execute sleep query")
    nil ->
      send_resp(conn, 503, "Failed to complete in time")
  end
end

This handles timeouts nicely, and I can see the query being aborted and cleaned up in the logs. This is 80% of what I want so I’m already pretty happy. However if I send the query with the abort signal it still runs up until the server’s timeout. Does anything in Plug or Bandit signal the client hanging up in a similar way as the C# example? There are a few places in my app where I would like to release resources sooner if possible.

First Post!

shuiRong

shuiRong

I have the same need. After searching in multiple places, it seems that there isn’t any ready-made solution available.

Where Next?

Popular in Questions Top

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41989 114
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31265 143
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52673 488
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement