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

🔥 Feature (v3): Add buffered streaming support #3131

Merged

Conversation

grivera64
Copy link
Member

@grivera64 grivera64 commented Sep 14, 2024

Description

This feature adds buffered streaming support to Fiber v3 through the new Ctx method SendStreamWriter:

func (c Ctx ) SendStreamWriter(streamWriter func(w *bufio.Writer))

c.SendStreamWriter() essentially is a wrapper for calling fasthttp's SetBodyStreamWriter method. This feature wraps this method in the same way that c.SendStream() wraps fasthttp's SetBodyStream().

With this feature, Fiber users can send shorter segments of content over persistent HTTP connections. This functionality is important for several web-based applications such as:

  • Server-Side Events (SSE)
  • Large File Downloads

For example, a simple self-contained SSE example using this new feature can be setup as the following:

homeHtml := `<!DOCTYPE html>
<html>
    <body>
        <h1>SSE Messages</h1>
        <div id="result"></div>

        <script>
            if (typeof(EventSource) !== "undefined") {
                var source = new EventSource("http://127.0.0.1:3000/sse");
                source.addEventListener("my-event", function(event) {
                    document.getElementById("result").innerHTML += event.data + "<br>";
                });
            } else {
                document.getElementById("result").innerHTML = "Sorry, your browser does not support server-sent events...";
            }
        </script>
    </body>
</html>
`
app := fiber.New()
app.Get("/", func(c fiber.Ctx) error {
    c.Set("Content-Type", fiber.MIMETextHTMLCharsetUTF8)
    return c.SendString(homeHtml)
})

app.Get("/sse", func (c fiber.Ctx) error {
    c.Set("Content-Type", "text/event-stream")
    c.Set("Cache-Control", "no-cache")
    c.Set("Connection", "keep-alive")
    c.Set("Transfer-Encoding", "chunked")

    return c.Status(fiber.StatusOK).SendStreamWriter(func (w *bufio.Writer) {
        for {
            fmt.Fprintf(w, "event: my-event\ndata: %s\n\n", time.Now().Local())
            if err := w.Flush(); err != nil {
                return
            }
            time.Sleep(time.Second)
        }
    })
})

Fixes #3127

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)
  • Documentation update (changes to documentation)

CURRENT STATUS

  • Features

    • Add Ctx.SendStreamWriter() to ctx.go
  • Unit Tests

    • Add Test_Ctx_SendStream_Writer to ctx_test.go

    • Add Test_Ctx_SendStreamWriter_Interrupted to ctx_test.go

      ⚠️ WARNING: FIXED ✅
      This test gives a race condition warning when running with -race, but is stable and consistent
      via mutex/channels.

  • Documentation

    • Add Ctx.SendStreamWriter() docs to docs/api/ctx.md
  • Benchmarks

Checklist

Before you submit your pull request, please make sure you meet these requirements:

  • Followed the inspiration of the Express.js framework for new functionalities, making them similar in usage.
  • Conducted a self-review of the code and provided comments for complex or critical parts.
  • Updated the documentation in the /docs/ directory for Fiber's documentation.
  • Added or updated unit tests to validate the effectiveness of the changes or new features.
  • Ensured that new and existing unit tests pass locally with the changes.
  • Verified that any new dependencies are essential and have been agreed upon by the maintainers/community.
  • Aimed for optimal performance with minimal allocations in the new code.
  • Provided benchmarks for the new code to analyze and improve upon.

Create a new `*DefaultCtx` method called `SendStreamWriter()`
that maps to fasthttp's `Response.SetBodyStreamWriter()`
- Adds Test_Ctx_SendStreamWriter to ctx_test.go
- Adds Test_Ctx_SendStreamWriter_Interrupted to ctx_test.go
    - (Work-In-Progress) This test verifies that some data is
      still sent before a client disconnects when using the method
      `c.SendStreamWriter()`.

**Note:** Running this test reports a race condition when using
the `-race` flag or running `make test`. The test uses a channel
and mutex to prevent race conditions, but still triggers a warning.
@grivera64 grivera64 requested a review from a team as a code owner September 14, 2024 00:39
@grivera64 grivera64 requested review from gaby, sixcolors, ReneWerner87 and efectn and removed request for a team September 14, 2024 00:39
Copy link
Contributor

coderabbitai bot commented Sep 14, 2024

Walkthrough

The pull request introduces a new method, SendStreamWriter, to the DefaultCtx struct and the Ctx interface within the fiber package. This method allows users to set a response body stream writer using a function that accepts a pointer to a bufio.Writer. Additionally, two new test functions are added to validate the functionality of SendStreamWriter. Documentation is updated to include usage examples for this new method, enhancing the framework's ability to handle streaming data in HTTP responses.

Changes

Files Change Summary
ctx.go, ctx_interface_gen.go Added SendStreamWriter(func(*bufio.Writer)) error method to DefaultCtx and Ctx interface.
ctx_test.go Introduced tests Test_Ctx_SendStreamWriter and Test_Ctx_SendStreamWriter_Interrupted for the new method.
docs/api/ctx.md Updated documentation to include the SendStreamWriter method with usage examples.

Assessment against linked issues

Objective Addressed Explanation
Add buffered streaming support (Proposal #3127)
Ensure API stability without breaking changes (Proposal #3127)
Provide examples for Server-Side Events (SSE) in Fiber (Proposal #3127)

Possibly related PRs

Suggested reviewers

  • gaby
  • sixcolors
  • ReneWerner87
  • efectn

Poem

In the land of code where rabbits play,
A stream of data flows all day.
With SendStreamWriter, swift and bright,
We send our messages, a joyful sight!
Hopping through bytes, we dance and cheer,
For streaming support is finally here! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@gaby
Copy link
Member

gaby commented Sep 14, 2024

@grivera64 Try this:

func Test_Ctx_SendStreamWriter_Interrupted(t *testing.T) {
    t.Parallel()
    app := New()
    c := app.AcquireCtx(&fasthttp.RequestCtx{})

    var mutex sync.Mutex
    var wg sync.WaitGroup // WaitGroup to synchronize goroutines
    startChan := make(chan bool)

    interruptStreamWriter := func() {
        defer wg.Done() // Notify WaitGroup when done
        <-startChan
        time.Sleep(5 * time.Millisecond)
        mutex.Lock()
        c.Response().CloseBodyStream() //nolint:errcheck // It is fine to ignore the error
        mutex.Unlock()
    }

    wg.Add(1) // Increment WaitGroup counter before starting goroutine
    err := c.SendStreamWriter(func(w *bufio.Writer) {
        go interruptStreamWriter()

        startChan <- true
        for lineNum := 1; lineNum <= 5; lineNum++ {
            mutex.Lock()
            fmt.Fprintf(w, "Line %d\n", lineNum) //nolint:errcheck, revive // It is fine to ignore the error
            mutex.Unlock()

            if err := w.Flush(); err != nil {
                if lineNum < 3 {
                    t.Errorf("unexpected error: %s", err)
                }
                return
            }

            time.Sleep(1500 * time.Microsecond)
        }
    })
    require.NoError(t, err)

    wg.Wait() // Wait for the interruptStreamWriter to finish

    // Protect access to the response body with the mutex
    mutex.Lock()
    defer mutex.Unlock()
    require.Equal(t, "Line 1\nLine 2\nLine 3\n", string(c.Response().Body()))
}

@ReneWerner87 ReneWerner87 added this to the v3 milestone Sep 15, 2024
@grivera64
Copy link
Member Author

grivera64 commented Sep 16, 2024

@gaby Thanks for the recommendation! Adding the Wait Group does remove the race error, but now I am getting an empty response body. I think this may be due to one of the following:

  • Using c.Response().CloseBodyStream() may be freeing the response body internally, making c.Context().Body() no longer valid after waiting long enough. This seems strange, but is the most probable, as it is a race condition.
    • The fix for this would be to find a different method to initiate a client disconnect.
  • The Wait Group isn't waiting long enough to allow Flush() to work. This is least probable, since I also tried increasing the counter to ensure that both interruptStreamWriter and SendStreamWriter finished executing, and the body returned is still empty:
// go test -run Test_Ctx_SendStreamWriter_Interrupted
func Test_Ctx_SendStreamWriter_Interrupted(t *testing.T) {
	t.Parallel()
	app := New()
	c := app.AcquireCtx(&fasthttp.RequestCtx{})

	var mutex sync.Mutex
	var wg sync.WaitGroup
	startChan := make(chan bool)

	interruptStreamWriter := func() {
		wg.Add(1)
		defer wg.Done()
		<-startChan
		time.Sleep(5 * time.Millisecond)
		mutex.Lock()
		c.Response().CloseBodyStream() //nolint:errcheck // It is fine to ignore the error
		mutex.Unlock()
	}

	wg.Add(1)
	err := c.SendStreamWriter(func(w *bufio.Writer) {
		go interruptStreamWriter()
		defer wg.Done()

		startChan <- true
		for lineNum := 1; lineNum <= 5; lineNum++ {
			mutex.Lock()
			fmt.Fprintf(w, "Line %d\n", lineNum) //nolint:errcheck, revive // It is fine to ignore the error
			mutex.Unlock()

			if err := w.Flush(); err != nil {
				if lineNum < 3 {
					t.Errorf("unexpected error: %s", err)
				}
				return
			}

			time.Sleep(1500 * time.Microsecond)
		}
	})
	require.NoError(t, err)

	// Wait for StreamWriter and the goroutine to finish
	wg.Wait()
	mutex.Lock()
	require.Equal(t, "Line 1\nLine 2\nLine 3\n", string(c.Response().Body()))
	mutex.Unlock()
}

I will file an issue on valyala/fasthttp to ask how to mock client disconnections for this test. If it's not possible, we could remove this test case, as most of the other tests do not test for client disconnection issues.

@grivera64
Copy link
Member Author

grivera64 commented Sep 17, 2024

Hey all, after reading a bit more on Fiber and Fasthttp documentation, I believe that the race condition is due to fasthttp.(*Response) not being concurrent-safe.

I tried using a modified version of app's Test() method, that closes an underlying net.Conn connection (I modified testConn too to make Close() block further writes but allow reads). This seemed to do the same as c.Response().CloseBodyStream(), but still gave the race condition issue.

This is the new race condition warning:

WARNING: DATA RACE
Read at 0x00c00016c130 by goroutine 9:
  github.com/gofiber/fiber/v3.(*testConn).Write()
      /fiber/helpers.go:628 +0x6b
  bufio.(*Writer).Flush()
      /usr/local/go/src/bufio/bufio.go:639 +0xee
  github.com/valyala/fasthttp.writeChunk()
      /go/pkg/mod/github.com/valyala/fasthttp@v1.55.0/http.go:2250 +0x10b
  github.com/valyala/fasthttp.writeBodyChunked()
      /go/pkg/mod/github.com/valyala/fasthttp@v1.55.0/http.go:2170 +0xce
  github.com/valyala/fasthttp.(*Response).writeBodyStream()
      /go/pkg/mod/github.com/valyala/fasthttp@v1.55.0/http.go:2066 +0x338
  github.com/valyala/fasthttp.(*Response).Write()
      /go/pkg/mod/github.com/valyala/fasthttp@v1.55.0/http.go:1967 +0x2c4
  github.com/valyala/fasthttp.writeResponse()
      /go/pkg/mod/github.com/valyala/fasthttp@v1.55.0/server.go:2589 +0xb8
  github.com/valyala/fasthttp.(*Server).serveConn()
      /go/pkg/mod/github.com/valyala/fasthttp@v1.55.0/server.go:2432 +0x1ead
  github.com/valyala/fasthttp.(*Server).ServeConn()
      /go/pkg/mod/github.com/valyala/fasthttp@v1.55.0/server.go:2042 +0x154
  github.com/gofiber/fiber/v3.(*App).TestWithInterrupt.func1()
      /fiber/app.go:975 +0xde
...

This was the old warning when directly using c.SendStreamWriter() without a server:

github.com/valyala/fasthttp.(*Response).SetBodyStream()
      /go/pkg/mod/github.com/valyala/fasthttp@v1.55.0/http.go:249 +0x4f
  github.com/valyala/fasthttp.(*Response).SetBodyStreamWriter()
      /go/pkg/mod/github.com/valyala/fasthttp@v1.55.0/http.go:292 +0x64
...

I believe fixing this race warning in the cleanest way possible would require an upstream PR to fasthttp (most likely adding a mutex for fasthttp.(*Response) related methods).

Based on Fiber's current codebase, there doesn't seem to be other interrupt tests (while other tests ignore output when the response times out). If this isn't something we should be testing for, we could just remove the interrupt test and keep the remaining tests.

What are your thoughts on this? Please let me know if you want to see the modified app.Test() code.

Edit: I will still try to work with the modified app.Test() code to see if I can get the race error to go away, since it still does look promising despite the above.

@grivera64
Copy link
Member Author

After editing app.Test() a bit, I was able to get a working test without the race warning. I had to create an app method called TestWithInterrupt():

// TestWithInterrupt is used for internal debugging by passing a *http.Request with an interruptAfter duration.
func (app *App) TestWithInterrupt(req *http.Request, interruptAfter time.Duration) (*http.Response, error)

With this change, I was able to use that in the test case as written below as a fix:

func Test_Ctx_SendStreamWriter_Interrupted_New(t *testing.T) {
	t.Parallel()
	app := New(Config{StreamRequestBody: true})
	app.Get("/", func(c Ctx) error {
		return c.SendStreamWriter(func(w *bufio.Writer) {
			for lineNum := 1; lineNum <= 5; lineNum++ {
				time.Sleep(time.Duration(lineNum) * time.Millisecond)

				fmt.Fprintf(w, "Line %d\n", lineNum) //nolint:errcheck, revive // It is fine to ignore the error
				if err := w.Flush(); err != nil {
					if lineNum <= 3 {
						t.Errorf("unexpected error: %s", err)
					}
					return
				}
			}
		})
	})
	resp, err := app.TestWithInterrupt(httptest.NewRequest(MethodGet, "/", nil), 8*time.Millisecond)
	require.NoError(t, err, "app.TestWithInterrupt(req)")
	body, err := io.ReadAll(resp.Body)
	require.NotNil(t, err)
	require.Equal(t, "Line 1\nLine 2\nLine 3\n", string(body))
}

Would you all like me to write an issue/PR for adding app.TestWithInterrupt() then use the function here after the merge?

@grivera64
Copy link
Member Author

Hello all, I hope you all have been doing well. I created an issue for the TestWithInterrupt() I proposed above. Please check it out here:

#3149

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

🧹 Outside diff range and nitpick comments (3)
ctx_interface_gen.go (2)

286-287: LGTM: New SendStreamWriter method

The SendStreamWriter method is a great addition for buffered streaming support. The signature is well-designed and consistent with other methods in the interface.

Consider adding a brief comment above the method to describe its purpose and usage, similar to other methods in this interface. For example:

// SendStreamWriter sets the response body stream writer for buffered streaming

This would improve the documentation and make it easier for developers to understand the method's purpose at a glance.


Line range hint 1-387: Summary: Successful implementation of buffered streaming support

The changes to ctx_interface_gen.go effectively introduce buffered streaming support to the Fiber v3 framework:

  1. The addition of the "bufio" import provides the necessary functionality for buffered I/O operations.
  2. The new SendStreamWriter method in the Ctx interface allows for setting a response body stream writer, enabling buffered streaming.

These changes align well with the PR objectives and maintain consistency with the existing Fiber API. The implementation is non-breaking and focused, adding functionality without modifying existing methods.

To fully leverage this new feature:

  1. Ensure that the concrete implementation of the Ctx interface properly handles the SendStreamWriter method.
  2. Update the documentation to include examples of how to use this new method for Server-Side Events (SSE) and large file downloads.
  3. Consider adding unit tests that specifically target this new method to ensure its correct functionality and integration with the rest of the framework.
ctx.go (1)

1673-1679: LGTM! Consider enhancing error handling.

The implementation of SendStreamWriter looks good and aligns well with the PR objectives. It correctly uses fasthttp.StreamWriter to set the body stream writer.

Consider enhancing the error handling. Currently, the method always returns nil. You might want to capture and return any potential errors that could occur during the stream writing process. This could be done by modifying the streamWriter function signature to return an error and handling it accordingly.

-func (c *DefaultCtx) SendStreamWriter(streamWriter func(*bufio.Writer)) error {
-	c.fasthttp.Response.SetBodyStreamWriter(fasthttp.StreamWriter(streamWriter))
-
-	return nil
+func (c *DefaultCtx) SendStreamWriter(streamWriter func(*bufio.Writer) error) error {
+	return c.fasthttp.Response.SetBodyStreamWriter(fasthttp.StreamWriter(func(w *bufio.Writer) error {
+		return streamWriter(w)
+	}))
}

This change would allow for more robust error handling and propagation.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 024ac5e and 7b238d1.

📒 Files selected for processing (4)
  • ctx.go (2 hunks)
  • ctx_interface_gen.go (2 hunks)
  • ctx_test.go (2 hunks)
  • docs/api/ctx.md (1 hunks)
🔇 Additional comments (2)
ctx_interface_gen.go (1)

6-6: LGTM: Import addition for bufio package

The addition of the "bufio" import is correct and necessary for the new SendStreamWriter method. It's properly placed within the existing import block.

docs/api/ctx.md (1)

1874-1914: Excellent addition of SendStreamWriter method documentation!

The added documentation for the SendStreamWriter method is well-structured, clear, and informative. It provides:

  1. A concise description of the method's purpose.
  2. The method signature for easy reference.
  3. A basic usage example that demonstrates how to use the method.
  4. An advanced example showcasing manual flushing for more complex scenarios.
  5. Important notes about the flushing behavior, which is crucial for users to understand.

The documentation style and format are consistent with the rest of the file, making it easy for users to understand and integrate into their workflow. Great job on providing comprehensive and user-friendly documentation for this new method!

ctx_test.go Show resolved Hide resolved
ctx_test.go Show resolved Hide resolved
ctx_test.go Outdated Show resolved Hide resolved
ctx_test.go Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Outside diff range and nitpick comments (4)
docs/api/ctx.md (2)

1859-1862: Improve note formatting and clarity.

The note about the streamWriter function could be clearer and more detailed.

-:::note
-The argument `streamWriter` represents a function that populates
-the response body using a buffered stream writer.
-:::
+:::info
+The `streamWriter` argument is a function that receives a `*bufio.Writer` and is responsible for writing 
+the response body. The writer is buffered for performance, and its contents will be automatically 
+flushed after the function returns unless manually flushed within the function.
+:::

1855-1894: Add important usage notes and best practices.

The documentation should include important notes about common pitfalls and best practices.

Add the following notes section after the examples:

:::caution
Important considerations when using `SendStreamWriter`:
1. Always handle write errors as they indicate client disconnection
2. Long-running operations should periodically check for client disconnection
3. Large responses should be sent in chunks with periodic flushes
4. The writer buffer is automatically flushed when the function returns
5. The connection may be closed by the client at any time
:::

:::tip
Best practices:
1. Use `SendStreamWriter` for streaming large responses or real-time data
2. Implement proper error handling for all write operations
3. Consider using timeouts for long-running operations
4. Use periodic flushes for real-time updates
5. Clean up resources if the client disconnects early
:::
ctx_test.go (2)

4490-4490: Avoid using fixed time.Sleep in tests

Using fixed delays like time.Sleep(5 * time.Millisecond) can lead to flaky tests due to timing uncertainties on different systems. Consider using synchronization mechanisms to ensure proper timing without arbitrary sleeps.


4511-4511: Avoid using fixed time.Sleep in tests

Using fixed delays like time.Sleep(1500 * time.Microsecond) can cause flaky tests. Consider synchronizing using channels or other synchronization primitives instead of time.Sleep.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 7b238d1 and 95b1745.

📒 Files selected for processing (4)
  • ctx.go (2 hunks)
  • ctx_interface_gen.go (2 hunks)
  • ctx_test.go (2 hunks)
  • docs/api/ctx.md (1 hunks)
🔇 Additional comments (7)
ctx_interface_gen.go (2)

6-6: LGTM: Required import for new streaming functionality

The addition of the bufio import is necessary for the new SendStreamWriter method.


287-288: LGTM: Well-designed streaming method signature

The new SendStreamWriter method is well-designed for buffered streaming:

  • Takes a writer function that can write to a buffered stream
  • Returns an error for proper error handling
  • Follows the framework's naming conventions
  • Non-breaking addition to the interface

Let's verify the implementation and usage patterns:

✅ Verification successful

Implementation and usage patterns verified successfully

The implementation and usage patterns are well-tested and documented:

  • Implementation in ctx.go correctly wraps fasthttp's StreamWriter
  • Comprehensive test coverage in ctx_test.go includes:
    • Basic streaming functionality
    • Multi-line streaming with flush
    • Empty stream handling
    • Interrupt handling with proper cleanup
  • Well-documented in docs/api/ctx.md with clear examples and usage notes
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check implementation and usage patterns of SendStreamWriter

# Find the implementation
echo "Looking for SendStreamWriter implementation..."
ast-grep --pattern 'func (c *DefaultCtx) SendStreamWriter($_) $_'

# Find example usages
echo "Looking for example usages..."
rg "SendStreamWriter" -A 5 -B 5

Length of output: 5508

ctx.go (1)

1676-1680: LGTM! Implementation is clean and correct.

The implementation properly wraps the fasthttp.StreamWriter functionality while maintaining a clean API.

ctx_test.go (4)

4458-4458: Check the error returned by w.WriteString

This issue was previously noted and remains applicable.


4465-4465: Check the error returned by fmt.Fprintf

This issue was previously noted and remains applicable.


4492-4492: Check the error returned by c.Response().CloseBodyStream()

This issue was previously noted and remains applicable.


4501-4501: Check the error returned by fmt.Fprintf

This issue was previously noted and remains applicable.

docs/api/ctx.md Show resolved Hide resolved
docs/api/ctx.md Outdated Show resolved Hide resolved
Comment on lines +1675 to +1676
// SendStreamWriter sets response body stream writer
func (c *DefaultCtx) SendStreamWriter(streamWriter func(*bufio.Writer)) error {
Copy link
Contributor

@coderabbitai coderabbitai bot Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add comprehensive documentation for the new public API method.

The method lacks proper documentation. As this is a new public API method for buffered streaming support, it should include:

  • A description of its purpose and use cases (e.g., SSE, large file downloads)
  • Parameter explanation
  • Return value description
  • Usage examples
  • Any important notes about concurrency or memory management

Add documentation like this:

+// SendStreamWriter sets the response body stream writer for sending 
+// chunked data through a callback function.
+//
+// The callback function is called when the response is being written to the client.
+// This allows for efficient memory usage as data can be written in chunks.
+//
+// Perfect for:
+// - Server-Sent Events (SSE)
+// - Large file downloads
+// - Real-time data streaming
+//
+// Example:
+//
+//	app.Get("/", func(c fiber.Ctx) error {
+//		return c.SendStreamWriter(func(w *bufio.Writer) {
+//			fmt.Fprintf(w, "Hello, World!")
+//			w.Flush()
+//		})
+//	})
 // SendStreamWriter sets response body stream writer
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// SendStreamWriter sets response body stream writer
func (c *DefaultCtx) SendStreamWriter(streamWriter func(*bufio.Writer)) error {
// SendStreamWriter sets the response body stream writer for sending
// chunked data through a callback function.
//
// The callback function is called when the response is being written to the client.
// This allows for efficient memory usage as data can be written in chunks.
//
// Perfect for:
// - Server-Sent Events (SSE)
// - Large file downloads
// - Real-time data streaming
//
// Example:
//
// app.Get("/", func(c fiber.Ctx) error {
// return c.SendStreamWriter(func(w *bufio.Writer) {
// fmt.Fprintf(w, "Hello, World!")
// w.Flush()
// })
// })
func (c *DefaultCtx) SendStreamWriter(streamWriter func(*bufio.Writer)) error {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should add an example/recipe about this feature after the PR get merged

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@efectn, that's a great idea. Adding an example or recipe about this feature after the PR is merged will be beneficial for users. Would you like me to open a new GitHub issue to track this follow-up action?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

@efectn
Copy link
Member

efectn commented Nov 24, 2024

@grivera64 can you update testcases using new app.Test config properties?

@grivera64
Copy link
Member Author

@grivera64 can you update testcases using new app.Test config properties?

For sure @efectn, I am working on using the new app.Test() for the unit test Test_Ctx_SendStreamWriter_Interrupted.

Should I also update the previous Test_Ctx_SendStreamWriter unit test to be consistent with the interrupted version, even though it still works as-is?

Copy link

codecov bot commented Nov 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.79%. Comparing base (ff55cfd) to head (76dee57).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3131   +/-   ##
=======================================
  Coverage   82.79%   82.79%           
=======================================
  Files         114      114           
  Lines       11193    11197    +4     
=======================================
+ Hits         9267     9271    +4     
  Misses       1526     1526           
  Partials      400      400           
Flag Coverage Δ
unittests 82.79% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@efectn
Copy link
Member

efectn commented Nov 25, 2024

@grivera64 can you update testcases using new app.Test config properties?

For sure @efectn, I am working on using the new app.Test() for the unit test Test_Ctx_SendStreamWriter_Interrupted.

Should I also update the previous Test_Ctx_SendStreamWriter unit test to be consistent with the interrupted version, even though it still works as-is?

I think it's good as-is since app.Test is not used for that test

@ReneWerner87
Copy link
Member

@grivera64 thanks for this feature
can you check our ai hints
this could help to get the best out of it

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (1)
ctx.go (1)

1676-1680: Add parameter validation and error handling.

The current implementation could be improved with:

  1. Parameter validation
  2. Error handling from the writer
  3. Context cancellation support

Consider this enhanced implementation:

 func (c *DefaultCtx) SendStreamWriter(streamWriter func(*bufio.Writer)) error {
+    if streamWriter == nil {
+        return fmt.Errorf("streamWriter cannot be nil")
+    }
+
+    done := make(chan error, 1)
+    
+    c.fasthttp.Response.SetBodyStreamWriter(func(w *bufio.Writer) {
+        if err := c.Context().Err(); err != nil {
+            done <- err
+            return
+        }
+        
+        streamWriter(w)
+        done <- nil
+    })
+
+    return <-done
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between d82e3bc and 4d36527.

📒 Files selected for processing (1)
  • ctx.go (2 hunks)
🔇 Additional comments (1)
ctx.go (1)

1676-1680: Integration looks good!

The method:

  • Follows the established pattern of other streaming methods
  • Properly integrates with fasthttp's streaming capabilities
  • Maintains consistency with the existing API design

Comment on lines +1675 to +1676
// SendStreamWriter sets response body stream writer
func (c *DefaultCtx) SendStreamWriter(streamWriter func(*bufio.Writer)) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add comprehensive documentation for the new public API method.

As this is a new public API method for buffered streaming support, it should include:

  • A description of its purpose and use cases (e.g., SSE, large file downloads)
  • Parameter explanation
  • Example usage with SSE
  • Notes about concurrency and memory management

Apply this diff to improve the documentation:

-// SendStreamWriter sets response body stream writer
+// SendStreamWriter sets the response body stream writer for sending chunked data through a callback function.
+//
+// The callback function is called when the response is being written to the client.
+// This allows for efficient memory usage as data can be written in chunks.
+//
+// Perfect for:
+// - Server-Sent Events (SSE)
+// - Large file downloads
+// - Real-time data streaming
+//
+// Example:
+//  app.Get("/sse", func(c fiber.Ctx) error {
+//    c.Set("Content-Type", "text/event-stream")
+//    c.Set("Cache-Control", "no-cache")
+//    c.Set("Connection", "keep-alive")
+//    c.Set("Transfer-Encoding", "chunked")
+//
+//    return c.SendStreamWriter(func(w *bufio.Writer) {
+//      fmt.Fprintf(w, "event: message\ndata: Hello SSE\n\n")
+//      w.Flush()
+//    })
+//  })
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// SendStreamWriter sets response body stream writer
func (c *DefaultCtx) SendStreamWriter(streamWriter func(*bufio.Writer)) error {
// SendStreamWriter sets the response body stream writer for sending chunked data through a callback function.
//
// The callback function is called when the response is being written to the client.
// This allows for efficient memory usage as data can be written in chunks.
//
// Perfect for:
// - Server-Sent Events (SSE)
// - Large file downloads
// - Real-time data streaming
//
// Example:
// app.Get("/sse", func(c fiber.Ctx) error {
// c.Set("Content-Type", "text/event-stream")
// c.Set("Cache-Control", "no-cache")
// c.Set("Connection", "keep-alive")
// c.Set("Transfer-Encoding", "chunked")
//
// return c.SendStreamWriter(func(w *bufio.Writer) {
// fmt.Fprintf(w, "event: message\ndata: Hello SSE\n\n")
// w.Flush()
// })
// })
func (c *DefaultCtx) SendStreamWriter(streamWriter func(*bufio.Writer)) error {

@grivera64
Copy link
Member Author

grivera64 commented Nov 26, 2024

@grivera64 thanks for this feature can you check our ai hints this could help to get the best out of it

No problem @ReneWerner87 !

In regards to the Code Rabbit hints, I've added a modified simple SSE example using the example the AI hints gave to the whats_new.md file.

The hints also mention about checking the return value of fmt.Fprintf(w, ...). It appears that fmt.Fprintf (under normal circumstances if I'm not mistaken) should not return any errors, including if the connection is disconnected. The method w.Flush() does return an error in this on a disconnection. Should we still include error checking for fmt.Fprintf calls in the examples?

Thanks again for the suggestions and comments!

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (4)
docs/whats_new.md (2)

302-303: Consider adding more context about buffered streaming.

The introduction could be enhanced by explaining what buffered streaming is and its advantages over non-buffered streaming. This would help developers understand when to use this feature.

-In v3, we added support for buffered streaming by providing the new method `SendStreamWriter()`.
+In v3, we added support for buffered streaming through the new method `SendStreamWriter()`. Buffered streaming allows you to efficiently write data in chunks using a buffer, which is particularly useful for handling large datasets or real-time data streams while maintaining memory efficiency.

335-336: Add reference to performance considerations.

The documentation should include a section about performance considerations when using buffered streaming, such as buffer sizes and memory usage.

 You can find more details about this feature in [/docs/api/ctx.md](./api/ctx.md).
+
+### Performance Considerations
+
+When using buffered streaming, consider the following:
+- Buffer size affects memory usage and throughput
+- For large files, consider using appropriate chunk sizes
+- Monitor memory usage when handling multiple concurrent streams
+- Use appropriate timeouts to prevent resource exhaustion
docs/api/ctx.md (2)

1855-1866: Add use-case context to the documentation.

Consider adding a brief explanation of when to use SendStreamWriter vs other response methods like SendStream. This would help developers choose the right method for their needs.

Add this note before the existing note:

+:::tip
+Use `SendStreamWriter` when you need to write response data incrementally or stream large responses without loading them entirely into memory. This is particularly useful for Server-Sent Events (SSE), large file downloads, or real-time data streaming.
+:::

 :::note
 The argument `streamWriter` represents a function that populates
 the response body using a buffered stream writer.
 :::

1868-1875: Set appropriate Content-Type header in the example.

The example should demonstrate setting the appropriate Content-Type header for the response.

 app.Get("/", func (c fiber.Ctx) error {
+  c.Set("Content-Type", "text/plain")
   return c.SendStreamWriter(func(w *bufio.Writer) {
     fmt.Fprintf(w, "Hello, World!\n")
   })
 })
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 4d36527 and c59764e.

📒 Files selected for processing (2)
  • docs/api/ctx.md (1 hunks)
  • docs/whats_new.md (2 hunks)

Comment on lines +315 to +333
app.Get("/sse", func(c fiber.Ctx) {
c.Set("Content-Type", "text/event-stream")
c.Set("Cache-Control", "no-cache")
c.Set("Connection", "keep-alive")
c.Set("Transfer-Encoding", "chunked")

return c.SendStreamWriter(func(w *bufio.Writer) {
for {
fmt.Fprintf(w, "event: my-event\n")
fmt.Fprintf(w, "data: Hello SSE\n\n")

if err := w.Flush(); err != nil {
log.Print("Client disconnected!")
return
}
}
})
})
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance the SSE example with best practices and error handling.

The Server-Sent Events example could be improved by:

  1. Adding proper error handling for write operations
  2. Including a cancellation mechanism
  3. Demonstrating how to handle client disconnections gracefully
 app.Get("/sse", func(c fiber.Ctx) {
   c.Set("Content-Type", "text/event-stream")
   c.Set("Cache-Control", "no-cache")
   c.Set("Connection", "keep-alive")
   c.Set("Transfer-Encoding", "chunked")
 
+  // Create a channel to handle cancellation
+  done := make(chan bool)
+
+  // Handle client disconnection
+  c.Context().SetBodyStreamWriter(func(w *bufio.Writer) {
+    ticker := time.NewTicker(time.Second)
+    defer ticker.Stop()
+
+    for {
+      select {
+      case <-done:
+        return
+      case <-ticker.C:
+        // Write event with proper error handling
+        if _, err := fmt.Fprintf(w, "event: my-event\n"); err != nil {
+          log.Printf("Error writing event: %v", err)
+          return
+        }
+        if _, err := fmt.Fprintf(w, "data: Hello SSE\n\n"); err != nil {
+          log.Printf("Error writing data: %v", err)
+          return
+        }
+
+        if err := w.Flush(); err != nil {
+          log.Printf("Client disconnected: %v", err)
+          return
+        }
+      }
+    }
+  })
+
+  // Clean up when the handler returns
+  defer close(done)
+
   return c.SendStreamWriter(func(w *bufio.Writer) {
     for {
       fmt.Fprintf(w, "event: my-event\n")
       fmt.Fprintf(w, "data: Hello SSE\n\n")
 
       if err := w.Flush(); err != nil {
         log.Print("Client disconnected!")
         return
       }
     }
   })
 })

Committable suggestion skipped: line range outside the PR's diff.

docs/api/ctx.md Outdated
Comment on lines 1887 to 1913
```go title="Example"
app.Get("/wait", func(c fiber.Ctx) error {
return c.SendStreamWriter(func(w *bufio.Writer) {
// Begin Work
fmt.Fprintf(w, "Please wait for 10 seconds\n")
if err := w.Flush(); err != nil {
log.Print("Client disconnected!")
return
}

// Send progress over time
time.Sleep(time.Second)
for i := 0; i < 9; i++ {
fmt.Fprintf("Still waiting...\n")
if err := w.Flush(); err != nil {
// If client disconnected, cancel work and finish
log.Print("Client disconnected!")
return
}
time.Sleep(time.Second)
}

// Finish
fmt.Fprintf(w, "Done!\n")
})
})
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix syntax error in fmt.Fprintf call.

There's a syntax error in the fmt.Fprintf call that would prevent compilation.

-      fmt.Fprintf("Still waiting...\n")
+      fmt.Fprintf(w, "Still waiting...\n")
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
```go title="Example"
app.Get("/wait", func(c fiber.Ctx) error {
return c.SendStreamWriter(func(w *bufio.Writer) {
// Begin Work
fmt.Fprintf(w, "Please wait for 10 seconds\n")
if err := w.Flush(); err != nil {
log.Print("Client disconnected!")
return
}
// Send progress over time
time.Sleep(time.Second)
for i := 0; i < 9; i++ {
fmt.Fprintf("Still waiting...\n")
if err := w.Flush(); err != nil {
// If client disconnected, cancel work and finish
log.Print("Client disconnected!")
return
}
time.Sleep(time.Second)
}
// Finish
fmt.Fprintf(w, "Done!\n")
})
})
```
```go title="Example"
app.Get("/wait", func(c fiber.Ctx) error {
return c.SendStreamWriter(func(w *bufio.Writer) {
// Begin Work
fmt.Fprintf(w, "Please wait for 10 seconds\n")
if err := w.Flush(); err != nil {
log.Print("Client disconnected!")
return
}
// Send progress over time
time.Sleep(time.Second)
for i := 0; i < 9; i++ {
fmt.Fprintf(w, "Still waiting...\n")
if err := w.Flush(); err != nil {
// If client disconnected, cancel work and finish
log.Print("Client disconnected!")
return
}
time.Sleep(time.Second)
}
// Finish
fmt.Fprintf(w, "Done!\n")
})
})
```

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (3)
docs/api/ctx.md (3)

1857-1862: Enhance method description with more details.

The description could be more comprehensive by explaining:

  • The purpose of using a buffered stream writer
  • Common use cases (e.g., Server-Sent Events, large file downloads)
  • The relationship with the underlying fasthttp library
-Sets the response body stream writer.
+Sets the response body stream writer, enabling buffered streaming of response data.
+This method is particularly useful for implementing Server-Sent Events (SSE),
+streaming large files, or any scenario requiring chunked data transfer.
+It wraps the fasthttp.SetBodyStreamWriter functionality to provide a simpler interface.

1877-1881: Enhance the flushing note with more context.

The note about flushing could be more informative by explaining the benefits and use cases.

-To send data before `streamWriter` returns, you can call `w.Flush()`
-on the provided writer. Otherwise, the buffered stream flushes after
-`streamWriter` returns.
+To send data immediately before `streamWriter` returns, call `w.Flush()`
+on the provided writer. This is useful for:
+- Sending initial data quickly while processing continues
+- Implementing real-time progress updates
+- Managing memory usage for large responses
+If not manually flushed, the buffered stream automatically flushes after
+`streamWriter` returns.

1883-1885: Enhance the client disconnection note with best practices.

The note about client disconnection could provide more guidance on error handling.

-`w.Flush()` will return an error if the client disconnects before `streamWriter` finishes writing a response.
+`w.Flush()` returns an error if the client disconnects before `streamWriter` finishes writing a response.
+This error should be handled to:
+- Clean up resources
+- Cancel ongoing operations
+- Log the disconnection if needed
+See the advanced example below for proper error handling implementation.
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between c59764e and 76dee57.

📒 Files selected for processing (1)
  • docs/api/ctx.md (1 hunks)
🔇 Additional comments (2)
docs/api/ctx.md (2)

1868-1875: Previous review comment about error handling is still applicable.


1887-1913: Previous review comment about the syntax error is still applicable.

@gaby
Copy link
Member

gaby commented Nov 26, 2024

@grivera64 I think you have to run make generate since the ctx.go changed

Copy link
Member

@gaby gaby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Just one comment. LGTM

@grivera64
Copy link
Member Author

@grivera64 I think you have to run make generate since the ctx.go changed

Running make generate doesn't provide any new diffs to commit:

fiber (feature/add-buffered-streaming-support) $ make generate
go install github.com/tinylib/msgp@latest
go install github.com/vburenin/ifacemaker@975a95966976eeb2d4365a7fb236e274c54da64c
go generate ./...
fiber (feature/add-buffered-streaming-support) $ git status
On branch feature/add-buffered-streaming-support
Your branch is up to date with 'origin/feature/add-buffered-streaming-support'.

nothing to commit, working tree clean

Is this fine?

@grivera64
Copy link
Member Author

👍 Just one comment. LGTM

@gaby For sure thanks for the review!

With regards to the recipes/example comment, should I make an issue on gofiber/recipes to update the original SSE example to use c.SendStreamWriter()?

@efectn
Copy link
Member

efectn commented Nov 27, 2024

👍 Just one comment. LGTM

@gaby For sure thanks for the review!

With regards to the recipes/example comment, should I make an issue on gofiber/recipes to update the original SSE example to use c.SendStreamWriter()?

Yes, you can

@ReneWerner87 ReneWerner87 merged commit 31a503f into gofiber:main Nov 27, 2024
17 of 18 checks passed
@gaby
Copy link
Member

gaby commented Nov 27, 2024

@grivera64 I think you have to run make generate since the ctx.go changed

Running make generate doesn't provide any new diffs to commit:

fiber (feature/add-buffered-streaming-support) $ make generate
go install github.com/tinylib/msgp@latest
go install github.com/vburenin/ifacemaker@975a95966976eeb2d4365a7fb236e274c54da64c
go generate ./...
fiber (feature/add-buffered-streaming-support) $ git status
On branch feature/add-buffered-streaming-support
Your branch is up to date with 'origin/feature/add-buffered-streaming-support'.

nothing to commit, working tree clean

Is this fine?

Yes, that's fine 💪

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

Successfully merging this pull request may close these issues.

📝 [Proposal]: Add buffered streaming support
4 participants