1

In a web application with a frontend and a backend part, how exactly is the cancellation token mechanism implemented over HTTP? HTTP is a stateless protocol and it does not allow "sharing" any data between the parties. So, how can the frontend create objects which then somehow "monitored" by the backend?

Val
  • 177

4 Answers4

3

Both sides have persistent storage, so we don't need the transport mechanism to have persistent storage. At a very naive level:

  • Frontend to backend: "Please start doing THING for me"
  • Backend to backend: "Sure, please use ID 0d6b739b-9a8d-404d-92cb-48bfb83ced1e to refer to THING"
  • ... time passes ...
  • Frontend to backend: "Please stop what you were doing with ID 0d6b739b-9a8d-404d-92cb-48bfb83ced1e"

Note that the transport mechanism isn't even mentioned here, we don't care if it's HTTP or small gnomes on bicycles.

3

HTTP is implemented on top of TCP. This means that at any point, the client can simply send a FIN packet to the server to close the connection. The server should react to this by indicating request cancellation.

2

The usual cancellation token in ASP isn't transmitted at all, rather it comes from loss of transmission. If the connection between client and webserver is dropped while user code is generating content, the webserver may set the cancellation token to indicate "actually, don't bother, the result will be thrown away".

pjc50
  • 15,223
0

In the context of HTTP requests, cancellation tokens are implemented using the interface in modern web APIs, particularly in JavaScript. It allows you to create an instance and associate it with a fetch request. Then, you can use the associated to signal cancellation, which can be observed by the fetch request, allowing it to abort the ongoing HTTP request.