From 58f126fd27bb2f366f76f42223b93ba3b31a0bd8 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Mon, 10 Feb 2020 09:13:29 -0800 Subject: [PATCH] HEAD operation changes to fasthttp --- go.mod | 2 +- go.sum | 8 ++++++++ weed/operation/assign_file_id.go | 20 ++++++++++++++------ weed/util/http_util.go | 25 +++++++++++++++++-------- weed/util/http_util_test.go | 19 +++++++++++++++++++ 5 files changed, 59 insertions(+), 15 deletions(-) create mode 100644 weed/util/http_util_test.go diff --git a/go.mod b/go.mod index 48879fd8c..0bf76980e 100644 --- a/go.mod +++ b/go.mod @@ -36,7 +36,6 @@ require ( github.com/jcmturner/gofork v1.0.0 // indirect github.com/karlseguin/ccache v2.0.3+incompatible github.com/karlseguin/expect v1.0.1 // indirect - github.com/klauspost/cpuid v1.2.1 // indirect github.com/klauspost/crc32 v1.2.0 github.com/klauspost/reedsolomon v1.9.2 github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect @@ -76,6 +75,7 @@ require ( github.com/uber-go/atomic v1.4.0 // indirect github.com/uber/jaeger-client-go v2.17.0+incompatible // indirect github.com/uber/jaeger-lib v2.0.0+incompatible // indirect + github.com/valyala/fasthttp v1.9.0 github.com/willf/bitset v1.1.10 // indirect github.com/willf/bloom v2.0.3+incompatible github.com/wsxiaoys/terminal v0.0.0-20160513160801-0940f3fc43a0 // indirect diff --git a/go.sum b/go.sum index d16280568..132119abd 100644 --- a/go.sum +++ b/go.sum @@ -282,6 +282,8 @@ github.com/karlseguin/expect v1.0.1/go.mod h1:zNBxMY8P21owkeogJELCLeHIt+voOSduHY github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/klauspost/compress v1.8.2 h1:Bx0qjetmNjdFXASH02NSAREKpiaDwkO1DRZ3dV2KCcs= +github.com/klauspost/compress v1.8.2/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= github.com/klauspost/cpuid v0.0.0-20170728055534-ae7887de9fa5/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek= github.com/klauspost/cpuid v1.2.1 h1:vJi+O/nMdFt0vqm8NZBI6wzALWdA2X+egi0ogNyrC/w= github.com/klauspost/cpuid v1.2.1/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek= @@ -545,6 +547,11 @@ github.com/unrolled/render v0.0.0-20180914162206-b9786414de4d h1:ggUgChAeyge4NZ4 github.com/unrolled/render v0.0.0-20180914162206-b9786414de4d/go.mod h1:tu82oB5W2ykJRVioYsB+IQKcft7ryBr7w12qMBUPyXg= github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/urfave/negroni v0.3.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasthttp v1.9.0 h1:hNpmUdy/+ZXYpGy0OBfm7K0UQTzb73W0T0U4iJIVrMw= +github.com/valyala/fasthttp v1.9.0/go.mod h1:FstJa9V+Pj9vQ7OJie2qMHdwemEDaDiSdBnvPM1Su9w= +github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio= github.com/willf/bitset v1.1.10 h1:NotGKqX0KwQ72NUzqrjZq5ipPNDQex9lo3WpaS8L2sc= github.com/willf/bitset v1.1.10/go.mod h1:RjeCKbqT1RxIR/KWY6phxZiaY1IyutSBfGjNPySAYV4= github.com/willf/bloom v2.0.3+incompatible h1:QDacWdqcAUI1MPOwIQZRy9kOR7yxfyEmxX8Wdm2/JPA= @@ -626,6 +633,7 @@ golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190619014844-b5b0513f8c1b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190909003024-a7b16738d86b h1:XfVGCX+0T4WOStkaOsJRllbsiImhB2jgVBGc9L0lPGc= golang.org/x/net v0.0.0-20190909003024-a7b16738d86b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= diff --git a/weed/operation/assign_file_id.go b/weed/operation/assign_file_id.go index b67d8b708..3f3bb13e0 100644 --- a/weed/operation/assign_file_id.go +++ b/weed/operation/assign_file_id.go @@ -1,13 +1,17 @@ package operation import ( + "bytes" "context" "fmt" + + "github.com/valyala/fasthttp" + "google.golang.org/grpc" + + "github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/pb/master_pb" "github.com/chrislusf/seaweedfs/weed/security" "github.com/chrislusf/seaweedfs/weed/util" - "google.golang.org/grpc" - "strings" ) type VolumeAssignRequest struct { @@ -89,12 +93,16 @@ func Assign(server string, grpcDialOption grpc.DialOption, primaryRequest *Volum func LookupJwt(master string, fileId string) security.EncodedJwt { tokenStr := "" + lookupUrl := fmt.Sprintf("http://%s/dir/lookup?fileId=%s", master, fileId) - if h, e := util.Head(fmt.Sprintf("http://%s/dir/lookup?fileId=%s", master, fileId)); e == nil { - bearer := h.Get("Authorization") - if len(bearer) > 7 && strings.ToUpper(bearer[0:6]) == "BEARER" { - tokenStr = bearer[7:] + err := util.Head(lookupUrl, func(header fasthttp.ResponseHeader) { + bearer := header.Peek("Authorization") + if len(bearer) > 7 && string(bytes.ToUpper(bearer[0:6])) == "BEARER" { + tokenStr = string(bearer[7:]) } + }) + if err != nil { + glog.V(0).Infof("failed to lookup jwt %s: %v", lookupUrl, err) } return security.EncodedJwt(tokenStr) diff --git a/weed/util/http_util.go b/weed/util/http_util.go index 08007a038..b74e30ad7 100644 --- a/weed/util/http_util.go +++ b/weed/util/http_util.go @@ -12,6 +12,8 @@ import ( "net/url" "strings" + "github.com/valyala/fasthttp" + "github.com/chrislusf/seaweedfs/weed/glog" ) @@ -83,16 +85,23 @@ func Get(url string) ([]byte, error) { return b, nil } -func Head(url string) (http.Header, error) { - r, err := client.Head(url) - if err != nil { - return nil, err +func Head(url string, fn func(header fasthttp.ResponseHeader)) error { + req := fasthttp.AcquireRequest() + resp := fasthttp.AcquireResponse() + defer fasthttp.ReleaseRequest(req) // <- do not forget to release + defer fasthttp.ReleaseResponse(resp) // <- do not forget to release + + c := fasthttp.Client{} + req.SetRequestURI(url) + req.Header.SetMethod(fasthttp.MethodHead) + if err := c.Do(req, resp); err != nil { + return err } - defer r.Body.Close() - if r.StatusCode >= 400 { - return nil, fmt.Errorf("%s: %s", url, r.Status) + if resp.StatusCode() >= 400 { + return fmt.Errorf("%s: %d", url, resp.StatusCode()) } - return r.Header, nil + fn(resp.Header) + return nil } func Delete(url string, jwt string) error { diff --git a/weed/util/http_util_test.go b/weed/util/http_util_test.go new file mode 100644 index 000000000..a8a1172d2 --- /dev/null +++ b/weed/util/http_util_test.go @@ -0,0 +1,19 @@ +package util + +import ( + "testing" + + "github.com/valyala/fasthttp" +) + +func TestFasthttpClientHead(t *testing.T) { + err := Head("https://www.google.com", func(header fasthttp.ResponseHeader) { + header.VisitAll(func(key, value []byte) { + println(string(key) + ": " + string(value)) + }) + }) + if err != nil { + println(err.Error()) + } + +}