Avoid repeated initialization

This commit is contained in:
maskpp 2023-09-25 23:40:20 +08:00
parent 82ec555d70
commit 999c1a21ac

View file

@ -478,16 +478,15 @@ type gzipResponseWriter struct {
contentLength uint64 // total length of the uncompressed response contentLength uint64 // total length of the uncompressed response
written uint64 // amount of written bytes from the uncompressed response written uint64 // amount of written bytes from the uncompressed response
hasLength bool // true if uncompressed response had Content-Length hasLength bool // true if uncompressed response had Content-Length
inited bool // true after init was called for the first time inited atomic.Bool // true after init was called for the first time
} }
// init runs just before response headers are written. Among other things, this function // init runs just before response headers are written. Among other things, this function
// also decides whether compression will be applied at all. // also decides whether compression will be applied at all.
func (w *gzipResponseWriter) init() { func (w *gzipResponseWriter) init() {
if w.inited { if w.inited.CompareAndSwap(false, true) {
return return
} }
w.inited = true
hdr := w.resp.Header() hdr := w.resp.Header()
length := hdr.Get("content-length") length := hdr.Get("content-length")