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

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
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
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
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

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31194 112
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 39523 209
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

We're in Beta

About us Mission Statement