mlh758

mlh758

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.

Showing Posts 1 to 1

shuiRong

shuiRong

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

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews