add endpoint context (#979)

pull/980/head
Tony Chen 4 years ago committed by GitHub
parent f83438b693
commit 079f11fb50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      transport/grpc/client.go
  2. 4
      transport/grpc/context.go
  3. 4
      transport/grpc/server.go
  4. 12
      transport/http/client.go
  5. 3
      transport/http/context.go
  6. 4
      transport/http/server.go
  7. 3
      transport/transport.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 { 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 { 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 = transport.NewContext(ctx, transport.Transport{Kind: transport.KindGRPC, Endpoint: cc.Target()})
ctx = NewClientContext(ctx, ClientInfo{FullMethod: method, Target: cc.Target()}) ctx = NewClientContext(ctx, ClientInfo{FullMethod: method})
if timeout > 0 { if timeout > 0 {
var cancel context.CancelFunc var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, timeout) ctx, cancel = context.WithTimeout(ctx, timeout)

@ -2,7 +2,6 @@ package grpc
import ( import (
"context" "context"
"net/url"
) )
// ServerInfo represent gRPC server information. // ServerInfo represent gRPC server information.
@ -11,8 +10,6 @@ type ServerInfo struct {
Server interface{} Server interface{}
// FullMethod is the full RPC method string, i.e., /package.service/method. // FullMethod is the full RPC method string, i.e., /package.service/method.
FullMethod string FullMethod string
// Endpoint is a real address to registry endpoint.
Endpoint *url.URL
} }
type serverKey struct{} type serverKey struct{}
@ -32,7 +29,6 @@ func FromServerContext(ctx context.Context) (info ServerInfo, ok bool) {
type ClientInfo struct { type ClientInfo struct {
// FullMethod is the full RPC method string, i.e., /package.service/method. // FullMethod is the full RPC method string, i.e., /package.service/method.
FullMethod string FullMethod string
Target string
} }
type clientKey struct{} 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) { return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
ctx, cancel := ic.Merge(ctx, s.ctx) ctx, cancel := ic.Merge(ctx, s.ctx)
defer cancel() defer cancel()
ctx = transport.NewContext(ctx, transport.Transport{Kind: transport.KindGRPC}) ctx = transport.NewContext(ctx, transport.Transport{Kind: transport.KindGRPC, Endpoint: s.endpoint.String()})
ctx = NewServerContext(ctx, ServerInfo{Server: info.Server, FullMethod: info.FullMethod, Endpoint: s.endpoint}) ctx = NewServerContext(ctx, ServerInfo{Server: info.Server, FullMethod: info.FullMethod})
if s.timeout > 0 { if s.timeout > 0 {
var cancel context.CancelFunc var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, s.timeout) 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 reqBody io.Reader
contentType string contentType string
) )
c := defaultCallInfo() c := defaultCallInfo()
for _, o := range opts { for _, o := range opts {
if err := o.before(&c); err != nil { if err := o.before(&c); err != nil {
return err return err
} }
} }
if args != nil { if args != nil {
var ( var (
body []byte body []byte
@ -239,14 +237,8 @@ func (client *Client) Invoke(ctx context.Context, path string, args interface{},
if client.userAgent != "" { if client.userAgent != "" {
req.Header.Set("User-Agent", client.userAgent) req.Header.Set("User-Agent", client.userAgent)
} }
ctx = transport.NewContext(ctx, transport.Transport{Kind: transport.KindHTTP, Endpoint: client.endpoint})
ctx = transport.NewContext(ctx, transport.Transport{Kind: transport.KindHTTP}) ctx = NewClientContext(ctx, ClientInfo{PathPattern: c.pathPattern, Request: req})
ctx = NewClientContext(ctx, ClientInfo{
Target: client.endpoint,
PathPattern: c.pathPattern,
Request: req,
})
return client.invoke(ctx, req, args, reply, c) return client.invoke(ctx, req, args, reply, c)
} }

@ -3,14 +3,12 @@ package http
import ( import (
"context" "context"
"net/http" "net/http"
"net/url"
) )
// ServerInfo represent HTTP server information. // ServerInfo represent HTTP server information.
type ServerInfo struct { type ServerInfo struct {
Request *http.Request Request *http.Request
Response http.ResponseWriter Response http.ResponseWriter
Endpoint *url.URL
} }
type serverKey struct{} type serverKey struct{}
@ -30,7 +28,6 @@ func FromServerContext(ctx context.Context) (info ServerInfo, ok bool) {
type ClientInfo struct { type ClientInfo struct {
Request *http.Request Request *http.Request
PathPattern string PathPattern string
Target string
} }
type clientKey struct{} 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) { func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request) {
ctx, cancel := ic.Merge(req.Context(), s.ctx) ctx, cancel := ic.Merge(req.Context(), s.ctx)
defer cancel() defer cancel()
ctx = transport.NewContext(ctx, transport.Transport{Kind: transport.KindHTTP}) ctx = transport.NewContext(ctx, transport.Transport{Kind: transport.KindHTTP, Endpoint: s.endpoint.String()})
ctx = NewServerContext(ctx, ServerInfo{Request: req, Response: res, Endpoint: s.endpoint}) ctx = NewServerContext(ctx, ServerInfo{Request: req, Response: res})
if s.timeout > 0 { if s.timeout > 0 {
ctx, cancel = context.WithTimeout(ctx, s.timeout) ctx, cancel = context.WithTimeout(ctx, s.timeout)
defer cancel() defer cancel()

@ -24,7 +24,8 @@ type Endpointer interface {
// Transport is transport context value. // Transport is transport context value.
type Transport struct { type Transport struct {
Kind Kind Kind Kind
Endpoint string
} }
// Kind defines the type of Transport // Kind defines the type of Transport

Loading…
Cancel
Save