Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does cancelling a Google Cloud Storage stream affect network use?

I'm developing a media server for my project that will serve streamable audio files. I have set up a caching system so that Google Cloud Storage won't be pinged as much.

But I am noticing Chrome sends two requests to properly stream, the first requests starts streaming the entire file, but then Chrome ignores that since the server sends the Accept byte header, and sends another request with the byte ranges of 0- which means to stream the entire file, again.

The streams are piped directly from Google Cloud Storage (when not cached) but I am concerned about this behaviour being potentially wasteful and I would like to figure out a way to circumvent it.

I have tried the following:

  • On the first request if no byte range header is present, simply send empty data and do not start the stream. This seemed to work in Chrome but made other browsers like Firefox unable to stream the audio.

  • Stream only a small portion on the first request if there is no byte range header. This didn't work either and caused Chrome to think the stream was corrupted. I am assuming this is because of mp3 headers.

Currently my code for handling the requests look like this (extracted from irrelevant code):

const meta = results[0]
const size = Number(meta.size)

const hasRange = !!headers.range

req.size = size
res.set('Accept-Ranges', 'bytes');
res.set('Content-Length', size);

if (!hasRange) {
  req.range = { start: 0, end: size }
  return next()
}

const ranges = getAudioStreamRange(size, headers.range)
req.range = ranges

const { start, end } = ranges

res.set('Content-Length', (end - start) + 1);
res.set('Content-Range', `bytes ${start}-${end}/${size}`);
res.status(206)

next()

Note that the middleware called after this is what sends the streams.

Is there a way to detect when Chrome abandons the first request and then stop the stream, and would this actually factor into network usage from Google Cloud Storage?

like image 497
Sebastian Olsen Avatar asked Sep 01 '26 04:09

Sebastian Olsen


1 Answers

Google Cloud Storage only charges for network egress that was actually sent over the wire. If the client cancels a download and ends the TCP connection, the Cloud Storage server will detect that and stop sending bytes, so no additional network usage will be incurred.

like image 193
lot Avatar answered Sep 02 '26 19:09

lot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!