export httpx.GetRemoteAddr

master
kevin 4 years ago
parent b541403ce2
commit 80d55dbc02

@ -8,7 +8,7 @@ import (
"github.com/tal-tech/go-zero/core/breaker" "github.com/tal-tech/go-zero/core/breaker"
"github.com/tal-tech/go-zero/core/logx" "github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/core/stat" "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" "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 { if err != nil {
metrics.AddDrop() metrics.AddDrop()
logx.Errorf("[http] dropped, %s - %s - %s", 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) w.WriteHeader(http.StatusServiceUnavailable)
return return
} }

@ -13,6 +13,7 @@ import (
"github.com/tal-tech/go-zero/core/logx" "github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/core/timex" "github.com/tal-tech/go-zero/core/timex"
"github.com/tal-tech/go-zero/core/utils" "github.com/tal-tech/go-zero/core/utils"
"github.com/tal-tech/go-zero/rest/httpx"
"github.com/tal-tech/go-zero/rest/internal" "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 var buf bytes.Buffer
duration := timer.Duration() duration := timer.Duration()
buf.WriteString(fmt.Sprintf("%d - %s - %s - %s - %s", 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 { if duration > slowThreshold {
logx.Slowf("[HTTP] %d - %s - %s - %s - slowcall(%s)", 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) ok := isOkResponse(code)

@ -7,7 +7,7 @@ import (
"github.com/tal-tech/go-zero/core/load" "github.com/tal-tech/go-zero/core/load"
"github.com/tal-tech/go-zero/core/logx" "github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/core/stat" "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" "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() metrics.AddDrop()
sheddingStat.IncrementDrop() sheddingStat.IncrementDrop()
logx.Errorf("[http] dropped, %s - %s - %s", 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) w.WriteHeader(http.StatusServiceUnavailable)
return return
} }

@ -1,4 +1,4 @@
package internal package httpx
import "net/http" import "net/http"

@ -1,4 +1,4 @@
package internal package httpx
import ( import (
"net/http" "net/http"
@ -16,4 +16,3 @@ func TestGetRemoteAddr(t *testing.T) {
r.Header.Set(xForwardFor, host) r.Header.Set(xForwardFor, host)
assert.Equal(t, host, GetRemoteAddr(r)) assert.Equal(t, host, GetRemoteAddr(r))
} }

@ -7,6 +7,7 @@ import (
"sync" "sync"
"github.com/tal-tech/go-zero/core/logx" "github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/rest/httpx"
) )
const LogContext = "request_logs" 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 { 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)
} }

Loading…
Cancel
Save