#How to check, if the browser aborted reading the response?

1 messages · Page 1 of 1 (latest)

grim ledge
#

For some requests, I return a huge blob (Think 2-3GB). But If the browser aborts reading (E.g. tab closed), I need to cancel writing to the Response Body. /* I use version 4.73.0, as it contains
the only way I could find to make it work, as e.g. https://github.com/vapor/vapor/blob/main/Sources/Vapor/Response/Response+Body.swift#L153C1-L153C144 requires the blobs to be synchronously fetched and that is not possible with the API I use. If you know a better way please tell me, as I know it's a non-solution to just use an older version. */

Code snippet (Truncated due to reasons):

app.get("v1", "stream", ":path") { req async throws in
  let file = req.parameters.get("path")!
  let fileStream = findFile(file) // AsyncThrowingStream
  let response = Response(
    body: .init(managedAsyncStream: { writer in
      for try await blob in fileStream { try await writer.write(.buffer(blob)) }
    })
  )
  return response
}

The thing I want to do here is to have some check in my for loop to break out from it, as soon as it's no longer read from. Is that possible? Or am I trying to tackle this problem from the wrong POV?

GitHub

💧 A server-side Swift HTTP web framework. Contribute to vapor/vapor development by creating an account on GitHub.

wind bay
#

You should use FileIO for that, it should handle all the different use cases for you

grim ledge
wind bay
#

Oh so findFile is reading from an SSH stream instead of reading from disk?

grim ledge
pulsar bear
#

@grim ledge any reason you’re not using the SFTP client?

grim ledge
#

I haven't found a way to make it streaming, as my file is really big and without streaming, I would load my entire blob into RAM and then send it. And I want to concurrently receive from my SSH-Host and write into the response

pulsar bear
#

You can also write streaming logic with the SFTP client. But ultimately that's aside of the question here.
What you're asking is possible, but you'd need to rely on task cancellation which I don't think Vapor 4 supports yet

#

To stream using SFTP, you'd need to move the readOffset forward with each read, and read the amount of bytes you'll want from there

grim ledge
#

Ah ok, I feared it would read the entire file and just return the array from offset to offset+length

pulsar bear
#

In SFTP it does, or it can accumulate a big blob for you. But that's exactly what you don't want 🙂

#

But you'd be the one to maintain the reader offset in current APIs

#

cat also works, but is less efficient on the host's side

grim ledge
#

Ok. But basically my entire problem from the beginning is currently not easily solvable? Because fetching the data isn't the problem, just doing a streaming response (IDK whether this is the right term, I'm a webdev newbie) is my problem

wind bay
#

Essentially the channel handler will throw an error, causing the route handler to exit and things to clean up. Whether it actually cleans up is something I'd be interested in

grim ledge
#

Ok, thanks I will try that

pulsar bear