Skip to content

Commit 1364348

Browse files
committed
addressing comments
1 parent df43eb9 commit 1364348

7 files changed

Lines changed: 35 additions & 50 deletions

File tree

internal/internal.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,9 @@ var (
246246
return cleanup
247247
}
248248

249-
// ServerStreamWrapper returns a ServerOption that sets the internal
250-
// stream wrapper used for HTTP filters.
251-
ServerStreamWrapper any // func(func(any) (any, error)) grpc.ServerOption
249+
// XDSFilterWrapperOption returns a ServerOption that sets the internal
250+
// stream wrapper used for server-side xDS HTTP filters.
251+
XDSFilterWrapperOption any // func(func(grpc.ServerStream) (grpc.ServerStream, error)) grpc.ServerOption
252252
)
253253

254254
// HealthChecker defines the signature of the client-side LB channel health

internal/xds/httpfilter/httpfilter.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ type ClientInterceptor interface {
9595
// Note: RPCInfo.Context is currently unused and will be nil.
9696
NewStream(ctx context.Context, ri iresolver.RPCInfo, newStream func(ctx context.Context, opts ...grpc.CallOption) (grpc.ClientStream, error), opts ...grpc.CallOption) (grpc.ClientStream, error)
9797

98-
// Close closes the interceptor. Once called, no new calls to NewStream are
99-
// accepted. Ongoing calls to NewStream are allowed to complete.
98+
// Close closes the interceptor. No new RPCs will be dispatched to this
99+
// interceptor, but ongoing calls to NewStream are allowed to complete.
100100
Close()
101101
}
102102

@@ -142,8 +142,8 @@ type ServerInterceptor interface {
142142
// Implementations should never return (nil, nil).
143143
InterceptRPC(ss grpc.ServerStream) (grpc.ServerStream, error)
144144

145-
// Close closes the interceptor. Once called, no new calls to InterceptRPC are
146-
// accepted. Ongoing calls to InterceptRPC are allowed to complete.
145+
// Close closes the interceptor. No new RPCs will be dispatched to this
146+
// interceptor, but ongoing calls to InterceptRPC are allowed to complete.
147147
Close()
148148
}
149149

internal/xds/server/routing.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,13 @@ package server
2020

2121
import (
2222
"errors"
23-
"fmt"
2423
"strings"
2524

2625
"google.golang.org/grpc"
2726
"google.golang.org/grpc/codes"
2827
"google.golang.org/grpc/internal/transport"
2928
"google.golang.org/grpc/internal/xds/xdsclient/xdsresource"
3029
"google.golang.org/grpc/metadata"
31-
"google.golang.org/grpc/status"
3230
)
3331

3432
// RouteAndProcess routes the incoming RPC to a configured route in the route
@@ -50,16 +48,16 @@ func RouteAndProcess(ss grpc.ServerStream) (grpc.ServerStream, error) {
5048
if logger.V(2) {
5149
logger.Infof("RPC on connection with xDS Configuration error: %v", rc.err)
5250
}
53-
return nil, status.Error(codes.Unavailable, fmt.Sprintf("error from xDS configuration for matched route configuration: %v", rc.err))
51+
return nil, rc.statusErrWithNodeID(codes.Unavailable, "error from xDS configuration for matched route configuration: %v", rc.err)
5452
}
5553

5654
mn, ok := grpc.Method(ctx)
5755
if !ok {
58-
return nil, errors.New("missing method name in incoming context")
56+
return nil, rc.statusErrWithNodeID(codes.Internal, "missing method name in incoming context")
5957
}
6058
md, ok := metadata.FromIncomingContext(ctx)
6159
if !ok {
62-
return nil, errors.New("missing metadata in incoming context")
60+
return nil, rc.statusErrWithNodeID(codes.Internal, "missing metadata in incoming context")
6361
}
6462
// A41 added logic to the core grpc implementation to guarantee that once the
6563
// RPC gets to this point, there will be a single, unambiguous authority

server.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,7 @@ func init() {
8989
internal.MetricsRecorderForServer = func(srv *Server) estats.MetricsRecorder {
9090
return istats.NewMetricsRecorderList(srv.opts.statsHandlers)
9191
}
92-
internal.ServerStreamWrapper = func(w func(any) (any, error)) any {
93-
return serverStreamWrapper(func(ss ServerStream) (ServerStream, error) {
94-
res, err := w(ss)
95-
if err != nil || res == nil {
96-
return nil, err
97-
}
98-
return res.(ServerStream), nil
99-
})
100-
}
92+
internal.XDSFilterWrapperOption = xdsFilterWrapperOption
10193
}
10294

10395
var statusOK = status.New(codes.OK, "")
@@ -681,9 +673,9 @@ func bufferPool(bufferPool mem.BufferPool) ServerOption {
681673
})
682674
}
683675

684-
// serverStreamWrapper returns a ServerOption that sets the server-level
676+
// xdsFilterWrapperOption returns a ServerOption that sets the server-level
685677
// stream wrapper (used internally by xDS server filters).
686-
func serverStreamWrapper(w func(ServerStream) (ServerStream, error)) ServerOption {
678+
func xdsFilterWrapperOption(w func(ServerStream) (ServerStream, error)) ServerOption {
687679
return newFuncServerOption(func(o *serverOptions) {
688680
o.streamWrapper = w
689681
})
@@ -1450,12 +1442,7 @@ func (s *Server) processRPC(ctx context.Context, stream *transport.ServerStream,
14501442
var appErr error
14511443
var wrappedStream ServerStream = ss
14521444
if s.opts.streamWrapper != nil {
1453-
ws, err := s.opts.streamWrapper(ss)
1454-
if err != nil {
1455-
appErr = err
1456-
} else if ws != nil {
1457-
wrappedStream = ws
1458-
}
1445+
wrappedStream, appErr = s.opts.streamWrapper(ss)
14591446
}
14601447

14611448
if appErr == nil {

test/server_test.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -490,20 +490,17 @@ type wrappedTestStream struct {
490490
grpc.ServerStream
491491
}
492492

493-
// Test verifies that an internal stream wrapper option configured on the
493+
// Test verifies that an internal xDS filter wrapper option configured on the
494494
// server gets invoked and can wrap the ServerStream for both Unary and
495495
// Streaming RPCs.
496-
func (s) TestServerStreamWrapper(t *testing.T) {
496+
func (s) TestXDSFilterWrapperOption(t *testing.T) {
497497
var wrapperCalled atomic.Bool
498-
wrapper := func(ss any) (any, error) {
498+
wrapper := func(ss grpc.ServerStream) (grpc.ServerStream, error) {
499499
wrapperCalled.Store(true)
500-
if sstream, ok := ss.(grpc.ServerStream); ok && sstream != nil {
501-
return &wrappedTestStream{ServerStream: sstream}, nil
502-
}
503-
return nil, nil
500+
return &wrappedTestStream{ServerStream: ss}, nil
504501
}
505502

506-
opt := internal.ServerStreamWrapper.(func(func(any) (any, error)) any)(wrapper).(grpc.ServerOption)
503+
opt := internal.XDSFilterWrapperOption.(func(func(grpc.ServerStream) (grpc.ServerStream, error)) grpc.ServerOption)(wrapper)
507504

508505
ss := &stubserver.StubServer{
509506
EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) {
@@ -524,7 +521,7 @@ func (s) TestServerStreamWrapper(t *testing.T) {
524521
t.Fatalf("EmptyCall failed: %v", err)
525522
}
526523
if !wrapperCalled.Load() {
527-
t.Fatal("ServerStreamWrapper callback was not called for Unary RPC")
524+
t.Fatal("XDSFilterWrapperOption callback was not called for Unary RPC")
528525
}
529526

530527
wrapperCalled.Store(false)
@@ -536,19 +533,19 @@ func (s) TestServerStreamWrapper(t *testing.T) {
536533
t.Fatalf("Recv failed: %v", err)
537534
}
538535
if !wrapperCalled.Load() {
539-
t.Fatal("ServerStreamWrapper callback was not called for Streaming RPC")
536+
t.Fatal("XDSFilterWrapperOption callback was not called for Streaming RPC")
540537
}
541538
}
542539

543-
// Test verifies that if an internal stream wrapper returns an error,
540+
// Test verifies that if an internal xDS filter wrapper returns an error,
544541
// the RPC is rejected early with that status error before executing
545542
// handlers.
546-
func (s) TestServerStreamWrapper_EarlyRejection(t *testing.T) {
547-
wrapper := func(any) (any, error) {
543+
func (s) TestXDSFilterWrapperOption_EarlyRejection(t *testing.T) {
544+
wrapper := func(grpc.ServerStream) (grpc.ServerStream, error) {
548545
return nil, status.Error(codes.PermissionDenied, "early rejection by internal wrapper")
549546
}
550547

551-
opt := internal.ServerStreamWrapper.(func(func(any) (any, error)) any)(wrapper).(grpc.ServerOption)
548+
opt := internal.XDSFilterWrapperOption.(func(func(grpc.ServerStream) (grpc.ServerStream, error)) grpc.ServerOption)(wrapper)
552549

553550
ss := &stubserver.StubServer{
554551
EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) {

xds/server.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,18 @@ func NewGRPCServer(opts ...grpc.ServerOption) (*GRPCServer, error) {
7575
// xdsFilterWrapper is a callback passed to the underlying gRPC server.
7676
// It adapts the generic stream parameter and delegates to RouteAndProcess
7777
// to execute xDS HTTP filter interceptors for each incoming RPC.
78-
xdsFilterWrapper := func(ss any) (any, error) {
79-
return server.RouteAndProcess(ss.(grpc.ServerStream))
78+
xDSFilterWrapper := func(ss grpc.ServerStream) (grpc.ServerStream, error) {
79+
return server.RouteAndProcess(ss)
8080
}
81-
// Construct a grpc.ServerOption that registers xdsFilterWrapper on
81+
// Construct a grpc.ServerOption that registers xDSFilterWrapper on
8282
// the server.
83-
xdsInternalOpt := internal.ServerStreamWrapper.(func(func(any) (any, error)) any)(xdsFilterWrapper).(grpc.ServerOption)
84-
opts = append(opts, xdsInternalOpt)
83+
xDSFilterWrapperOption := internal.XDSFilterWrapperOption.(func(func(grpc.ServerStream) (grpc.ServerStream, error)) grpc.ServerOption)
84+
newOpts := []grpc.ServerOption{
85+
xDSFilterWrapperOption(xDSFilterWrapper),
86+
}
87+
newOpts = append(newOpts, opts...)
8588
s := &GRPCServer{
86-
gs: newGRPCServer(opts...),
89+
gs: newGRPCServer(newOpts...),
8790
quit: grpcsync.NewEvent(),
8891
}
8992
s.handleServerOptions(opts)

xds/server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func (s) TestNewServer_Success(t *testing.T) {
147147
t.Fatalf("%d ServerOptions passed to grpc.Server, want %d", got, wantServerOpts)
148148
}
149149
// Verify that the user passed ServerOptions are forwarded as is.
150-
if !reflect.DeepEqual(opts[:len(test.serverOpts)], test.serverOpts) {
150+
if !reflect.DeepEqual(opts[1:], test.serverOpts) {
151151
t.Fatalf("got ServerOptions %v, want %v", opts[:len(test.serverOpts)], test.serverOpts)
152152
}
153153
return grpc.NewServer(opts...)

0 commit comments

Comments
 (0)