set transport.kind to be strongly typed (#728)

Signed-off-by: storyicon <storyicon@foxmail.com>
pull/746/head
storyicon 4 years ago committed by GitHub
parent 27dc0d45df
commit 18752bf0ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      transport/grpc/client.go
  2. 2
      transport/grpc/server.go
  3. 2
      transport/http/client.go
  4. 2
      transport/http/server.go
  5. 11
      transport/transport.go

@ -101,7 +101,7 @@ func dial(ctx context.Context, insecure bool, opts ...ClientOption) (*grpc.Clien
// UnaryClientInterceptor retruns a unary client interceptor.
func UnaryClientInterceptor(m middleware.Middleware) 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: "gRPC"})
ctx = transport.NewContext(ctx, transport.Transport{Kind: transport.KindGRPC})
ctx = NewClientContext(ctx, ClientInfo{FullMethod: method})
h := func(ctx context.Context, req interface{}) (interface{}, error) {
return reply, invoker(ctx, method, req, reply, cc, opts...)

@ -146,7 +146,7 @@ func UnaryTimeoutInterceptor(timeout time.Duration) grpc.UnaryServerInterceptor
// UnaryServerInterceptor returns a unary server interceptor.
func UnaryServerInterceptor(m middleware.Middleware) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
ctx = transport.NewContext(ctx, transport.Transport{Kind: "gRPC"})
ctx = transport.NewContext(ctx, transport.Transport{Kind: transport.KindGRPC})
ctx = NewServerContext(ctx, ServerInfo{Server: info.Server, FullMethod: info.FullMethod})
h := func(ctx context.Context, req interface{}) (interface{}, error) {
return handler(ctx, req)

@ -90,7 +90,7 @@ func (t *baseTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if t.userAgent != "" && req.Header.Get("User-Agent") == "" {
req.Header.Set("User-Agent", t.userAgent)
}
ctx := transport.NewContext(req.Context(), transport.Transport{Kind: "HTTP"})
ctx := transport.NewContext(req.Context(), transport.Transport{Kind: transport.KindHTTP})
ctx = NewClientContext(ctx, ClientInfo{Request: req})
ctx, cancel := context.WithTimeout(ctx, t.timeout)
defer cancel()

@ -96,7 +96,7 @@ func (s *Server) HandleFunc(path string, h http.HandlerFunc) {
func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request) {
ctx, cancel := context.WithTimeout(req.Context(), s.timeout)
defer cancel()
ctx = transport.NewContext(ctx, transport.Transport{Kind: "HTTP"})
ctx = transport.NewContext(ctx, transport.Transport{Kind: transport.KindHTTP})
ctx = NewServerContext(ctx, ServerInfo{Request: req, Response: res})
s.router.ServeHTTP(res, req.WithContext(ctx))
}

@ -17,9 +17,18 @@ type Server interface {
// Transport is transport context value.
type Transport struct {
Kind string
Kind Kind
}
// Kind defines the type of Transport
type Kind string
// Defines a set of transport kind
const (
KindGRPC Kind = "gRPC"
KindHTTP Kind = "HTTP"
)
type transportKey struct{}
// NewContext returns a new Context that carries value.

Loading…
Cancel
Save