clean transport (#920)

pull/921/head
Tony Chen 4 years ago committed by GitHub
parent 8f8b861f7d
commit 0dbcaa2443
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      transport/grpc/server.go
  2. 6
      transport/http/client.go

@ -15,7 +15,7 @@ import (
"github.com/go-kratos/kratos/v2/transport" "github.com/go-kratos/kratos/v2/transport"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/health" "google.golang.org/grpc/health"
healthpb "google.golang.org/grpc/health/grpc_health_v1" "google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/reflection" "google.golang.org/grpc/reflection"
) )
@ -105,11 +105,9 @@ func NewServer(opts ...ServerOption) *Server {
} }
srv.Server = grpc.NewServer(grpcOpts...) srv.Server = grpc.NewServer(grpcOpts...)
srv.metaServer = metadata.NewServer(srv.Server) srv.metaServer = metadata.NewServer(srv.Server)
// grpc health register // internal register
healthpb.RegisterHealthServer(srv.Server, srv.health) grpc_health_v1.RegisterHealthServer(srv.Server, srv.health)
// api metadata register
api.RegisterMetadataServer(srv.Server, srv.metaServer) api.RegisterMetadataServer(srv.Server, srv.metaServer)
// reflection register
reflection.Register(srv.Server) reflection.Register(srv.Server)
return srv return srv
} }

@ -74,7 +74,7 @@ func NewTransport(ctx context.Context, opts ...ClientOption) (http.RoundTripper,
ctx: ctx, ctx: ctx,
timeout: 500 * time.Millisecond, timeout: 500 * time.Millisecond,
transport: http.DefaultTransport, transport: http.DefaultTransport,
errorDecoder: CheckResponse, errorDecoder: checkResponse,
} }
for _, o := range opts { for _, o := range opts {
o(options) o(options)
@ -144,9 +144,9 @@ func Do(client *http.Client, req *http.Request, target interface{}) error {
return codec.Unmarshal(data, target) return codec.Unmarshal(data, target)
} }
// CheckResponse returns an error (of type *Error) if the response // checkResponse returns an error (of type *Error) if the response
// status code is not 2xx. // status code is not 2xx.
func CheckResponse(ctx context.Context, res *http.Response) error { func checkResponse(ctx context.Context, res *http.Response) error {
if res.StatusCode >= 200 && res.StatusCode <= 299 { if res.StatusCode >= 200 && res.StatusCode <= 299 {
return nil return nil
} }

Loading…
Cancel
Save