chore: refactor gateway (#3157)

This commit is contained in:
Kevin Wan 2023-04-22 23:25:51 +08:00 committed by GitHub
parent de1e0f2410
commit 027193dc99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 32 deletions

View File

@ -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
}
@ -44,7 +41,6 @@ func MustNewServer(c GatewayConf, opts ...Option) *Server {
svr := &Server{
c: c,
Server: rest.MustNewServer(c.RestConf),
timeout: time.Duration(c.Timeout) * time.Millisecond,
}
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)
}

View File

@ -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,7 +61,8 @@ func TestMustNewServer(t *testing.T) {
},
},
},
client: zrpc.MustNewClient(zrpc.RpcClientConf{
client: zrpc.MustNewClient(
zrpc.RpcClientConf{
Endpoints: []string{"foo"},
Timeout: 1000,
Middlewares: zrpc.ClientMiddlewaresConf{
@ -71,10 +74,7 @@ func TestMustNewServer(t *testing.T) {
},
},
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)
}