unify metadata header key

pull/112/head
realityone 6 years ago
parent b59ec79e5c
commit 0edc2de268
No known key found for this signature in database
GPG Key ID: ABD4DB50620408C3
  1. 3
      pkg/net/http/blademaster/client.go
  2. 88
      pkg/net/http/blademaster/metadata.go
  3. 11
      pkg/net/http/blademaster/server.go
  4. 4
      pkg/net/http/blademaster/server_test.go

@ -253,9 +253,6 @@ func (client *Client) Raw(c context.Context, req *xhttp.Request, v ...string) (b
setTimeout(req, timeout) setTimeout(req, timeout)
req = req.WithContext(c) req = req.WithContext(c)
setCaller(req) setCaller(req)
if color := metadata.String(c, metadata.Color); color != "" {
setColor(req, color)
}
metadata.Range(c, metadata.Range(c,
func(key string, value interface{}) { func(key string, value interface{}) {
setMetadata(req, key, value) setMetadata(req, key, value)

@ -9,7 +9,7 @@ import (
"github.com/bilibili/kratos/pkg/conf/env" "github.com/bilibili/kratos/pkg/conf/env"
"github.com/bilibili/kratos/pkg/log" "github.com/bilibili/kratos/pkg/log"
criticalityPkg "github.com/bilibili/kratos/pkg/net/criticality" "github.com/bilibili/kratos/pkg/net/criticality"
"github.com/bilibili/kratos/pkg/net/metadata" "github.com/bilibili/kratos/pkg/net/metadata"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -18,22 +18,50 @@ import (
const ( const (
// http head // http head
_httpHeaderUser = "x1-bmspy-user" _httpHeaderUser = "x1-bmspy-user"
_httpHeaderColor = "x1-bmspy-color"
_httpHeaderTimeout = "x1-bmspy-timeout" _httpHeaderTimeout = "x1-bmspy-timeout"
_httpHeaderMirror = "x1-bmspy-mirror"
_httpHeaderRemoteIP = "x-backend-bm-real-ip" _httpHeaderRemoteIP = "x-backend-bm-real-ip"
_httpHeaderRemoteIPPort = "x-backend-bm-real-ipport" _httpHeaderRemoteIPPort = "x-backend-bm-real-ipport"
_httpHeaderCriticality = "x-backend-bili-criticality"
) )
const ( const (
_httpHeaderMetadata = "x-bili-metadata-" _httpHeaderMetadata = "x-bm-metadata-"
) )
var _outgoingHeader = map[string]string{ var _parser = map[string]func(string) interface{}{
metadata.Color: _httpHeaderColor, "mirror": func(mirrorStr string) interface{} {
metadata.Criticality: _httpHeaderCriticality, if mirrorStr == "" {
metadata.Mirror: _httpHeaderMirror, return false
}
val, err := strconv.ParseBool(mirrorStr)
if err != nil {
log.Warn("blademaster: failed to parse mirror: %+v", errors.Wrap(err, mirrorStr))
return false
}
if !val {
log.Warn("blademaster: request mirrorStr value :%s is false", mirrorStr)
}
return val
},
"criticality": func(in string) interface{} {
if crtl := criticality.Criticality(in); crtl != criticality.EmptyCriticality {
return string(crtl)
}
return string(criticality.Critical)
},
}
func parseMetadataTo(req *http.Request, to metadata.MD) {
for rawKey := range req.Header {
key := strings.ReplaceAll(strings.TrimLeft(strings.ToLower(rawKey), _httpHeaderMetadata), "-", "_")
rawValue := req.Header.Get(rawKey)
var value interface{} = rawValue
parser, ok := _parser[key]
if ok {
value = parser(rawValue)
}
to[key] = value
}
return
} }
func setMetadata(req *http.Request, key string, value interface{}) { func setMetadata(req *http.Request, key string, value interface{}) {
@ -45,47 +73,11 @@ func setMetadata(req *http.Request, key string, value interface{}) {
req.Header.Set(header, strV) req.Header.Set(header, strV)
} }
// mirror return true if x-bmspy-mirror in http header and its value is 1 or true.
func mirror(req *http.Request) bool {
mirrorStr := req.Header.Get(_httpHeaderMirror)
if mirrorStr == "" {
return false
}
val, err := strconv.ParseBool(mirrorStr)
if err != nil {
log.Warn("blademaster: failed to parse mirror: %+v", errors.Wrap(err, mirrorStr))
return false
}
if !val {
log.Warn("blademaster: request mirrorStr value :%s is false", mirrorStr)
}
return val
}
// setCaller set caller into http request. // setCaller set caller into http request.
func setCaller(req *http.Request) { func setCaller(req *http.Request) {
req.Header.Set(_httpHeaderUser, env.AppID) req.Header.Set(_httpHeaderUser, env.AppID)
} }
// caller get caller from http request.
func caller(req *http.Request) string {
return req.Header.Get(_httpHeaderUser)
}
// setColor set color into http request.
func setColor(req *http.Request, color string) {
req.Header.Set(_httpHeaderColor, color)
}
// color get color from http request.
func color(req *http.Request) string {
c := req.Header.Get(_httpHeaderColor)
if c == "" {
c = env.Color
}
return c
}
// setTimeout set timeout into http request. // setTimeout set timeout into http request.
func setTimeout(req *http.Request, timeout time.Duration) { func setTimeout(req *http.Request, timeout time.Duration) {
td := int64(timeout / time.Millisecond) td := int64(timeout / time.Millisecond)
@ -102,12 +94,6 @@ func timeout(req *http.Request) time.Duration {
return time.Duration(timeout) * time.Millisecond return time.Duration(timeout) * time.Millisecond
} }
// criticality get criticality from http request.
func criticality(req *http.Request) criticalityPkg.Criticality {
raw := req.Header.Get(_httpHeaderCriticality)
return criticalityPkg.Parse(raw)
}
// remoteIP implements a best effort algorithm to return the real client IP, it parses // remoteIP implements a best effort algorithm to return the real client IP, it parses
// x-backend-bm-real-ip or X-Real-IP or X-Forwarded-For in order to work properly with reverse-proxies such us: nginx or haproxy. // x-backend-bm-real-ip or X-Real-IP or X-Forwarded-For in order to work properly with reverse-proxies such us: nginx or haproxy.
// Use X-Forwarded-For before X-Real-Ip as nginx uses X-Real-Ip with the proxy's IP. // Use X-Forwarded-For before X-Real-Ip as nginx uses X-Real-Ip with the proxy's IP.

@ -15,7 +15,7 @@ import (
"github.com/bilibili/kratos/pkg/conf/dsn" "github.com/bilibili/kratos/pkg/conf/dsn"
"github.com/bilibili/kratos/pkg/log" "github.com/bilibili/kratos/pkg/log"
criticalityPkg "github.com/bilibili/kratos/pkg/net/criticality" "github.com/bilibili/kratos/pkg/net/criticality"
"github.com/bilibili/kratos/pkg/net/ip" "github.com/bilibili/kratos/pkg/net/ip"
"github.com/bilibili/kratos/pkg/net/metadata" "github.com/bilibili/kratos/pkg/net/metadata"
"github.com/bilibili/kratos/pkg/stat" "github.com/bilibili/kratos/pkg/stat"
@ -262,16 +262,11 @@ func (engine *Engine) handleContext(c *Context) {
tm = ctm tm = ctm
} }
md := metadata.MD{ md := metadata.MD{
metadata.Color: color(req),
metadata.RemoteIP: remoteIP(req), metadata.RemoteIP: remoteIP(req),
metadata.RemotePort: remotePort(req), metadata.RemotePort: remotePort(req),
metadata.Caller: caller(req), metadata.Criticality: string(criticality.Critical),
metadata.Mirror: mirror(req),
metadata.Criticality: string(criticalityPkg.Critical),
}
if crtl := criticality(req); crtl != criticalityPkg.EmptyCriticality {
md[metadata.Criticality] = string(crtl)
} }
parseMetadataTo(req, md)
ctx := metadata.NewContext(context.Background(), md) ctx := metadata.NewContext(context.Background(), md)
if tm > 0 { if tm > 0 {
c.Context, cancel = context.WithTimeout(ctx, tm) c.Context, cancel = context.WithTimeout(ctx, tm)

@ -87,7 +87,7 @@ func TestCriticality(t *testing.T) {
for _, testCase := range tests { for _, testCase := range tests {
req, err := http.NewRequest("GET", uri(SockAddr, testCase.path), nil) req, err := http.NewRequest("GET", uri(SockAddr, testCase.path), nil)
assert.NoError(t, err) assert.NoError(t, err)
req.Header.Set(_httpHeaderCriticality, string(testCase.crtl)) req.Header.Set("x-bm-metadata-criticality", string(testCase.crtl))
resp, err := client.Do(req) resp, err := client.Do(req)
assert.NoError(t, err) assert.NoError(t, err)
defer resp.Body.Close() defer resp.Body.Close()
@ -126,7 +126,7 @@ func TestNoneCriticality(t *testing.T) {
for _, testCase := range tests { for _, testCase := range tests {
req, err := http.NewRequest("GET", uri(SockAddr, testCase.path), nil) req, err := http.NewRequest("GET", uri(SockAddr, testCase.path), nil)
assert.NoError(t, err) assert.NoError(t, err)
req.Header.Set(_httpHeaderCriticality, string(testCase.crtl)) req.Header.Set("x-bm-metadata-criticality", string(testCase.crtl))
resp, err := client.Do(req) resp, err := client.Do(req)
assert.NoError(t, err) assert.NoError(t, err)
defer resp.Body.Close() defer resp.Body.Close()

Loading…
Cancel
Save