Skip to content

Commit

Permalink
feat(compress): enable config the zstd compressor for encoding response
Browse files Browse the repository at this point in the history
Signed-off-by: xkx <[email protected]>
  • Loading branch information
xkx9431 committed Nov 19, 2024
1 parent 744691e commit 2472f57
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ require (
github.com/influxdata/influxdb-observability/otel2influx v0.2.19
github.com/influxdata/influxql v1.1.1-0.20211004132434-7e7d61973256
github.com/json-iterator/go v1.1.12
github.com/klauspost/compress v1.17.11
github.com/klauspost/compress v1.17.2
github.com/mattn/go-sqlite3 v1.14.19
github.com/mitchellh/cli v1.1.5
github.com/nxadm/tail v1.4.11
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1177,8 +1177,8 @@ github.com/klauspost/compress v1.11.13/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdY
github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
github.com/klauspost/compress v1.13.5/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
github.com/klauspost/compress v1.17.2 h1:RlWWUY/Dr4fL8qk9YG7DTZ7PDgME2V4csBXA8L/ixi4=
github.com/klauspost/compress v1.17.2/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/klauspost/cpuid v0.0.0-20170728055534-ae7887de9fa5/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/klauspost/cpuid v1.2.1/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
Expand Down
31 changes: 19 additions & 12 deletions lib/util/lifted/influx/httpd/handler_compress.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2024 Huawei Cloud Computing Technologies Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package httpd

import (
Expand All @@ -23,18 +37,19 @@ type lazyCompressResponseWriter struct {
func compressFilter(inner http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var writer io.Writer = w

if shouldApplyGzip(r) {
contentEncoding := r.Header.Get("Content-Encoding")
switch {
case strings.Contains(contentEncoding, "gzip"):
gz := getGzipWriter(w)
defer gz.Close()
writer = gz
w.Header().Set("Content-Encoding", "gzip")
} else if shouldApplyZstd(r) {
case strings.Contains(contentEncoding, "zstd"):
enc := getZstdWriter(w)
defer enc.Close()
writer = enc
w.Header().Set("Content-Encoding", "zstd")
} else {
default:
inner.ServeHTTP(w, r)
return
}
Expand All @@ -55,14 +70,6 @@ func compressFilter(inner http.Handler) http.Handler {
})
}

func shouldApplyGzip(r *http.Request) bool {
return strings.Contains(r.Header.Get("Accept-Encoding"), "gzip")
}

func shouldApplyZstd(r *http.Request) bool {
return strings.Contains(r.Header.Get("Accept-Encoding"), "zstd")
}

func (w *lazyCompressResponseWriter) WriteHeader(code int) {
if !w.wroteHeader {
w.ResponseWriter.Header().Del("Content-Length")
Expand Down

0 comments on commit 2472f57

Please sign in to comment.