From 999c1a21ac76f4f27dff68e682e96fc0b325817d Mon Sep 17 00:00:00 2001 From: maskpp Date: Mon, 25 Sep 2023 23:40:20 +0800 Subject: [PATCH] Avoid repeated initialization --- node/rpcstack.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/node/rpcstack.go b/node/rpcstack.go index b33c238051..eb8b9fbb21 100644 --- a/node/rpcstack.go +++ b/node/rpcstack.go @@ -475,19 +475,18 @@ type gzipResponseWriter struct { resp http.ResponseWriter gz *gzip.Writer - contentLength uint64 // total length of the uncompressed response - written uint64 // amount of written bytes from the uncompressed response - hasLength bool // true if uncompressed response had Content-Length - inited bool // true after init was called for the first time + contentLength uint64 // total length of the uncompressed response + written uint64 // amount of written bytes from the uncompressed response + hasLength bool // true if uncompressed response had Content-Length + 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 // also decides whether compression will be applied at all. func (w *gzipResponseWriter) init() { - if w.inited { + if w.inited.CompareAndSwap(false, true) { return } - w.inited = true hdr := w.resp.Header() length := hdr.Get("content-length")