ditointernet / go-dito

MIT License
6 stars 0 forks source link

[CDPSEG-177] feat: Allow for a variable ReadBufferSize in the underlying fasthttp … #53

Closed lucafmarques closed 2 years ago

lucafmarques commented 2 years ago

…server

This change allows consumers of go-dito/http to pass how many bytes of data a single request may contain, thus accommodating different service's needs.


Tested the change by creating a simple main.go at the root of this project, like so:

package main

import (
    "fmt"
    "net/url"
    "strings"

    "github.com/ditointernet/go-dito/http"
    routing "github.com/jackwhelpton/fasthttp-routing/v2"
)

func main() {
    server := http.NewServer(http.ServerInput{
        Port:           8081,
        ReadBufferSize: 20000,
        Handler: func(r *routing.Router) {
            r.Get("/ids", func(ctx *routing.Context) error {
                query, _ := url.ParseQuery(string(ctx.QueryArgs().QueryString()))
                ctx.SetBodyString(fmt.Sprint(len(strings.Split(query["ids"][0], ","))))
                return nil
            })
        },
    })

    server.Run()
}

This creates a server with a fixed, non-default (fasthttp's default is 4096 bytes), number of bytes allowed in the incoming request buffer. With that we can our server, passing a query-string ids that contains a lot of entries.

For example, using the default (omit the ReadBufferSize struct entry), the server allows close to 80 UUIDv4 ids. By changing it to ReadBufferSize: 20000, the server allows more than 500 UUIDv4 ids.