Skip to content

Commit

Permalink
optimization: replacing commonly used HTTP strings with enumerations. (
Browse files Browse the repository at this point in the history
  • Loading branch information
yojay11717 authored May 18, 2023
1 parent 3d537c9 commit 505c27a
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 12 deletions.
16 changes: 16 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,19 @@ func IsNil(i interface{}) bool {
}
return false
}

const (
// HttpHeaderContentType HTTP request header keyword: Content-Type which is used in HTTP request and response
// headers to specify the media type of the entity body
HttpHeaderContentType = "Content-Type"
// HttpHeaderContentLength HTTP request header keyword: Content-Length which is used to indicate the size of the
// message body, ensuring that the message can be transmitted and parsed correctly
HttpHeaderContentLength = "Content-Length"
// HttpHeaderTransferEncoding HTTP request header keyword: Transfer-Encoding which is used to indicate the HTTP
// transmission encoding type used by the server
HttpHeaderTransferEncoding = "Transfer-Encoding"

// HttpContentTypeJson HTTP request Content-Type type: application/json which is used to indicate that the data
// type transmitted in the HTTP request and response body is JSON
HttpContentTypeJson = "application/json"
)
3 changes: 2 additions & 1 deletion pkg/yurthub/otaupdate/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"k8s.io/klog/v2"

"github.com/openyurtio/openyurt/pkg/controller/daemonpodupdater"
yurtutil "github.com/openyurtio/openyurt/pkg/util"
)

// Derived from kubelet encodePods
Expand All @@ -52,7 +53,7 @@ func WriteJSONResponse(w http.ResponseWriter, data []byte) {
w.WriteHeader(http.StatusOK)
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set(yurtutil.HttpHeaderContentType, yurtutil.HttpContentTypeJson)
w.WriteHeader(http.StatusOK)
n, err := w.Write(data)
if err != nil || n != len(data) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/yurthub/proxy/local/faketoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
apirequest "k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/klog/v2"

yurtutil "github.com/openyurtio/openyurt/pkg/util"
"github.com/openyurtio/openyurt/pkg/yurthub/kubernetes/serializer"
"github.com/openyurtio/openyurt/pkg/yurthub/util"
)
Expand All @@ -43,7 +44,7 @@ func WithFakeTokenInject(handler http.Handler, serializerManager *serializer.Ser
if info.Resource == "serviceaccounts" && info.Subresource == "token" {
klog.Infof("find serviceaccounts token request when cluster is unhealthy, try to write fake token to response.")
var buf bytes.Buffer
headerNStr := req.Header.Get("Content-Length")
headerNStr := req.Header.Get(yurtutil.HttpHeaderContentLength)
headerN, _ := strconv.Atoi(headerNStr)
n, err := buf.ReadFrom(req.Body)
if err != nil || (headerN != 0 && int(n) != headerN) {
Expand Down
9 changes: 5 additions & 4 deletions pkg/yurthub/proxy/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
apirequest "k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/klog/v2"

yurtutil "github.com/openyurtio/openyurt/pkg/util"
manager "github.com/openyurtio/openyurt/pkg/yurthub/cachemanager"
hubmeta "github.com/openyurtio/openyurt/pkg/yurthub/kubernetes/meta"
"github.com/openyurtio/openyurt/pkg/yurthub/proxy/util"
Expand Down Expand Up @@ -132,7 +133,7 @@ func (lp *LocalProxy) localPost(w http.ResponseWriter, req *http.Request) error
req.Body = rc
}

headerNStr := req.Header.Get("Content-Length")
headerNStr := req.Header.Get(yurtutil.HttpHeaderContentLength)
headerN, _ := strconv.Atoi(headerNStr)
n, err := buf.ReadFrom(req.Body)
if err != nil || (headerN != 0 && int(n) != headerN) {
Expand Down Expand Up @@ -171,8 +172,8 @@ func (lp *LocalProxy) localWatch(w http.ResponseWriter, req *http.Request) error

ctx := req.Context()
contentType, _ := hubutil.ReqContentTypeFrom(ctx)
w.Header().Set("Content-Type", contentType)
w.Header().Set("Transfer-Encoding", "chunked")
w.Header().Set(yurtutil.HttpHeaderContentType, contentType)
w.Header().Set(yurtutil.HttpHeaderTransferEncoding, "chunked")
w.WriteHeader(http.StatusOK)
flusher.Flush()

Expand Down Expand Up @@ -236,7 +237,7 @@ func (lp *LocalProxy) localReqCache(w http.ResponseWriter, req *http.Request) er

func copyHeader(dst, src http.Header) {
for k, vv := range src {
if k == "Content-Type" || k == "Content-Length" {
if k == yurtutil.HttpHeaderContentType || k == yurtutil.HttpHeaderContentLength {
for _, v := range vv {
dst.Add(k, v)
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/yurthub/proxy/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
apirequest "k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/klog/v2"

yurtutil "github.com/openyurtio/openyurt/pkg/util"
"github.com/openyurtio/openyurt/pkg/yurthub/cachemanager"
"github.com/openyurtio/openyurt/pkg/yurthub/filter/manager"
"github.com/openyurtio/openyurt/pkg/yurthub/proxy/util"
Expand Down Expand Up @@ -224,7 +225,7 @@ func (pp *PoolCoordinatorProxy) modifyResponse(resp *http.Response) error {
if resp.StatusCode >= http.StatusOK && resp.StatusCode <= http.StatusPartialContent {
// prepare response content type
reqContentType, _ := hubutil.ReqContentTypeFrom(ctx)
respContentType := resp.Header.Get("Content-Type")
respContentType := resp.Header.Get(yurtutil.HttpHeaderContentType)
if len(respContentType) == 0 {
respContentType = reqContentType
}
Expand All @@ -243,7 +244,7 @@ func (pp *PoolCoordinatorProxy) modifyResponse(resp *http.Response) error {
resp.Body = filterRc
if size > 0 {
resp.ContentLength = int64(size)
resp.Header.Set("Content-Length", fmt.Sprint(size))
resp.Header.Set(yurtutil.HttpHeaderContentLength, fmt.Sprint(size))
}

// after gunzip in filter, the header content encoding should be removed.
Expand Down
5 changes: 3 additions & 2 deletions pkg/yurthub/proxy/remote/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
apirequest "k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/klog/v2"

yurtutil "github.com/openyurtio/openyurt/pkg/util"
"github.com/openyurtio/openyurt/pkg/yurthub/cachemanager"
"github.com/openyurtio/openyurt/pkg/yurthub/filter/manager"
"github.com/openyurtio/openyurt/pkg/yurthub/healthchecker"
Expand Down Expand Up @@ -278,7 +279,7 @@ func (lb *loadBalancer) modifyResponse(resp *http.Response) error {
if resp.StatusCode >= http.StatusOK && resp.StatusCode <= http.StatusPartialContent {
// prepare response content type
reqContentType, _ := hubutil.ReqContentTypeFrom(ctx)
respContentType := resp.Header.Get("Content-Type")
respContentType := resp.Header.Get(yurtutil.HttpHeaderContentType)
if len(respContentType) == 0 {
respContentType = reqContentType
}
Expand All @@ -297,7 +298,7 @@ func (lb *loadBalancer) modifyResponse(resp *http.Response) error {
resp.Body = filterRc
if size > 0 {
resp.ContentLength = int64(size)
resp.Header.Set("Content-Length", fmt.Sprint(size))
resp.Header.Set(yurtutil.HttpHeaderContentLength, fmt.Sprint(size))
}

// after gunzip in filter, the header content encoding should be removed.
Expand Down
3 changes: 2 additions & 1 deletion pkg/yurthub/server/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"net/http"

yurtutil "github.com/openyurtio/openyurt/pkg/util"
"github.com/openyurtio/openyurt/pkg/yurthub/certificate"
)

Expand Down Expand Up @@ -56,7 +57,7 @@ func updateTokenHandler(certificateMgr certificate.YurtCertificateManager) http.
}

w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
w.Header().Set(yurtutil.HttpHeaderContentType, yurtutil.HttpContentTypeJson)
fmt.Fprintf(w, "update bootstrap token successfully")
return
})
Expand Down
3 changes: 2 additions & 1 deletion pkg/yurthub/server/nonresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"k8s.io/utils/pointer"

"github.com/openyurtio/openyurt/cmd/yurthub/app/config"
yurtutil "github.com/openyurtio/openyurt/pkg/util"
"github.com/openyurtio/openyurt/pkg/yurthub/cachemanager"
"github.com/openyurtio/openyurt/pkg/yurthub/kubernetes/rest"
"github.com/openyurtio/openyurt/pkg/yurthub/storage"
Expand Down Expand Up @@ -121,6 +122,6 @@ func writeErrResponse(path string, err error, w http.ResponseWriter) {
}

func writeRawJSON(output []byte, w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set(yurtutil.HttpHeaderContentType, yurtutil.HttpContentTypeJson)
w.Write(output)
}

0 comments on commit 505c27a

Please sign in to comment.