Giter VIP home page Giter VIP logo

Comments (3)

valyala avatar valyala commented on July 25, 2024 3

Short story: fasthttp works faster than nginx on small files (up to 8Kb on my laptop). When serving big files, its' performance it about 80% comparing to nginx performance, because go's sendfile code path isn't optimized yet comparing to nginx.

Below are random remarks regarding the provided code:

The following line is not necessary, since Go automatically sets GOMAXPROCS to runtime.NumCPU() starting from go 1.5:

    runtime.GOMAXPROCS(runtime.NumCPU())

The following line may panic, since it assumes that the requested path contains at least four parts delimited by '/' . This is not true for arbitrary user input:

      return []byte("/" + urlPart[2] + "/" + urlPart[3])

This line also makes at least two memory allocations - the first one for string concatenation and the second one for string to []byte conversion. This may degrade performance a bit.

An additional memory allocation is made on the line for []string slice returned from strings.Split:

    urlPart := strings.Split(string(ctx.Path()), "/")

I'd recommend rewriting rw in the following way:

rw := func(ctx *fasthttp.RequestCtx) []byte {
        path := ctx.Path()

        // fasthttp guarantees that the path starts with '/'
        n := bytes.IndexByte(path[1:], '/')
        if n < 0 {
            return []byte("/invalid-path")
        }
        return path[n:]
}

The following line may be rewritten:

        CacheDuration:   time.Duration(1) * time.Hour,

into

        CacheDuration:   time.Hour,

Panic may occur on the following line due to the same reason mentioned above:

        fileName := urlPart[4]

I'd recommend using the following code instead:

        fileName := ctx.URI().LastPathSegment()

The following expression may be shortened:

ctx.IsGet() == true || ctx.IsHead() == true

into

ctx.IsGet() || ctx.IsHead()

As for Content-Disposition: attachment response header, it looks like it would be great adding the corresponding config parameter to FS (for example, FS.ServeAttachments), so fasthttp could automatically adding the header when the parameter is set.

from fasthttp.

valyala avatar valyala commented on July 25, 2024 1

Just noticed that I recommended non-equivalent path rewriter function. Your function strips the trailing part of the path. So the code must be:

rw := func(ctx *fasthttp.RequestCtx) []byte {
        // fasthttp guarantees that the path starts with '/'
        path := ctx.Path()[1:]

        n := bytes.IndexByte(path, '/')
        if n < 0 {
            return []byte("/invalid-path")
        }
        nn := bytes.LastIndexByte(path, '/')
        return path[n:nn]
}

I didn't test it, so it may contain bugs. But I hope you caught the idea.

from fasthttp.

iowelinux avatar iowelinux commented on July 25, 2024

@valyala thanks a lot for the explanation in details

from fasthttp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.