Merge pull request #622 from kiyonlin/fix-fs-data-race

👷 fix data race in sendFile test
pull/624/head
fenny 2020-07-15 21:08:23 -04:00 committed by GitHub
commit 9dc7890f4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 2 deletions

6
ctx.go
View File

@ -876,6 +876,7 @@ func (ctx *Ctx) SendBytes(body []byte) {
ctx.Fasthttp.Response.SetBodyRaw(body)
}
var fsOnce sync.Once
var sendFileFS *fasthttp.FS
var sendFileHandler fasthttp.RequestHandler
@ -884,7 +885,7 @@ var sendFileHandler fasthttp.RequestHandler
// Sets the Content-Type response HTTP header field based on the filenames extension.
func (ctx *Ctx) SendFile(file string, compress ...bool) error {
// https://github.com/valyala/fasthttp/blob/master/fs.go#L81
if sendFileFS == nil {
fsOnce.Do(func() {
sendFileFS = &fasthttp.FS{
Root: "/",
GenerateIndexPages: false,
@ -898,7 +899,8 @@ func (ctx *Ctx) SendFile(file string, compress ...bool) error {
},
}
sendFileHandler = sendFileFS.NewRequestHandler()
}
})
// Keep original path for mutable params
ctx.pathOriginal = utils.ImmutableString(ctx.pathOriginal)
// Disable compression