Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement shared buffer for multiple clients per stream ID #220

Merged
merged 37 commits into from
Feb 4, 2025

Conversation

sonroyaalmerol
Copy link
Owner

@sonroyaalmerol sonroyaalmerol commented Feb 2, 2025

Context

Choices

  • Removed BUFFER_MB in favor of BUFFER_CHUNK_NUM which now refers to the number of chunks stored in the shared buffer with the defaults set to 4. Each chunk will be fixed to 1 MB.

How it works

The stream buffer system is essentially an implementation of a Circular Buffer data structure.

Imagine a circular sushi conveyor belt (ring.Ring) with a fixed number of plates (BUFFER_CHUNK_NUM). Each plate (ChunkData) can hold a piece of sushi (chunk of data).

The Chef (Writer)

At the preparation station, there's a chef (StartWriter) who:

  • Takes raw fish (stream data) and cuts it into bite-sized pieces (ChunkSize)
  • Places each piece on an empty plate (ChunkData)
  • Puts the plate on the conveyor belt and moves to the next empty plate
  • If something goes wrong with the fish (error), marks the plate with a warning flag

The chef works continuously unless:

  • The restaurant closes (context cancelled)
  • There are no more customers (client count drops to 0)
  • They run out of fish (EOF)
  • Something goes wrong in the kitchen (error)

The Customers (Readers)

Customers (ReadChunks) sitting at different points around the belt:

  • Remember which plate they last looked at (fromPosition)
  • Can look at all plates that have passed by since they last checked
  • Make their own copy of each piece of sushi they want (copying the data)
  • If they catch up to where the chef is currently placing plates, they wait (dataNotify.Wait())
  • If they see a plate with a warning flag (error), they know to stop eating

If a customer is too slow and takes too long to check a plate, the data might get replaced by the time they look again (buffer overwrite)

This system ensures that streaming data (sushi) flows smoothly from the source (kitchen) to multiple consumers (customers) while efficiently managing memory (plates) and handling errors (food safety warnings).

Checklist before requesting a review

  • I have performed a self-review of my code
  • I've added documentation about this change to the README.
  • I've not introduced breaking changes.

@sonroyaalmerol sonroyaalmerol marked this pull request as ready for review February 2, 2025 17:25
@sonroyaalmerol
Copy link
Owner Author

@kpirnie Sorry to bother you but do you mind trying this out? This is a completely different implementation of the existing (and dead simple) buffer but allows multiple clients to share one stream connection. I was wondering if this idea scales fine with the amount of providers you have.

Theoretically, the memory usage of this implementation should be proportional by the number of active connections to the IPTV providers and not the number of client connections necessarily.

Copy link

github-actions bot commented Feb 4, 2025

The Docker image for this pull request has been built and pushed to GHCR.

Image URL: ghcr.io/sonroyaalmerol/m3u-stream-merger-proxy:pr-220

@sonroyaalmerol sonroyaalmerol merged commit ef1a0e3 into main Feb 4, 2025
6 checks passed
@sonroyaalmerol sonroyaalmerol deleted the shared-buffer branch February 4, 2025 01:48
@aniel300
Copy link

aniel300 commented Feb 4, 2025

am having major buffering with this pr. idk if it something related to my set up or what

@sonroyaalmerol
Copy link
Owner Author

Hi @aniel300. I can't help you without any debug logs. Please create a new issue with more context. Also, there are important notes in the release changelog.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Follow ffmpeg's buffer behavior to reduce playback issues on slow connections
2 participants