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

Add register_before_chunk/2 function #1153

Closed
feynmanliang opened this issue Jun 2, 2023 · 1 comment · Fixed by #1154
Closed

Add register_before_chunk/2 function #1153

feynmanliang opened this issue Jun 2, 2023 · 1 comment · Fixed by #1154

Comments

@feynmanliang
Copy link
Contributor

I am building a caching plug with cache keys from conn.body_params and cache values from the conn's response.

We currently have Plug.Conn.register_before_send/2 to register a callback before sending a response. This works great for caching conn.resp_body when it is available.

However, for server sent events (SSEs) and chunked responses more generally, it seems like run_before_send is only run on the initial Plug.Conn.send_chunked/2 call before any chunks are sent. At this moment, conn.resp_body is empty so an empty value is cached.

What we would like to do is to append to the cache as individual chunks are sent using Plug.Conn.chunk/2, but I do not currently see any lifecycle callback available to do this. I am wondering if there is another way to achieve this, and if not would adding a register_before_chunk/2 method to register callbacks (say inside conn.private[:before_send]) which receive the conn and chunk passed to each chunk/2 call be valuable?

I am happy to help contribute the implementation if it is desired.

@whatyouhide whatyouhide changed the title register_before_chunk Add register_before_chunk/2 function Jun 3, 2023
@whatyouhide
Copy link
Member

Closing in favor of the PR #1154.

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

Successfully merging a pull request may close this issue.

2 participants