Comments (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.
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.
@valyala thanks a lot for the explanation in details
from fasthttp.
Related Issues (20)
- NewFastHTTPHandler does not set response status code. HOT 1
- NewFastHTTPHandler does not set response status code HOT 7
- Error when serving connection "x.x.x.x:443"<->"172.56.198.126:18271": EOF HOT 3
- http parsing of the request line - where does that happen? HOT 1
- gnet is going to have tls working on it very soon. was wondering if anyone has tried to port fasthttp to use gnet instead. HOT 1
- Feature request : update and show all examples for doing zero allocation when using fasthttp. 100% zero alloc for each req / resp. HOT 3
- It is not safe to read all stream body to memory without a max size limit. HOT 4
- serving a compress enabled public folder without +w permissions results in a 404 HOT 1
- no free connections available to host HOT 3
- when setting MaxConnsPerIP to value greater than zero, the TLSConnectionState( ) is null on a TLS connection returned from the worker pool. HOT 11
- invalid memory address or nil pointer dereference for firstByteReader'c HOT 2
- peeking nested query args HOT 1
- Incorrect Tag for version 1.54.0 HOT 1
- PFlag/Cobra compatibility in prefork HOT 1
- `\r` is improperly permitted in header names. HOT 1
- How to Optimize the Performance of My fasthttp Client in a Production Environment HOT 1
- `\r` and `\x00` are improperly permitted in header values HOT 1
- fasthttp.FS won't return Content-Encoding when files are too small HOT 4
- Empty header names are improperly permitted HOT 2
- How to get all custom headers of ResponseHeader and RequestHeader HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fasthttp.