mirror of
https://github.com/go-kratos/kratos.git
synced 2025-01-24 03:46:37 +02:00
set transport.kind to be strongly typed (#728)
Signed-off-by: storyicon <storyicon@foxmail.com>
This commit is contained in:
parent
27dc0d45df
commit
18752bf0ec
@ -101,7 +101,7 @@ func dial(ctx context.Context, insecure bool, opts ...ClientOption) (*grpc.Clien
|
|||||||
// UnaryClientInterceptor retruns a unary client interceptor.
|
// UnaryClientInterceptor retruns a unary client interceptor.
|
||||||
func UnaryClientInterceptor(m middleware.Middleware) grpc.UnaryClientInterceptor {
|
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 {
|
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})
|
ctx = NewClientContext(ctx, ClientInfo{FullMethod: method})
|
||||||
h := func(ctx context.Context, req interface{}) (interface{}, error) {
|
h := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||||
return reply, invoker(ctx, method, req, reply, cc, opts...)
|
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.
|
// UnaryServerInterceptor returns a unary server interceptor.
|
||||||
func UnaryServerInterceptor(m middleware.Middleware) grpc.UnaryServerInterceptor {
|
func UnaryServerInterceptor(m middleware.Middleware) 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 = 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})
|
ctx = NewServerContext(ctx, ServerInfo{Server: info.Server, FullMethod: info.FullMethod})
|
||||||
h := func(ctx context.Context, req interface{}) (interface{}, error) {
|
h := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||||
return handler(ctx, req)
|
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") == "" {
|
if t.userAgent != "" && req.Header.Get("User-Agent") == "" {
|
||||||
req.Header.Set("User-Agent", t.userAgent)
|
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 = NewClientContext(ctx, ClientInfo{Request: req})
|
||||||
ctx, cancel := context.WithTimeout(ctx, t.timeout)
|
ctx, cancel := context.WithTimeout(ctx, t.timeout)
|
||||||
defer cancel()
|
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) {
|
func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
||||||
ctx, cancel := context.WithTimeout(req.Context(), s.timeout)
|
ctx, cancel := context.WithTimeout(req.Context(), s.timeout)
|
||||||
defer cancel()
|
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})
|
ctx = NewServerContext(ctx, ServerInfo{Request: req, Response: res})
|
||||||
s.router.ServeHTTP(res, req.WithContext(ctx))
|
s.router.ServeHTTP(res, req.WithContext(ctx))
|
||||||
}
|
}
|
||||||
|
@ -17,9 +17,18 @@ type Server interface {
|
|||||||
|
|
||||||
// Transport is transport context value.
|
// Transport is transport context value.
|
||||||
type Transport struct {
|
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{}
|
type transportKey struct{}
|
||||||
|
|
||||||
// NewContext returns a new Context that carries value.
|
// NewContext returns a new Context that carries value.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user