Skip to content

Commit

Permalink
[middleware/proxy] add buffer size configuration (#1292)
Browse files Browse the repository at this point in the history
* add buffer size

* add tests
  • Loading branch information
jney authored Apr 20, 2021
1 parent a48cb80 commit c3aafde
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
9 changes: 9 additions & 0 deletions middleware/proxy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ type Config struct {
//
// Optional. Default: nil
ModifyResponse fiber.Handler

// Per-connection buffer size for requests' reading.
// This also limits the maximum header size.
// Increase this buffer if your clients send multi-KB RequestURIs
// and/or multi-KB headers (for example, BIG cookies).
ReadBufferSize int

// Per-connection buffer size for responses' writing.
WriteBufferSize int
}

// ConfigDefault is the default config
Expand Down
3 changes: 3 additions & 0 deletions middleware/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ func Balancer(config Config) fiber.Handler {
NoDefaultUserAgentHeader: true,
DisablePathNormalizing: true,
Addr: u.Host,

ReadBufferSize: config.ReadBufferSize,
WriteBufferSize: config.WriteBufferSize,
}

lbc.Clients = append(lbc.Clients, client)
Expand Down
39 changes: 39 additions & 0 deletions middleware/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io/ioutil"
"net"
"net/http/httptest"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -178,3 +179,41 @@ func Test_Proxy_Modify_Request(t *testing.T) {
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, "modified request", string(b))
}

func Test_Proxy_Buffer_Size_Response(t *testing.T) {
t.Parallel()

target := fiber.New(fiber.Config{DisableStartupMessage: true})
target.Get("/", func(c *fiber.Ctx) error {
long := strings.Join(make([]string, 5000), "-")
c.Response().Header.Set("Very-Long-Header", long)
return c.SendString("ok")
})

ln, err := net.Listen(fiber.NetworkTCP4, "127.0.0.1:0")
utils.AssertEqual(t, nil, err)

go func() {
utils.AssertEqual(t, nil, target.Listener(ln))
}()

time.Sleep(2 * time.Second)
addr := ln.Addr().String()

app := fiber.New()
app.Use(Balancer(Config{Servers: []string{addr}}))

resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, fiber.StatusInternalServerError, resp.StatusCode)

app = fiber.New()
app.Use(Balancer(Config{
Servers: []string{addr},
ReadBufferSize: 1024 * 8,
}))

resp, err = app.Test(httptest.NewRequest("GET", "/", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
}

0 comments on commit c3aafde

Please sign in to comment.