mirror of
https://github.com/go-kratos/kratos.git
synced 2025-01-22 03:38:41 +02:00
add endpoint context (#979)
This commit is contained in:
parent
f83438b693
commit
079f11fb50
@ -102,8 +102,8 @@ func dial(ctx context.Context, insecure bool, opts ...ClientOption) (*grpc.Clien
|
||||
|
||||
func unaryClientInterceptor(m middleware.Middleware, timeout time.Duration) grpc.UnaryClientInterceptor {
|
||||
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
|
||||
ctx = transport.NewContext(ctx, transport.Transport{Kind: transport.KindGRPC})
|
||||
ctx = NewClientContext(ctx, ClientInfo{FullMethod: method, Target: cc.Target()})
|
||||
ctx = transport.NewContext(ctx, transport.Transport{Kind: transport.KindGRPC, Endpoint: cc.Target()})
|
||||
ctx = NewClientContext(ctx, ClientInfo{FullMethod: method})
|
||||
if timeout > 0 {
|
||||
var cancel context.CancelFunc
|
||||
ctx, cancel = context.WithTimeout(ctx, timeout)
|
||||
|
@ -2,7 +2,6 @@ package grpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// ServerInfo represent gRPC server information.
|
||||
@ -11,8 +10,6 @@ type ServerInfo struct {
|
||||
Server interface{}
|
||||
// FullMethod is the full RPC method string, i.e., /package.service/method.
|
||||
FullMethod string
|
||||
// Endpoint is a real address to registry endpoint.
|
||||
Endpoint *url.URL
|
||||
}
|
||||
|
||||
type serverKey struct{}
|
||||
@ -32,7 +29,6 @@ func FromServerContext(ctx context.Context) (info ServerInfo, ok bool) {
|
||||
type ClientInfo struct {
|
||||
// FullMethod is the full RPC method string, i.e., /package.service/method.
|
||||
FullMethod string
|
||||
Target string
|
||||
}
|
||||
|
||||
type clientKey struct{}
|
||||
|
@ -166,8 +166,8 @@ func (s *Server) unaryServerInterceptor() grpc.UnaryServerInterceptor {
|
||||
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
|
||||
ctx, cancel := ic.Merge(ctx, s.ctx)
|
||||
defer cancel()
|
||||
ctx = transport.NewContext(ctx, transport.Transport{Kind: transport.KindGRPC})
|
||||
ctx = NewServerContext(ctx, ServerInfo{Server: info.Server, FullMethod: info.FullMethod, Endpoint: s.endpoint})
|
||||
ctx = transport.NewContext(ctx, transport.Transport{Kind: transport.KindGRPC, Endpoint: s.endpoint.String()})
|
||||
ctx = NewServerContext(ctx, ServerInfo{Server: info.Server, FullMethod: info.FullMethod})
|
||||
if s.timeout > 0 {
|
||||
var cancel context.CancelFunc
|
||||
ctx, cancel = context.WithTimeout(ctx, s.timeout)
|
||||
|
@ -209,14 +209,12 @@ func (client *Client) Invoke(ctx context.Context, path string, args interface{},
|
||||
reqBody io.Reader
|
||||
contentType string
|
||||
)
|
||||
|
||||
c := defaultCallInfo()
|
||||
for _, o := range opts {
|
||||
if err := o.before(&c); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if args != nil {
|
||||
var (
|
||||
body []byte
|
||||
@ -239,14 +237,8 @@ func (client *Client) Invoke(ctx context.Context, path string, args interface{},
|
||||
if client.userAgent != "" {
|
||||
req.Header.Set("User-Agent", client.userAgent)
|
||||
}
|
||||
|
||||
ctx = transport.NewContext(ctx, transport.Transport{Kind: transport.KindHTTP})
|
||||
ctx = NewClientContext(ctx, ClientInfo{
|
||||
Target: client.endpoint,
|
||||
PathPattern: c.pathPattern,
|
||||
Request: req,
|
||||
})
|
||||
|
||||
ctx = transport.NewContext(ctx, transport.Transport{Kind: transport.KindHTTP, Endpoint: client.endpoint})
|
||||
ctx = NewClientContext(ctx, ClientInfo{PathPattern: c.pathPattern, Request: req})
|
||||
return client.invoke(ctx, req, args, reply, c)
|
||||
}
|
||||
|
||||
|
@ -3,14 +3,12 @@ package http
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// ServerInfo represent HTTP server information.
|
||||
type ServerInfo struct {
|
||||
Request *http.Request
|
||||
Response http.ResponseWriter
|
||||
Endpoint *url.URL
|
||||
}
|
||||
|
||||
type serverKey struct{}
|
||||
@ -30,7 +28,6 @@ func FromServerContext(ctx context.Context) (info ServerInfo, ok bool) {
|
||||
type ClientInfo struct {
|
||||
Request *http.Request
|
||||
PathPattern string
|
||||
Target string
|
||||
}
|
||||
|
||||
type clientKey struct{}
|
||||
|
@ -99,8 +99,8 @@ func (s *Server) HandleFunc(path string, h http.HandlerFunc) {
|
||||
func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
||||
ctx, cancel := ic.Merge(req.Context(), s.ctx)
|
||||
defer cancel()
|
||||
ctx = transport.NewContext(ctx, transport.Transport{Kind: transport.KindHTTP})
|
||||
ctx = NewServerContext(ctx, ServerInfo{Request: req, Response: res, Endpoint: s.endpoint})
|
||||
ctx = transport.NewContext(ctx, transport.Transport{Kind: transport.KindHTTP, Endpoint: s.endpoint.String()})
|
||||
ctx = NewServerContext(ctx, ServerInfo{Request: req, Response: res})
|
||||
if s.timeout > 0 {
|
||||
ctx, cancel = context.WithTimeout(ctx, s.timeout)
|
||||
defer cancel()
|
||||
|
@ -24,7 +24,8 @@ type Endpointer interface {
|
||||
|
||||
// Transport is transport context value.
|
||||
type Transport struct {
|
||||
Kind Kind
|
||||
Kind Kind
|
||||
Endpoint string
|
||||
}
|
||||
|
||||
// Kind defines the type of Transport
|
||||
|
Loading…
x
Reference in New Issue
Block a user