Skip to content

Commit

Permalink
refactor: do not return error as it is always nil (#1759)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear authored Apr 21, 2024
1 parent d3aa5a1 commit e88bd48
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 33 deletions.
44 changes: 18 additions & 26 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -1665,9 +1665,7 @@ func (resp *Response) WriteGzip(w *bufio.Writer) error {
//
// WriteGzipLevel doesn't flush response to w for performance reasons.
func (resp *Response) WriteGzipLevel(w *bufio.Writer, level int) error {
if err := resp.gzipBody(level); err != nil {
return err
}
resp.gzipBody(level)
return resp.Write(w)
}

Expand Down Expand Up @@ -1696,22 +1694,20 @@ func (resp *Response) WriteDeflate(w *bufio.Writer) error {
//
// WriteDeflateLevel doesn't flush response to w for performance reasons.
func (resp *Response) WriteDeflateLevel(w *bufio.Writer, level int) error {
if err := resp.deflateBody(level); err != nil {
return err
}
resp.deflateBody(level)
return resp.Write(w)
}

func (resp *Response) brotliBody(level int) error {
func (resp *Response) brotliBody(level int) {
if len(resp.Header.ContentEncoding()) > 0 {
// It looks like the body is already compressed.
// Do not compress it again.
return nil
return
}

if !resp.Header.isCompressibleContentType() {
// The content-type cannot be compressed.
return nil
return
}

if resp.bodyStream != nil {
Expand Down Expand Up @@ -1741,7 +1737,7 @@ func (resp *Response) brotliBody(level int) error {
// There is no sense in spending CPU time on small body compression,
// since there is a very high probability that the compressed
// body size will be bigger than the original body size.
return nil
return
}
w := responseBodyPool.Get()
w.B = AppendBrotliBytesLevel(w.B, bodyBytes, level)
Expand All @@ -1755,19 +1751,18 @@ func (resp *Response) brotliBody(level int) error {
}
resp.Header.SetContentEncodingBytes(strBr)
resp.Header.addVaryBytes(strAcceptEncoding)
return nil
}

func (resp *Response) gzipBody(level int) error {
func (resp *Response) gzipBody(level int) {
if len(resp.Header.ContentEncoding()) > 0 {
// It looks like the body is already compressed.
// Do not compress it again.
return nil
return
}

if !resp.Header.isCompressibleContentType() {
// The content-type cannot be compressed.
return nil
return
}

if resp.bodyStream != nil {
Expand Down Expand Up @@ -1797,7 +1792,7 @@ func (resp *Response) gzipBody(level int) error {
// There is no sense in spending CPU time on small body compression,
// since there is a very high probability that the compressed
// body size will be bigger than the original body size.
return nil
return
}
w := responseBodyPool.Get()
w.B = AppendGzipBytesLevel(w.B, bodyBytes, level)
Expand All @@ -1811,19 +1806,18 @@ func (resp *Response) gzipBody(level int) error {
}
resp.Header.SetContentEncodingBytes(strGzip)
resp.Header.addVaryBytes(strAcceptEncoding)
return nil
}

func (resp *Response) deflateBody(level int) error {
func (resp *Response) deflateBody(level int) {
if len(resp.Header.ContentEncoding()) > 0 {
// It looks like the body is already compressed.
// Do not compress it again.
return nil
return
}

if !resp.Header.isCompressibleContentType() {
// The content-type cannot be compressed.
return nil
return
}

if resp.bodyStream != nil {
Expand Down Expand Up @@ -1853,7 +1847,7 @@ func (resp *Response) deflateBody(level int) error {
// There is no sense in spending CPU time on small body compression,
// since there is a very high probability that the compressed
// body size will be bigger than the original body size.
return nil
return
}
w := responseBodyPool.Get()
w.B = AppendDeflateBytesLevel(w.B, bodyBytes, level)
Expand All @@ -1867,16 +1861,15 @@ func (resp *Response) deflateBody(level int) error {
}
resp.Header.SetContentEncodingBytes(strDeflate)
resp.Header.addVaryBytes(strAcceptEncoding)
return nil
}

func (resp *Response) zstdBody(level int) error {
func (resp *Response) zstdBody(level int) {
if len(resp.Header.ContentEncoding()) > 0 {
return nil
return
}

if !resp.Header.isCompressibleContentType() {
return nil
return
}

if resp.bodyStream != nil {
Expand All @@ -1903,7 +1896,7 @@ func (resp *Response) zstdBody(level int) error {
} else {
bodyBytes := resp.bodyBytes()
if len(bodyBytes) < minCompressLen {
return nil
return
}
w := responseBodyPool.Get()
w.B = AppendZstdBytesLevel(w.B, bodyBytes, level)
Expand All @@ -1916,7 +1909,6 @@ func (resp *Response) zstdBody(level int) error {
}
resp.Header.SetContentEncodingBytes(strZstd)
resp.Header.addVaryBytes(strAcceptEncoding)
return nil
}

// Bodies with sizes smaller than minCompressLen aren't compressed at all.
Expand Down
14 changes: 7 additions & 7 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,11 +525,11 @@ func CompressHandlerLevel(h RequestHandler, level int) RequestHandler {
h(ctx)
switch {
case ctx.Request.Header.HasAcceptEncodingBytes(strGzip):
ctx.Response.gzipBody(level) //nolint:errcheck
ctx.Response.gzipBody(level)
case ctx.Request.Header.HasAcceptEncodingBytes(strDeflate):
ctx.Response.deflateBody(level) //nolint:errcheck
ctx.Response.deflateBody(level)
case ctx.Request.Header.HasAcceptEncodingBytes(strZstd):
ctx.Response.zstdBody(level) //nolint:errcheck
ctx.Response.zstdBody(level)
}
}
}
Expand Down Expand Up @@ -557,13 +557,13 @@ func CompressHandlerBrotliLevel(h RequestHandler, brotliLevel, otherLevel int) R
h(ctx)
switch {
case ctx.Request.Header.HasAcceptEncodingBytes(strBr):
ctx.Response.brotliBody(brotliLevel) //nolint:errcheck
ctx.Response.brotliBody(brotliLevel)
case ctx.Request.Header.HasAcceptEncodingBytes(strGzip):
ctx.Response.gzipBody(otherLevel) //nolint:errcheck
ctx.Response.gzipBody(otherLevel)
case ctx.Request.Header.HasAcceptEncodingBytes(strDeflate):
ctx.Response.deflateBody(otherLevel) //nolint:errcheck
ctx.Response.deflateBody(otherLevel)
case ctx.Request.Header.HasAcceptEncodingBytes(strZstd):
ctx.Response.zstdBody(otherLevel) //nolint:errcheck
ctx.Response.zstdBody(otherLevel)
}
}
}
Expand Down

0 comments on commit e88bd48

Please sign in to comment.