@@ -48,8 +48,9 @@ import (
48
48
"google.golang.org/protobuf/reflect/protoreflect"
49
49
"google.golang.org/protobuf/reflect/protoregistry"
50
50
51
+ v1grpc "google.golang.org/grpc/reflection/grpc_reflection_v1"
52
+ v1pb "google.golang.org/grpc/reflection/grpc_reflection_v1"
51
53
v1alphagrpc "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
52
- v1alphapb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
53
54
)
54
55
55
56
// GRPCServer is the interface provided by a gRPC server. It is implemented by
@@ -63,9 +64,19 @@ type GRPCServer interface {
63
64
var _ GRPCServer = (* grpc .Server )(nil )
64
65
65
66
// Register registers the server reflection service on the given gRPC server.
67
+ // Both the v1 and v1alpha versions are registered.
66
68
func Register (s GRPCServer ) {
67
- svr := NewServer (ServerOptions {Services : s })
68
- v1alphagrpc .RegisterServerReflectionServer (s , svr )
69
+ svr := NewServerV1 (ServerOptions {Services : s })
70
+ v1alphagrpc .RegisterServerReflectionServer (s , asV1Alpha (svr ))
71
+ v1grpc .RegisterServerReflectionServer (s , svr )
72
+ }
73
+
74
+ // RegisterV1 registers only the v1 version of the server reflection service
75
+ // on the given gRPC server. Many clients may only support v1alpha so most
76
+ // users should use Register instead, at least until clients have upgraded.
77
+ func RegisterV1 (s GRPCServer ) {
78
+ svr := NewServerV1 (ServerOptions {Services : s })
79
+ v1grpc .RegisterServerReflectionServer (s , svr )
69
80
}
70
81
71
82
// ServiceInfoProvider is an interface used to retrieve metadata about the
@@ -120,13 +131,27 @@ type ServerOptions struct {
120
131
121
132
// NewServer returns a reflection server implementation using the given options.
122
133
// This can be used to customize behavior of the reflection service. Most usages
123
- // should prefer to use Register instead.
134
+ // should prefer to use Register instead. For backwards compatibility reasons,
135
+ // this returns the v1alpha version of the reflection server. For a v1 version
136
+ // of the reflection server, see NewServerV1.
124
137
//
125
138
// # Experimental
126
139
//
127
140
// Notice: This function is EXPERIMENTAL and may be changed or removed in a
128
141
// later release.
129
142
func NewServer (opts ServerOptions ) v1alphagrpc.ServerReflectionServer {
143
+ return asV1Alpha (NewServerV1 (opts ))
144
+ }
145
+
146
+ // NewServerV1 returns a reflection server implementation using the given options.
147
+ // This can be used to customize behavior of the reflection service. Most usages
148
+ // should prefer to use Register instead.
149
+ //
150
+ // # Experimental
151
+ //
152
+ // Notice: This function is EXPERIMENTAL and may be changed or removed in a
153
+ // later release.
154
+ func NewServerV1 (opts ServerOptions ) v1grpc.ServerReflectionServer {
130
155
if opts .DescriptorResolver == nil {
131
156
opts .DescriptorResolver = protoregistry .GlobalFiles
132
157
}
@@ -215,11 +240,11 @@ func (s *serverReflectionServer) allExtensionNumbersForTypeName(name string) ([]
215
240
}
216
241
217
242
// listServices returns the names of services this server exposes.
218
- func (s * serverReflectionServer ) listServices () []* v1alphapb .ServiceResponse {
243
+ func (s * serverReflectionServer ) listServices () []* v1pb .ServiceResponse {
219
244
serviceInfo := s .s .GetServiceInfo ()
220
- resp := make ([]* v1alphapb .ServiceResponse , 0 , len (serviceInfo ))
245
+ resp := make ([]* v1pb .ServiceResponse , 0 , len (serviceInfo ))
221
246
for svc := range serviceInfo {
222
- resp = append (resp , & v1alphapb .ServiceResponse {Name : svc })
247
+ resp = append (resp , & v1pb .ServiceResponse {Name : svc })
223
248
}
224
249
sort .Slice (resp , func (i , j int ) bool {
225
250
return resp [i ].Name < resp [j ].Name
@@ -228,7 +253,7 @@ func (s *serverReflectionServer) listServices() []*v1alphapb.ServiceResponse {
228
253
}
229
254
230
255
// ServerReflectionInfo is the reflection service handler.
231
- func (s * serverReflectionServer ) ServerReflectionInfo (stream v1alphagrpc .ServerReflection_ServerReflectionInfoServer ) error {
256
+ func (s * serverReflectionServer ) ServerReflectionInfo (stream v1grpc .ServerReflection_ServerReflectionInfoServer ) error {
232
257
sentFileDescriptors := make (map [string ]bool )
233
258
for {
234
259
in , err := stream .Recv ()
@@ -239,79 +264,79 @@ func (s *serverReflectionServer) ServerReflectionInfo(stream v1alphagrpc.ServerR
239
264
return err
240
265
}
241
266
242
- out := & v1alphapb .ServerReflectionResponse {
267
+ out := & v1pb .ServerReflectionResponse {
243
268
ValidHost : in .Host ,
244
269
OriginalRequest : in ,
245
270
}
246
271
switch req := in .MessageRequest .(type ) {
247
- case * v1alphapb .ServerReflectionRequest_FileByFilename :
272
+ case * v1pb .ServerReflectionRequest_FileByFilename :
248
273
var b [][]byte
249
274
fd , err := s .descResolver .FindFileByPath (req .FileByFilename )
250
275
if err == nil {
251
276
b , err = s .fileDescWithDependencies (fd , sentFileDescriptors )
252
277
}
253
278
if err != nil {
254
- out .MessageResponse = & v1alphapb .ServerReflectionResponse_ErrorResponse {
255
- ErrorResponse : & v1alphapb .ErrorResponse {
279
+ out .MessageResponse = & v1pb .ServerReflectionResponse_ErrorResponse {
280
+ ErrorResponse : & v1pb .ErrorResponse {
256
281
ErrorCode : int32 (codes .NotFound ),
257
282
ErrorMessage : err .Error (),
258
283
},
259
284
}
260
285
} else {
261
- out .MessageResponse = & v1alphapb .ServerReflectionResponse_FileDescriptorResponse {
262
- FileDescriptorResponse : & v1alphapb .FileDescriptorResponse {FileDescriptorProto : b },
286
+ out .MessageResponse = & v1pb .ServerReflectionResponse_FileDescriptorResponse {
287
+ FileDescriptorResponse : & v1pb .FileDescriptorResponse {FileDescriptorProto : b },
263
288
}
264
289
}
265
- case * v1alphapb .ServerReflectionRequest_FileContainingSymbol :
290
+ case * v1pb .ServerReflectionRequest_FileContainingSymbol :
266
291
b , err := s .fileDescEncodingContainingSymbol (req .FileContainingSymbol , sentFileDescriptors )
267
292
if err != nil {
268
- out .MessageResponse = & v1alphapb .ServerReflectionResponse_ErrorResponse {
269
- ErrorResponse : & v1alphapb .ErrorResponse {
293
+ out .MessageResponse = & v1pb .ServerReflectionResponse_ErrorResponse {
294
+ ErrorResponse : & v1pb .ErrorResponse {
270
295
ErrorCode : int32 (codes .NotFound ),
271
296
ErrorMessage : err .Error (),
272
297
},
273
298
}
274
299
} else {
275
- out .MessageResponse = & v1alphapb .ServerReflectionResponse_FileDescriptorResponse {
276
- FileDescriptorResponse : & v1alphapb .FileDescriptorResponse {FileDescriptorProto : b },
300
+ out .MessageResponse = & v1pb .ServerReflectionResponse_FileDescriptorResponse {
301
+ FileDescriptorResponse : & v1pb .FileDescriptorResponse {FileDescriptorProto : b },
277
302
}
278
303
}
279
- case * v1alphapb .ServerReflectionRequest_FileContainingExtension :
304
+ case * v1pb .ServerReflectionRequest_FileContainingExtension :
280
305
typeName := req .FileContainingExtension .ContainingType
281
306
extNum := req .FileContainingExtension .ExtensionNumber
282
307
b , err := s .fileDescEncodingContainingExtension (typeName , extNum , sentFileDescriptors )
283
308
if err != nil {
284
- out .MessageResponse = & v1alphapb .ServerReflectionResponse_ErrorResponse {
285
- ErrorResponse : & v1alphapb .ErrorResponse {
309
+ out .MessageResponse = & v1pb .ServerReflectionResponse_ErrorResponse {
310
+ ErrorResponse : & v1pb .ErrorResponse {
286
311
ErrorCode : int32 (codes .NotFound ),
287
312
ErrorMessage : err .Error (),
288
313
},
289
314
}
290
315
} else {
291
- out .MessageResponse = & v1alphapb .ServerReflectionResponse_FileDescriptorResponse {
292
- FileDescriptorResponse : & v1alphapb .FileDescriptorResponse {FileDescriptorProto : b },
316
+ out .MessageResponse = & v1pb .ServerReflectionResponse_FileDescriptorResponse {
317
+ FileDescriptorResponse : & v1pb .FileDescriptorResponse {FileDescriptorProto : b },
293
318
}
294
319
}
295
- case * v1alphapb .ServerReflectionRequest_AllExtensionNumbersOfType :
320
+ case * v1pb .ServerReflectionRequest_AllExtensionNumbersOfType :
296
321
extNums , err := s .allExtensionNumbersForTypeName (req .AllExtensionNumbersOfType )
297
322
if err != nil {
298
- out .MessageResponse = & v1alphapb .ServerReflectionResponse_ErrorResponse {
299
- ErrorResponse : & v1alphapb .ErrorResponse {
323
+ out .MessageResponse = & v1pb .ServerReflectionResponse_ErrorResponse {
324
+ ErrorResponse : & v1pb .ErrorResponse {
300
325
ErrorCode : int32 (codes .NotFound ),
301
326
ErrorMessage : err .Error (),
302
327
},
303
328
}
304
329
} else {
305
- out .MessageResponse = & v1alphapb .ServerReflectionResponse_AllExtensionNumbersResponse {
306
- AllExtensionNumbersResponse : & v1alphapb .ExtensionNumberResponse {
330
+ out .MessageResponse = & v1pb .ServerReflectionResponse_AllExtensionNumbersResponse {
331
+ AllExtensionNumbersResponse : & v1pb .ExtensionNumberResponse {
307
332
BaseTypeName : req .AllExtensionNumbersOfType ,
308
333
ExtensionNumber : extNums ,
309
334
},
310
335
}
311
336
}
312
- case * v1alphapb .ServerReflectionRequest_ListServices :
313
- out .MessageResponse = & v1alphapb .ServerReflectionResponse_ListServicesResponse {
314
- ListServicesResponse : & v1alphapb .ListServiceResponse {
337
+ case * v1pb .ServerReflectionRequest_ListServices :
338
+ out .MessageResponse = & v1pb .ServerReflectionResponse_ListServicesResponse {
339
+ ListServicesResponse : & v1pb .ListServiceResponse {
315
340
Service : s .listServices (),
316
341
},
317
342
}
0 commit comments