From 9428fface2dccc882c0018457007acdb609a3673 Mon Sep 17 00:00:00 2001 From: kevin Date: Wed, 12 Aug 2020 12:25:52 +0800 Subject: [PATCH] export httpx.GetRemoteAddr --- rest/handler/breakerhandler.go | 4 ++-- rest/handler/loghandler.go | 5 +++-- rest/handler/sheddinghandler.go | 4 ++-- rest/{internal => httpx}/util.go | 2 +- rest/{internal => httpx}/util_test.go | 3 +-- rest/internal/log.go | 3 ++- 6 files changed, 11 insertions(+), 10 deletions(-) rename rest/{internal => httpx}/util.go (93%) rename rest/{internal => httpx}/util_test.go (94%) diff --git a/rest/handler/breakerhandler.go b/rest/handler/breakerhandler.go index 6e10a56d..846e68be 100644 --- a/rest/handler/breakerhandler.go +++ b/rest/handler/breakerhandler.go @@ -8,7 +8,7 @@ import ( "github.com/tal-tech/go-zero/core/breaker" "github.com/tal-tech/go-zero/core/logx" "github.com/tal-tech/go-zero/core/stat" - "github.com/tal-tech/go-zero/rest/internal" + "github.com/tal-tech/go-zero/rest/httpx" "github.com/tal-tech/go-zero/rest/internal/security" ) @@ -22,7 +22,7 @@ func BreakerHandler(method, path string, metrics *stat.Metrics) func(http.Handle if err != nil { metrics.AddDrop() logx.Errorf("[http] dropped, %s - %s - %s", - r.RequestURI, internal.GetRemoteAddr(r), r.UserAgent()) + r.RequestURI, httpx.GetRemoteAddr(r), r.UserAgent()) w.WriteHeader(http.StatusServiceUnavailable) return } diff --git a/rest/handler/loghandler.go b/rest/handler/loghandler.go index 577c1c56..96157215 100644 --- a/rest/handler/loghandler.go +++ b/rest/handler/loghandler.go @@ -13,6 +13,7 @@ import ( "github.com/tal-tech/go-zero/core/logx" "github.com/tal-tech/go-zero/core/timex" "github.com/tal-tech/go-zero/core/utils" + "github.com/tal-tech/go-zero/rest/httpx" "github.com/tal-tech/go-zero/rest/internal" ) @@ -112,10 +113,10 @@ func logBrief(r *http.Request, code int, timer *utils.ElapsedTimer, logs *intern var buf bytes.Buffer duration := timer.Duration() buf.WriteString(fmt.Sprintf("%d - %s - %s - %s - %s", - code, r.RequestURI, internal.GetRemoteAddr(r), r.UserAgent(), timex.ReprOfDuration(duration))) + code, r.RequestURI, httpx.GetRemoteAddr(r), r.UserAgent(), timex.ReprOfDuration(duration))) if duration > slowThreshold { logx.Slowf("[HTTP] %d - %s - %s - %s - slowcall(%s)", - code, r.RequestURI, internal.GetRemoteAddr(r), r.UserAgent(), timex.ReprOfDuration(duration)) + code, r.RequestURI, httpx.GetRemoteAddr(r), r.UserAgent(), timex.ReprOfDuration(duration)) } ok := isOkResponse(code) diff --git a/rest/handler/sheddinghandler.go b/rest/handler/sheddinghandler.go index fa70f797..568e7887 100644 --- a/rest/handler/sheddinghandler.go +++ b/rest/handler/sheddinghandler.go @@ -7,7 +7,7 @@ import ( "github.com/tal-tech/go-zero/core/load" "github.com/tal-tech/go-zero/core/logx" "github.com/tal-tech/go-zero/core/stat" - "github.com/tal-tech/go-zero/rest/internal" + "github.com/tal-tech/go-zero/rest/httpx" "github.com/tal-tech/go-zero/rest/internal/security" ) @@ -35,7 +35,7 @@ func SheddingHandler(shedder load.Shedder, metrics *stat.Metrics) func(http.Hand metrics.AddDrop() sheddingStat.IncrementDrop() logx.Errorf("[http] dropped, %s - %s - %s", - r.RequestURI, internal.GetRemoteAddr(r), r.UserAgent()) + r.RequestURI, httpx.GetRemoteAddr(r), r.UserAgent()) w.WriteHeader(http.StatusServiceUnavailable) return } diff --git a/rest/internal/util.go b/rest/httpx/util.go similarity index 93% rename from rest/internal/util.go rename to rest/httpx/util.go index 5e7c2e2b..bdf6eaa8 100644 --- a/rest/internal/util.go +++ b/rest/httpx/util.go @@ -1,4 +1,4 @@ -package internal +package httpx import "net/http" diff --git a/rest/internal/util_test.go b/rest/httpx/util_test.go similarity index 94% rename from rest/internal/util_test.go rename to rest/httpx/util_test.go index 96594454..bcd9c416 100644 --- a/rest/internal/util_test.go +++ b/rest/httpx/util_test.go @@ -1,4 +1,4 @@ -package internal +package httpx import ( "net/http" @@ -16,4 +16,3 @@ func TestGetRemoteAddr(t *testing.T) { r.Header.Set(xForwardFor, host) assert.Equal(t, host, GetRemoteAddr(r)) } - diff --git a/rest/internal/log.go b/rest/internal/log.go index 78013835..80b19355 100644 --- a/rest/internal/log.go +++ b/rest/internal/log.go @@ -7,6 +7,7 @@ import ( "sync" "github.com/tal-tech/go-zero/core/logx" + "github.com/tal-tech/go-zero/rest/httpx" ) const LogContext = "request_logs" @@ -79,5 +80,5 @@ func formatf(r *http.Request, format string, v ...interface{}) string { } func formatWithReq(r *http.Request, v string) string { - return fmt.Sprintf("(%s - %s) %s", r.RequestURI, GetRemoteAddr(r), v) + return fmt.Sprintf("(%s - %s) %s", r.RequestURI, httpx.GetRemoteAddr(r), v) }