Repository: zeromicro/go-zero
Pull request #3157 (2023-04-22 14:11:17)

chore: refactor gateway

codecov[bot]: ## [Codecov](https://codecov.io/gh/zeromicro/go-zero/pull/3157?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#3157](https://codecov.io/gh/zeromicro/go-zero/pull/3157?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (73de14b) into [master](https://codecov.io/gh/zeromicro/go-zero/commit/062073ce5811d222be20e5713b4f29fff04347e6?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (062073c) will **decrease** coverage by `0.01%`.
> The diff coverage is `80.00%`.


| [Impacted Files](https://codecov.io/gh/zeromicro/go-zero/pull/3157?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [gateway/server.go](https://codecov.io/gh/zeromicro/go-zero/pull/3157?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Z2F0ZXdheS9zZXJ2ZXIuZ28=) | `36.76% <80.00%> (-2.96%)` | :arrow_down: |


Patch:
diff --git a/gateway/server.go b/gateway/server.go
index e022e460..e0b85e81 100644
--- a/gateway/server.go
+++ b/gateway/server.go
@@ -5,7 +5,6 @@ import (
 	"fmt"
 	"net/http"
 	"strings"
-	"time"
 
 	"github.com/fullstorydev/grpcurl"
 	"github.com/golang/protobuf/jsonpb"
@@ -17,7 +16,6 @@ import (
 	"github.com/zeromicro/go-zero/rest/httpx"
 	"github.com/zeromicro/go-zero/zrpc"
 	"google.golang.org/grpc/codes"
-	"google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
 )
 
 type (
@@ -26,7 +24,6 @@ type (
 		c GatewayConf
 		*rest.Server
 		upstreams     []*upstream
-		timeout       time.Duration
 		processHeader func(http.Header) []string
 	}
 
@@ -42,9 +39,8 @@ type (
 // MustNewServer creates a new gateway server.
 func MustNewServer(c GatewayConf, opts ...Option) *Server {
 	svr := &Server{
-		c:       c,
-		Server:  rest.MustNewServer(c.RestConf),
-		timeout: time.Duration(c.Timeout) * time.Millisecond,
+		c:      c,
+		Server: rest.MustNewServer(c.RestConf),
 	}
 	for _, opt := range opts {
 		opt(svr)
@@ -68,6 +64,7 @@ func (s *Server) build() error {
 	if err := s.buildClient(); err != nil {
 		return err
 	}
+
 	return s.buildUpstream()
 }
 
@@ -84,7 +81,9 @@ func (s *Server) buildClient() error {
 		target, err := up.Grpc.BuildTarget()
 		if err != nil {
 			cancel(err)
+			return
 		}
+
 		up.Name = target
 		cli := zrpc.MustNewClient(up.Grpc)
 		writer.Write(&upstream{
@@ -160,13 +159,9 @@ func (s *Server) buildHandler(source grpcurl.DescriptorSource, resolver jsonpb.A
 			return
 		}
 
-		timeout := internal.GetTimeout(r.Header, s.timeout)
-		ctx, can := context.WithTimeout(r.Context(), timeout)
-		defer can()
-
 		w.Header().Set(httpx.ContentType, httpx.JsonContentType)
 		handler := internal.NewEventHandler(w, resolver)
-		if err := grpcurl.InvokeRPC(ctx, source, cli.Conn(), rpcPath, s.prepareMetadata(r.Header),
+		if err := grpcurl.InvokeRPC(r.Context(), source, cli.Conn(), rpcPath, s.prepareMetadata(r.Header),
 			handler, parser.Next); err != nil {
 			httpx.ErrorCtx(r.Context(), w, err)
 		}
@@ -188,8 +183,7 @@ func (s *Server) createDescriptorSource(cli zrpc.Client, up Upstream) (grpcurl.D
 			return nil, err
 		}
 	} else {
-		refCli := grpc_reflection_v1alpha.NewServerReflectionClient(cli.Conn())
-		client := grpcreflect.NewClient(context.Background(), refCli)
+		client := grpcreflect.NewClientAuto(context.Background(), cli.Conn())
 		source = grpcurl.DescriptorSourceFromServer(context.Background(), client)
 	}
 


Test patch:
diff --git a/gateway/server_test.go b/gateway/server_test.go
index 2c420c3b..c9d168d9 100644
--- a/gateway/server_test.go
+++ b/gateway/server_test.go
@@ -44,10 +44,12 @@ func dialer() func(context.Context, string) (net.Conn, error) {
 func TestMustNewServer(t *testing.T) {
 	var c GatewayConf
 	assert.NoError(t, conf.FillDefault(&c))
+	// avoid popup alert on macos for asking permissions
+	c.DevServer.Host = "localhost"
+	c.Host = "localhost"
 	c.Port = 18881
 
 	s := MustNewServer(c)
-
 	s.upstreams = []*upstream{
 		{
 			Upstream: Upstream{
@@ -59,22 +61,20 @@ func TestMustNewServer(t *testing.T) {
 					},
 				},
 			},
-			client: zrpc.MustNewClient(zrpc.RpcClientConf{
-				Endpoints: []string{"foo"},
-				Timeout:   1000,
-				Middlewares: zrpc.ClientMiddlewaresConf{
-					Trace:      true,
-					Duration:   true,
-					Prometheus: true,
-					Breaker:    true,
-					Timeout:    true,
+			client: zrpc.MustNewClient(
+				zrpc.RpcClientConf{
+					Endpoints: []string{"foo"},
+					Timeout:   1000,
+					Middlewares: zrpc.ClientMiddlewaresConf{
+						Trace:      true,
+						Duration:   true,
+						Prometheus: true,
+						Breaker:    true,
+						Timeout:    true,
+					},
 				},
-			},
 				zrpc.WithDialOption(grpc.WithContextDialer(dialer())),
-				zrpc.WithUnaryClientInterceptor(func(ctx context.Context, method string, req, reply any,
-					cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
-					return invoker(ctx, method, req, reply, cc, opts...)
-				})),
+			),
 		},
 	}
 
@@ -83,13 +83,11 @@ func TestMustNewServer(t *testing.T) {
 
 	time.Sleep(time.Millisecond * 100)
 
-	ctx := context.Background()
-
-	resp, err := httpc.Do(ctx, http.MethodGet, "http://localhost:18881/deposit/100", nil)
+	resp, err := httpc.Do(context.Background(), http.MethodGet, "http://localhost:18881/deposit/100", nil)
 	assert.NoError(t, err)
 	assert.Equal(t, http.StatusOK, resp.StatusCode)
 
-	resp, err = httpc.Do(ctx, http.MethodGet, "http://localhost:18881/deposit_fail/100", nil)
+	resp, err = httpc.Do(context.Background(), http.MethodGet, "http://localhost:18881/deposit_fail/100", nil)
 	assert.NoError(t, err)
 	assert.Equal(t, http.StatusNotFound, resp.StatusCode)
 }