From 079f11fb5034cb539bb8dc2461a781e7fd3c404a Mon Sep 17 00:00:00 2001 From: Tony Chen Date: Tue, 1 Jun 2021 12:02:22 +0800 Subject: [PATCH] add endpoint context (#979) --- transport/grpc/client.go | 4 ++-- transport/grpc/context.go | 4 ---- transport/grpc/server.go | 4 ++-- transport/http/client.go | 12 ++---------- transport/http/context.go | 3 --- transport/http/server.go | 4 ++-- transport/transport.go | 3 ++- 7 files changed, 10 insertions(+), 24 deletions(-) diff --git a/transport/grpc/client.go b/transport/grpc/client.go index c56b3ea91..2a0709d75 100644 --- a/transport/grpc/client.go +++ b/transport/grpc/client.go @@ -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) diff --git a/transport/grpc/context.go b/transport/grpc/context.go index 0347a78c2..2708c56a3 100644 --- a/transport/grpc/context.go +++ b/transport/grpc/context.go @@ -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{} diff --git a/transport/grpc/server.go b/transport/grpc/server.go index 40f7fc48d..5acbbf1e7 100644 --- a/transport/grpc/server.go +++ b/transport/grpc/server.go @@ -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) diff --git a/transport/http/client.go b/transport/http/client.go index 5d4cf5c89..65351b5e2 100644 --- a/transport/http/client.go +++ b/transport/http/client.go @@ -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) } diff --git a/transport/http/context.go b/transport/http/context.go index 68c57d9ac..e8bab4a88 100644 --- a/transport/http/context.go +++ b/transport/http/context.go @@ -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{} diff --git a/transport/http/server.go b/transport/http/server.go index 10afbb572..162746e32 100644 --- a/transport/http/server.go +++ b/transport/http/server.go @@ -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() diff --git a/transport/transport.go b/transport/transport.go index aec86d1b4..51a5af0b3 100644 --- a/transport/transport.go +++ b/transport/transport.go @@ -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