| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 1 | package grpcreflect |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 5 | "context" |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 6 | "fmt" |
| 7 | "io" |
| 8 | "reflect" |
| 9 | "runtime" |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 10 | "sort" |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 11 | "sync" |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 12 | "sync/atomic" |
| 13 | "time" |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 14 | |
| 15 | "github.com/golang/protobuf/proto" |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 16 | "google.golang.org/grpc" |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 17 | "google.golang.org/grpc/codes" |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 18 | refv1 "google.golang.org/grpc/reflection/grpc_reflection_v1" |
| 19 | refv1alpha "google.golang.org/grpc/reflection/grpc_reflection_v1alpha" |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 20 | "google.golang.org/grpc/status" |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 21 | "google.golang.org/protobuf/reflect/protodesc" |
| 22 | "google.golang.org/protobuf/reflect/protoreflect" |
| 23 | "google.golang.org/protobuf/reflect/protoregistry" |
| 24 | "google.golang.org/protobuf/types/descriptorpb" |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 25 | |
| 26 | "github.com/jhump/protoreflect/desc" |
| 27 | "github.com/jhump/protoreflect/internal" |
| 28 | ) |
| 29 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 30 | // If we try the v1 reflection API and get back "not implemented", we'll wait |
| 31 | // this long before trying v1 again. This allows a long-lived client to |
| 32 | // dynamically switch from v1alpha to v1 if the underlying server is updated |
| 33 | // to support it. But it also prevents every stream request from always trying |
| 34 | // v1 first: if we try it and see it fail, we shouldn't continually retry it |
| 35 | // if we expect it will fail again. |
| 36 | const durationBetweenV1Attempts = time.Hour |
| 37 | |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 38 | // elementNotFoundError is the error returned by reflective operations where the |
| 39 | // server does not recognize a given file name, symbol name, or extension. |
| 40 | type elementNotFoundError struct { |
| 41 | name string |
| 42 | kind elementKind |
| 43 | symType symbolType // only used when kind == elementKindSymbol |
| 44 | tag int32 // only used when kind == elementKindExtension |
| 45 | |
| 46 | // only errors with a kind of elementKindFile will have a cause, which means |
| 47 | // the named file count not be resolved because of a dependency that could |
| 48 | // not be found where cause describes the missing dependency |
| 49 | cause *elementNotFoundError |
| 50 | } |
| 51 | |
| 52 | type elementKind int |
| 53 | |
| 54 | const ( |
| 55 | elementKindSymbol elementKind = iota |
| 56 | elementKindFile |
| 57 | elementKindExtension |
| 58 | ) |
| 59 | |
| 60 | type symbolType string |
| 61 | |
| 62 | const ( |
| 63 | symbolTypeService = "Service" |
| 64 | symbolTypeMessage = "Message" |
| 65 | symbolTypeEnum = "Enum" |
| 66 | symbolTypeUnknown = "Symbol" |
| 67 | ) |
| 68 | |
| 69 | func symbolNotFound(symbol string, symType symbolType, cause *elementNotFoundError) error { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 70 | if cause != nil && cause.kind == elementKindSymbol && cause.name == symbol { |
| 71 | // no need to wrap |
| 72 | if symType != symbolTypeUnknown && cause.symType == symbolTypeUnknown { |
| 73 | // We previously didn't know symbol type but now do? |
| 74 | // Create a new error that has the right symbol type. |
| 75 | return &elementNotFoundError{name: symbol, symType: symType, kind: elementKindSymbol} |
| 76 | } |
| 77 | return cause |
| 78 | } |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 79 | return &elementNotFoundError{name: symbol, symType: symType, kind: elementKindSymbol, cause: cause} |
| 80 | } |
| 81 | |
| 82 | func extensionNotFound(extendee string, tag int32, cause *elementNotFoundError) error { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 83 | if cause != nil && cause.kind == elementKindExtension && cause.name == extendee && cause.tag == tag { |
| 84 | // no need to wrap |
| 85 | return cause |
| 86 | } |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 87 | return &elementNotFoundError{name: extendee, tag: tag, kind: elementKindExtension, cause: cause} |
| 88 | } |
| 89 | |
| 90 | func fileNotFound(file string, cause *elementNotFoundError) error { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 91 | if cause != nil && cause.kind == elementKindFile && cause.name == file { |
| 92 | // no need to wrap |
| 93 | return cause |
| 94 | } |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 95 | return &elementNotFoundError{name: file, kind: elementKindFile, cause: cause} |
| 96 | } |
| 97 | |
| 98 | func (e *elementNotFoundError) Error() string { |
| 99 | first := true |
| 100 | var b bytes.Buffer |
| 101 | for ; e != nil; e = e.cause { |
| 102 | if first { |
| 103 | first = false |
| 104 | } else { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 105 | _, _ = fmt.Fprint(&b, "\ncaused by: ") |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 106 | } |
| 107 | switch e.kind { |
| 108 | case elementKindSymbol: |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 109 | _, _ = fmt.Fprintf(&b, "%s not found: %s", e.symType, e.name) |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 110 | case elementKindExtension: |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 111 | _, _ = fmt.Fprintf(&b, "Extension not found: tag %d for %s", e.tag, e.name) |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 112 | default: |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 113 | _, _ = fmt.Fprintf(&b, "File not found: %s", e.name) |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | return b.String() |
| 117 | } |
| 118 | |
| 119 | // IsElementNotFoundError determines if the given error indicates that a file |
| 120 | // name, symbol name, or extension field was could not be found by the server. |
| 121 | func IsElementNotFoundError(err error) bool { |
| 122 | _, ok := err.(*elementNotFoundError) |
| 123 | return ok |
| 124 | } |
| 125 | |
| 126 | // ProtocolError is an error returned when the server sends a response of the |
| 127 | // wrong type. |
| 128 | type ProtocolError struct { |
| 129 | missingType reflect.Type |
| 130 | } |
| 131 | |
| 132 | func (p ProtocolError) Error() string { |
| 133 | return fmt.Sprintf("Protocol error: response was missing %v", p.missingType) |
| 134 | } |
| 135 | |
| 136 | type extDesc struct { |
| 137 | extendedMessageName string |
| 138 | extensionNumber int32 |
| 139 | } |
| 140 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 141 | type resolvers struct { |
| 142 | descriptorResolver protodesc.Resolver |
| 143 | extensionResolver protoregistry.ExtensionTypeResolver |
| 144 | } |
| 145 | |
| 146 | type fileEntry struct { |
| 147 | fd *desc.FileDescriptor |
| 148 | fallback bool |
| 149 | } |
| 150 | |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 151 | // Client is a client connection to a server for performing reflection calls |
| 152 | // and resolving remote symbols. |
| 153 | type Client struct { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 154 | ctx context.Context |
| 155 | now func() time.Time |
| 156 | stubV1 refv1.ServerReflectionClient |
| 157 | stubV1Alpha refv1alpha.ServerReflectionClient |
| 158 | allowMissing atomic.Bool |
| 159 | fallbackResolver atomic.Pointer[resolvers] |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 160 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 161 | connMu sync.Mutex |
| 162 | cancel context.CancelFunc |
| 163 | stream refv1.ServerReflection_ServerReflectionInfoClient |
| 164 | useV1Alpha bool |
| 165 | lastTriedV1 time.Time |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 166 | |
| 167 | cacheMu sync.RWMutex |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 168 | protosByName map[string]*descriptorpb.FileDescriptorProto |
| 169 | filesByName map[string]fileEntry |
| 170 | filesBySymbol map[string]fileEntry |
| 171 | filesByExtension map[extDesc]fileEntry |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | // NewClient creates a new Client with the given root context and using the |
| 175 | // given RPC stub for talking to the server. |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 176 | // |
| 177 | // Deprecated: Use NewClientV1Alpha if you are intentionally pinning the |
| 178 | // v1alpha version of the reflection service. Otherwise, use NewClientAuto |
| 179 | // instead. |
| 180 | func NewClient(ctx context.Context, stub refv1alpha.ServerReflectionClient) *Client { |
| 181 | return NewClientV1Alpha(ctx, stub) |
| 182 | } |
| 183 | |
| 184 | // NewClientV1Alpha creates a new Client using the v1alpha version of reflection |
| 185 | // with the given root context and using the given RPC stub for talking to the |
| 186 | // server. |
| 187 | func NewClientV1Alpha(ctx context.Context, stub refv1alpha.ServerReflectionClient) *Client { |
| 188 | return newClient(ctx, nil, stub) |
| 189 | } |
| 190 | |
| 191 | // NewClientV1 creates a new Client using the v1 version of reflection with the |
| 192 | // given root context and using the given RPC stub for talking to the server. |
| 193 | func NewClientV1(ctx context.Context, stub refv1.ServerReflectionClient) *Client { |
| 194 | return newClient(ctx, stub, nil) |
| 195 | } |
| 196 | |
| 197 | func newClient(ctx context.Context, stubv1 refv1.ServerReflectionClient, stubv1alpha refv1alpha.ServerReflectionClient) *Client { |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 198 | cr := &Client{ |
| 199 | ctx: ctx, |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 200 | now: time.Now, |
| 201 | stubV1: stubv1, |
| 202 | stubV1Alpha: stubv1alpha, |
| 203 | protosByName: map[string]*descriptorpb.FileDescriptorProto{}, |
| 204 | filesByName: map[string]fileEntry{}, |
| 205 | filesBySymbol: map[string]fileEntry{}, |
| 206 | filesByExtension: map[extDesc]fileEntry{}, |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 207 | } |
| 208 | // don't leak a grpc stream |
| 209 | runtime.SetFinalizer(cr, (*Client).Reset) |
| 210 | return cr |
| 211 | } |
| 212 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 213 | // NewClientAuto creates a new Client that will use either v1 or v1alpha version |
| 214 | // of reflection (based on what the server supports) with the given root context |
| 215 | // and using the given client connection. |
| 216 | // |
| 217 | // It will first the v1 version of the reflection service. If it gets back an |
| 218 | // "Unimplemented" error, it will fall back to using the v1alpha version. It |
| 219 | // will remember which version the server supports for any subsequent operations |
| 220 | // that need to re-invoke the streaming RPC. But, if it's a very long-lived |
| 221 | // client, it will periodically retry the v1 version (in case the server is |
| 222 | // updated to support it also). The period for these retries is every hour. |
| 223 | func NewClientAuto(ctx context.Context, cc grpc.ClientConnInterface) *Client { |
| 224 | stubv1 := refv1.NewServerReflectionClient(cc) |
| 225 | stubv1alpha := refv1alpha.NewServerReflectionClient(cc) |
| 226 | return newClient(ctx, stubv1, stubv1alpha) |
| 227 | } |
| 228 | |
| 229 | // AllowMissingFileDescriptors configures the client to allow missing files |
| 230 | // when building descriptors when possible. Missing files are often fatal |
| 231 | // errors, but with this option they can sometimes be worked around. Building |
| 232 | // a schema can only succeed with some files missing if the files in question |
| 233 | // only provide custom options and/or other unused types. |
| 234 | func (cr *Client) AllowMissingFileDescriptors() { |
| 235 | cr.allowMissing.Store(true) |
| 236 | } |
| 237 | |
| 238 | // AllowFallbackResolver configures the client to allow falling back to the |
| 239 | // given resolvers if the server is unable to supply descriptors for a particular |
| 240 | // query. This allows working around issues where servers' reflection service |
| 241 | // provides an incomplete set of descriptors, but the client has knowledge of |
| 242 | // the missing descriptors from another source. It is usually most appropriate |
| 243 | // to pass [protoregistry.GlobalFiles] and [protoregistry.GlobalTypes] as the |
| 244 | // resolver values. |
| 245 | // |
| 246 | // The first value is used as a fallback for FileByFilename and FileContainingSymbol |
| 247 | // queries. The second value is used as a fallback for FileContainingExtension. It |
| 248 | // can also be used as a fallback for AllExtensionNumbersForType if it provides |
| 249 | // a method with the following signature (which *[protoregistry.Types] provides): |
| 250 | // |
| 251 | // RangeExtensionsByMessage(message protoreflect.FullName, f func(protoreflect.ExtensionType) bool) |
| 252 | func (cr *Client) AllowFallbackResolver(descriptors protodesc.Resolver, exts protoregistry.ExtensionTypeResolver) { |
| 253 | if descriptors == nil && exts == nil { |
| 254 | cr.fallbackResolver.Store(nil) |
| 255 | } else { |
| 256 | cr.fallbackResolver.Store(&resolvers{ |
| 257 | descriptorResolver: descriptors, |
| 258 | extensionResolver: exts, |
| 259 | }) |
| 260 | } |
| 261 | } |
| 262 | |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 263 | // FileByFilename asks the server for a file descriptor for the proto file with |
| 264 | // the given name. |
| 265 | func (cr *Client) FileByFilename(filename string) (*desc.FileDescriptor, error) { |
| 266 | // hit the cache first |
| 267 | cr.cacheMu.RLock() |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 268 | if entry, ok := cr.filesByName[filename]; ok { |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 269 | cr.cacheMu.RUnlock() |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 270 | return entry.fd, nil |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 271 | } |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 272 | // not there? see if we've downloaded the proto |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 273 | fdp, ok := cr.protosByName[filename] |
| 274 | cr.cacheMu.RUnlock() |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 275 | if ok { |
| 276 | return cr.descriptorFromProto(fdp) |
| 277 | } |
| 278 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 279 | req := &refv1.ServerReflectionRequest{ |
| 280 | MessageRequest: &refv1.ServerReflectionRequest_FileByFilename{ |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 281 | FileByFilename: filename, |
| 282 | }, |
| 283 | } |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 284 | accept := func(fd *desc.FileDescriptor) bool { |
| 285 | return fd.GetName() == filename |
| 286 | } |
| 287 | |
| 288 | fd, err := cr.getAndCacheFileDescriptors(req, filename, "", accept) |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 289 | if isNotFound(err) { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 290 | // File not found? see if we can look up via alternate name |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 291 | if alternate, ok := internal.StdFileAliases[filename]; ok { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 292 | req := &refv1.ServerReflectionRequest{ |
| 293 | MessageRequest: &refv1.ServerReflectionRequest_FileByFilename{ |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 294 | FileByFilename: alternate, |
| 295 | }, |
| 296 | } |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 297 | fd, err = cr.getAndCacheFileDescriptors(req, alternate, filename, accept) |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 298 | } |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 299 | } |
| 300 | if isNotFound(err) { |
| 301 | // Still no? See if we can use a fallback resolver |
| 302 | resolver := cr.fallbackResolver.Load() |
| 303 | if resolver != nil && resolver.descriptorResolver != nil { |
| 304 | fileDesc, fallbackErr := resolver.descriptorResolver.FindFileByPath(filename) |
| 305 | if fallbackErr == nil { |
| 306 | var wrapErr error |
| 307 | fd, wrapErr = desc.WrapFile(fileDesc) |
| 308 | if wrapErr == nil { |
| 309 | fd = cr.cacheFile(fd, true) |
| 310 | err = nil // clear error since we've succeeded via the fallback |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | if isNotFound(err) { |
| 316 | err = fileNotFound(filename, nil) |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 317 | } else if e, ok := err.(*elementNotFoundError); ok { |
| 318 | err = fileNotFound(filename, e) |
| 319 | } |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 320 | |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 321 | return fd, err |
| 322 | } |
| 323 | |
| 324 | // FileContainingSymbol asks the server for a file descriptor for the proto file |
| 325 | // that declares the given fully-qualified symbol. |
| 326 | func (cr *Client) FileContainingSymbol(symbol string) (*desc.FileDescriptor, error) { |
| 327 | // hit the cache first |
| 328 | cr.cacheMu.RLock() |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 329 | entry, ok := cr.filesBySymbol[symbol] |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 330 | cr.cacheMu.RUnlock() |
| 331 | if ok { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 332 | return entry.fd, nil |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 333 | } |
| 334 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 335 | req := &refv1.ServerReflectionRequest{ |
| 336 | MessageRequest: &refv1.ServerReflectionRequest_FileContainingSymbol{ |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 337 | FileContainingSymbol: symbol, |
| 338 | }, |
| 339 | } |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 340 | accept := func(fd *desc.FileDescriptor) bool { |
| 341 | return fd.FindSymbol(symbol) != nil |
| 342 | } |
| 343 | fd, err := cr.getAndCacheFileDescriptors(req, "", "", accept) |
| 344 | if isNotFound(err) { |
| 345 | // Symbol not found? See if we can use a fallback resolver |
| 346 | resolver := cr.fallbackResolver.Load() |
| 347 | if resolver != nil && resolver.descriptorResolver != nil { |
| 348 | d, fallbackErr := resolver.descriptorResolver.FindDescriptorByName(protoreflect.FullName(symbol)) |
| 349 | if fallbackErr == nil { |
| 350 | var wrapErr error |
| 351 | fd, wrapErr = desc.WrapFile(d.ParentFile()) |
| 352 | if wrapErr == nil { |
| 353 | fd = cr.cacheFile(fd, true) |
| 354 | err = nil // clear error since we've succeeded via the fallback |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | } |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 359 | if isNotFound(err) { |
| 360 | err = symbolNotFound(symbol, symbolTypeUnknown, nil) |
| 361 | } else if e, ok := err.(*elementNotFoundError); ok { |
| 362 | err = symbolNotFound(symbol, symbolTypeUnknown, e) |
| 363 | } |
| 364 | return fd, err |
| 365 | } |
| 366 | |
| 367 | // FileContainingExtension asks the server for a file descriptor for the proto |
| 368 | // file that declares an extension with the given number for the given |
| 369 | // fully-qualified message name. |
| 370 | func (cr *Client) FileContainingExtension(extendedMessageName string, extensionNumber int32) (*desc.FileDescriptor, error) { |
| 371 | // hit the cache first |
| 372 | cr.cacheMu.RLock() |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 373 | entry, ok := cr.filesByExtension[extDesc{extendedMessageName, extensionNumber}] |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 374 | cr.cacheMu.RUnlock() |
| 375 | if ok { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 376 | return entry.fd, nil |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 377 | } |
| 378 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 379 | req := &refv1.ServerReflectionRequest{ |
| 380 | MessageRequest: &refv1.ServerReflectionRequest_FileContainingExtension{ |
| 381 | FileContainingExtension: &refv1.ExtensionRequest{ |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 382 | ContainingType: extendedMessageName, |
| 383 | ExtensionNumber: extensionNumber, |
| 384 | }, |
| 385 | }, |
| 386 | } |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 387 | accept := func(fd *desc.FileDescriptor) bool { |
| 388 | return fd.FindExtension(extendedMessageName, extensionNumber) != nil |
| 389 | } |
| 390 | fd, err := cr.getAndCacheFileDescriptors(req, "", "", accept) |
| 391 | if isNotFound(err) { |
| 392 | // Extension not found? See if we can use a fallback resolver |
| 393 | resolver := cr.fallbackResolver.Load() |
| 394 | if resolver != nil && resolver.extensionResolver != nil { |
| 395 | extType, fallbackErr := resolver.extensionResolver.FindExtensionByNumber(protoreflect.FullName(extendedMessageName), protoreflect.FieldNumber(extensionNumber)) |
| 396 | if fallbackErr == nil { |
| 397 | var wrapErr error |
| 398 | fd, wrapErr = desc.WrapFile(extType.TypeDescriptor().ParentFile()) |
| 399 | if wrapErr == nil { |
| 400 | fd = cr.cacheFile(fd, true) |
| 401 | err = nil // clear error since we've succeeded via the fallback |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | } |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 406 | if isNotFound(err) { |
| 407 | err = extensionNotFound(extendedMessageName, extensionNumber, nil) |
| 408 | } else if e, ok := err.(*elementNotFoundError); ok { |
| 409 | err = extensionNotFound(extendedMessageName, extensionNumber, e) |
| 410 | } |
| 411 | return fd, err |
| 412 | } |
| 413 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 414 | func (cr *Client) getAndCacheFileDescriptors(req *refv1.ServerReflectionRequest, expectedName, alias string, accept func(*desc.FileDescriptor) bool) (*desc.FileDescriptor, error) { |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 415 | resp, err := cr.send(req) |
| 416 | if err != nil { |
| 417 | return nil, err |
| 418 | } |
| 419 | |
| 420 | fdResp := resp.GetFileDescriptorResponse() |
| 421 | if fdResp == nil { |
| 422 | return nil, &ProtocolError{reflect.TypeOf(fdResp).Elem()} |
| 423 | } |
| 424 | |
| 425 | // Response can contain the result file descriptor, but also its transitive |
| 426 | // deps. Furthermore, protocol states that subsequent requests do not need |
| 427 | // to send transitive deps that have been sent in prior responses. So we |
| 428 | // need to cache all file descriptors and then return the first one (which |
| 429 | // should be the answer). If we're looking for a file by name, we can be |
| 430 | // smarter and make sure to grab one by name instead of just grabbing the |
| 431 | // first one. |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 432 | var fds []*descriptorpb.FileDescriptorProto |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 433 | for _, fdBytes := range fdResp.FileDescriptorProto { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 434 | fd := &descriptorpb.FileDescriptorProto{} |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 435 | if err = proto.Unmarshal(fdBytes, fd); err != nil { |
| 436 | return nil, err |
| 437 | } |
| 438 | |
| 439 | if expectedName != "" && alias != "" && expectedName != alias && fd.GetName() == expectedName { |
| 440 | // we found a file was aliased, so we need to update the proto to reflect that |
| 441 | fd.Name = proto.String(alias) |
| 442 | } |
| 443 | |
| 444 | cr.cacheMu.Lock() |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 445 | // store in cache of raw descriptor protos, but don't overwrite existing protos |
| 446 | if existingFd, ok := cr.protosByName[fd.GetName()]; ok { |
| 447 | fd = existingFd |
| 448 | } else { |
| 449 | cr.protosByName[fd.GetName()] = fd |
| 450 | } |
| 451 | cr.cacheMu.Unlock() |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 452 | |
| 453 | fds = append(fds, fd) |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 454 | } |
| 455 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 456 | // find the right result from the files returned |
| 457 | for _, fd := range fds { |
| 458 | result, err := cr.descriptorFromProto(fd) |
| 459 | if err != nil { |
| 460 | return nil, err |
| 461 | } |
| 462 | if accept(result) { |
| 463 | return result, nil |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | return nil, status.Errorf(codes.NotFound, "response does not include expected file") |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 468 | } |
| 469 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 470 | func (cr *Client) descriptorFromProto(fd *descriptorpb.FileDescriptorProto) (*desc.FileDescriptor, error) { |
| 471 | allowMissing := cr.allowMissing.Load() |
| 472 | deps := make([]*desc.FileDescriptor, 0, len(fd.GetDependency())) |
| 473 | var deferredErr error |
| 474 | var missingDeps []int |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 475 | for i, depName := range fd.GetDependency() { |
| 476 | if dep, err := cr.FileByFilename(depName); err != nil { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 477 | if _, ok := err.(*elementNotFoundError); !ok || !allowMissing { |
| 478 | return nil, err |
| 479 | } |
| 480 | // We'll ignore for now to see if the file is really necessary. |
| 481 | // (If it only supplies custom options, we can get by without it.) |
| 482 | if deferredErr == nil { |
| 483 | deferredErr = err |
| 484 | } |
| 485 | missingDeps = append(missingDeps, i) |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 486 | } else { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 487 | deps = append(deps, dep) |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 488 | } |
| 489 | } |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 490 | if len(missingDeps) > 0 { |
| 491 | fd = fileWithoutDeps(fd, missingDeps) |
| 492 | } |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 493 | d, err := desc.CreateFileDescriptor(fd, deps...) |
| 494 | if err != nil { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 495 | if deferredErr != nil { |
| 496 | // assume the issue is the missing dep |
| 497 | return nil, deferredErr |
| 498 | } |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 499 | return nil, err |
| 500 | } |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 501 | d = cr.cacheFile(d, false) |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 502 | return d, nil |
| 503 | } |
| 504 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 505 | func (cr *Client) cacheFile(fd *desc.FileDescriptor, fallback bool) *desc.FileDescriptor { |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 506 | cr.cacheMu.Lock() |
| 507 | defer cr.cacheMu.Unlock() |
| 508 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 509 | // Cache file descriptor by name. If we can't overwrite an existing |
| 510 | // entry, return it. (Existing entry could come from concurrent caller.) |
| 511 | if existing, ok := cr.filesByName[fd.GetName()]; ok && !canOverwrite(existing, fallback) { |
| 512 | return existing.fd |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 513 | } |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 514 | entry := fileEntry{fd: fd, fallback: fallback} |
| 515 | cr.filesByName[fd.GetName()] = entry |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 516 | |
| 517 | // also cache by symbols and extensions |
| 518 | for _, m := range fd.GetMessageTypes() { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 519 | cr.cacheMessageLocked(m, entry) |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 520 | } |
| 521 | for _, e := range fd.GetEnumTypes() { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 522 | if !cr.maybeCacheFileBySymbol(e.GetFullyQualifiedName(), entry) { |
| 523 | continue |
| 524 | } |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 525 | for _, v := range e.GetValues() { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 526 | cr.maybeCacheFileBySymbol(v.GetFullyQualifiedName(), entry) |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | for _, e := range fd.GetExtensions() { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 530 | if !cr.maybeCacheFileBySymbol(e.GetFullyQualifiedName(), entry) { |
| 531 | continue |
| 532 | } |
| 533 | cr.maybeCacheFileByExtension(extDesc{e.GetOwner().GetFullyQualifiedName(), e.GetNumber()}, entry) |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 534 | } |
| 535 | for _, s := range fd.GetServices() { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 536 | if !cr.maybeCacheFileBySymbol(s.GetFullyQualifiedName(), entry) { |
| 537 | continue |
| 538 | } |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 539 | for _, m := range s.GetMethods() { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 540 | cr.maybeCacheFileBySymbol(m.GetFullyQualifiedName(), entry) |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
| 544 | return fd |
| 545 | } |
| 546 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 547 | func (cr *Client) cacheMessageLocked(md *desc.MessageDescriptor, entry fileEntry) { |
| 548 | if !cr.maybeCacheFileBySymbol(md.GetFullyQualifiedName(), entry) { |
| 549 | return |
| 550 | } |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 551 | for _, f := range md.GetFields() { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 552 | cr.maybeCacheFileBySymbol(f.GetFullyQualifiedName(), entry) |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 553 | } |
| 554 | for _, o := range md.GetOneOfs() { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 555 | cr.maybeCacheFileBySymbol(o.GetFullyQualifiedName(), entry) |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 556 | } |
| 557 | for _, e := range md.GetNestedEnumTypes() { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 558 | if !cr.maybeCacheFileBySymbol(e.GetFullyQualifiedName(), entry) { |
| 559 | continue |
| 560 | } |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 561 | for _, v := range e.GetValues() { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 562 | cr.maybeCacheFileBySymbol(v.GetFullyQualifiedName(), entry) |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | for _, e := range md.GetNestedExtensions() { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 566 | if !cr.maybeCacheFileBySymbol(e.GetFullyQualifiedName(), entry) { |
| 567 | continue |
| 568 | } |
| 569 | cr.maybeCacheFileByExtension(extDesc{e.GetOwner().GetFullyQualifiedName(), e.GetNumber()}, entry) |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 570 | } |
| 571 | for _, m := range md.GetNestedMessageTypes() { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 572 | cr.cacheMessageLocked(m, entry) // recurse |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 576 | func canOverwrite(existing fileEntry, fallback bool) bool { |
| 577 | return !fallback && existing.fallback |
| 578 | } |
| 579 | |
| 580 | func (cr *Client) maybeCacheFileBySymbol(symbol string, entry fileEntry) bool { |
| 581 | existing, ok := cr.filesBySymbol[symbol] |
| 582 | if ok && !canOverwrite(existing, entry.fallback) { |
| 583 | return false |
| 584 | } |
| 585 | cr.filesBySymbol[symbol] = entry |
| 586 | return true |
| 587 | } |
| 588 | |
| 589 | func (cr *Client) maybeCacheFileByExtension(ext extDesc, entry fileEntry) { |
| 590 | existing, ok := cr.filesByExtension[ext] |
| 591 | if ok && !canOverwrite(existing, entry.fallback) { |
| 592 | return |
| 593 | } |
| 594 | cr.filesByExtension[ext] = entry |
| 595 | } |
| 596 | |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 597 | // AllExtensionNumbersForType asks the server for all known extension numbers |
| 598 | // for the given fully-qualified message name. |
| 599 | func (cr *Client) AllExtensionNumbersForType(extendedMessageName string) ([]int32, error) { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 600 | req := &refv1.ServerReflectionRequest{ |
| 601 | MessageRequest: &refv1.ServerReflectionRequest_AllExtensionNumbersOfType{ |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 602 | AllExtensionNumbersOfType: extendedMessageName, |
| 603 | }, |
| 604 | } |
| 605 | resp, err := cr.send(req) |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 606 | var exts []int32 |
| 607 | if err != nil && !isNotFound(err) { |
| 608 | // If the server doesn't know about the message type and returns "not found", |
| 609 | // we'll treat that as "no known extensions" instead of returning an error. |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 610 | return nil, err |
| 611 | } |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 612 | if err == nil { |
| 613 | extResp := resp.GetAllExtensionNumbersResponse() |
| 614 | if extResp == nil { |
| 615 | return nil, &ProtocolError{reflect.TypeOf(extResp).Elem()} |
| 616 | } |
| 617 | exts = extResp.ExtensionNumber |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 618 | } |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 619 | |
| 620 | resolver := cr.fallbackResolver.Load() |
| 621 | if resolver != nil && resolver.extensionResolver != nil { |
| 622 | type extRanger interface { |
| 623 | RangeExtensionsByMessage(message protoreflect.FullName, f func(protoreflect.ExtensionType) bool) |
| 624 | } |
| 625 | if ranger, ok := resolver.extensionResolver.(extRanger); ok { |
| 626 | // Merge results with fallback resolver |
| 627 | extSet := map[int32]struct{}{} |
| 628 | ranger.RangeExtensionsByMessage(protoreflect.FullName(extendedMessageName), func(extType protoreflect.ExtensionType) bool { |
| 629 | extSet[int32(extType.TypeDescriptor().Number())] = struct{}{} |
| 630 | return true |
| 631 | }) |
| 632 | if len(extSet) > 0 { |
| 633 | // De-dupe with the set of extension numbers we got |
| 634 | // from the server and merge the results back into exts. |
| 635 | for _, ext := range exts { |
| 636 | extSet[ext] = struct{}{} |
| 637 | } |
| 638 | exts = make([]int32, 0, len(extSet)) |
| 639 | for ext := range extSet { |
| 640 | exts = append(exts, ext) |
| 641 | } |
| 642 | sort.Slice(exts, func(i, j int) bool { return exts[i] < exts[j] }) |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | return exts, nil |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | // ListServices asks the server for the fully-qualified names of all exposed |
| 650 | // services. |
| 651 | func (cr *Client) ListServices() ([]string, error) { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 652 | req := &refv1.ServerReflectionRequest{ |
| 653 | MessageRequest: &refv1.ServerReflectionRequest_ListServices{ |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 654 | // proto doesn't indicate any purpose for this value and server impl |
| 655 | // doesn't actually use it... |
| 656 | ListServices: "*", |
| 657 | }, |
| 658 | } |
| 659 | resp, err := cr.send(req) |
| 660 | if err != nil { |
| 661 | return nil, err |
| 662 | } |
| 663 | |
| 664 | listResp := resp.GetListServicesResponse() |
| 665 | if listResp == nil { |
| 666 | return nil, &ProtocolError{reflect.TypeOf(listResp).Elem()} |
| 667 | } |
| 668 | serviceNames := make([]string, len(listResp.Service)) |
| 669 | for i, s := range listResp.Service { |
| 670 | serviceNames[i] = s.Name |
| 671 | } |
| 672 | return serviceNames, nil |
| 673 | } |
| 674 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 675 | func (cr *Client) send(req *refv1.ServerReflectionRequest) (*refv1.ServerReflectionResponse, error) { |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 676 | // we allow one immediate retry, in case we have a stale stream |
| 677 | // (e.g. closed by server) |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 678 | resp, err := cr.doSend(req) |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 679 | if err != nil { |
| 680 | return nil, err |
| 681 | } |
| 682 | |
| 683 | // convert error response messages into errors |
| 684 | errResp := resp.GetErrorResponse() |
| 685 | if errResp != nil { |
| 686 | return nil, status.Errorf(codes.Code(errResp.ErrorCode), "%s", errResp.ErrorMessage) |
| 687 | } |
| 688 | |
| 689 | return resp, nil |
| 690 | } |
| 691 | |
| 692 | func isNotFound(err error) bool { |
| 693 | if err == nil { |
| 694 | return false |
| 695 | } |
| 696 | s, ok := status.FromError(err) |
| 697 | return ok && s.Code() == codes.NotFound |
| 698 | } |
| 699 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 700 | func (cr *Client) doSend(req *refv1.ServerReflectionRequest) (*refv1.ServerReflectionResponse, error) { |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 701 | // TODO: Streams are thread-safe, so we shouldn't need to lock. But without locking, we'll need more machinery |
| 702 | // (goroutines and channels) to ensure that responses are correctly correlated with their requests and thus |
| 703 | // delivered in correct oder. |
| 704 | cr.connMu.Lock() |
| 705 | defer cr.connMu.Unlock() |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 706 | return cr.doSendLocked(0, nil, req) |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 707 | } |
| 708 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 709 | func (cr *Client) doSendLocked(attemptCount int, prevErr error, req *refv1.ServerReflectionRequest) (*refv1.ServerReflectionResponse, error) { |
| 710 | if attemptCount >= 3 && prevErr != nil { |
| 711 | return nil, prevErr |
| 712 | } |
| 713 | if (status.Code(prevErr) == codes.Unimplemented || |
| 714 | status.Code(prevErr) == codes.Unavailable) && |
| 715 | cr.useV1() { |
| 716 | // If v1 is unimplemented, fallback to v1alpha. |
| 717 | // We also fallback on unavailable because some servers have been |
| 718 | // observed to close the connection/cancel the stream, w/out sending |
| 719 | // back status or headers, when the service name is not known. When |
| 720 | // this happens, the RPC status code is unavailable. |
| 721 | // See https://github.com/fullstorydev/grpcurl/issues/434 |
| 722 | cr.useV1Alpha = true |
| 723 | cr.lastTriedV1 = cr.now() |
| 724 | } |
| 725 | attemptCount++ |
| 726 | |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 727 | if err := cr.initStreamLocked(); err != nil { |
| 728 | return nil, err |
| 729 | } |
| 730 | |
| 731 | if err := cr.stream.Send(req); err != nil { |
| 732 | if err == io.EOF { |
| 733 | // if send returns EOF, must call Recv to get real underlying error |
| 734 | _, err = cr.stream.Recv() |
| 735 | } |
| 736 | cr.resetLocked() |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 737 | return cr.doSendLocked(attemptCount, err, req) |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 738 | } |
| 739 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 740 | resp, err := cr.stream.Recv() |
| 741 | if err != nil { |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 742 | cr.resetLocked() |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 743 | return cr.doSendLocked(attemptCount, err, req) |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 744 | } |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 745 | return resp, nil |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | func (cr *Client) initStreamLocked() error { |
| 749 | if cr.stream != nil { |
| 750 | return nil |
| 751 | } |
| 752 | var newCtx context.Context |
| 753 | newCtx, cr.cancel = context.WithCancel(cr.ctx) |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 754 | if cr.useV1Alpha == true && cr.now().Sub(cr.lastTriedV1) > durationBetweenV1Attempts { |
| 755 | // we're due for periodic retry of v1 |
| 756 | cr.useV1Alpha = false |
| 757 | } |
| 758 | if cr.useV1() { |
| 759 | // try the v1 API |
| 760 | streamv1, err := cr.stubV1.ServerReflectionInfo(newCtx) |
| 761 | if err == nil { |
| 762 | cr.stream = streamv1 |
| 763 | return nil |
| 764 | } |
| 765 | if status.Code(err) != codes.Unimplemented { |
| 766 | return err |
| 767 | } |
| 768 | // oh well, fall through below to try v1alpha and update state |
| 769 | // so we skip straight to v1alpha next time |
| 770 | cr.useV1Alpha = true |
| 771 | cr.lastTriedV1 = cr.now() |
| 772 | } |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 773 | var err error |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 774 | streamv1alpha, err := cr.stubV1Alpha.ServerReflectionInfo(newCtx) |
| 775 | if err == nil { |
| 776 | cr.stream = adaptStreamFromV1Alpha{streamv1alpha} |
| 777 | return nil |
| 778 | } |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 779 | return err |
| 780 | } |
| 781 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 782 | func (cr *Client) useV1() bool { |
| 783 | return !cr.useV1Alpha && cr.stubV1 != nil |
| 784 | } |
| 785 | |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 786 | // Reset ensures that any active stream with the server is closed, releasing any |
| 787 | // resources. |
| 788 | func (cr *Client) Reset() { |
| 789 | cr.connMu.Lock() |
| 790 | defer cr.connMu.Unlock() |
| 791 | cr.resetLocked() |
| 792 | } |
| 793 | |
| 794 | func (cr *Client) resetLocked() { |
| 795 | if cr.stream != nil { |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 796 | _ = cr.stream.CloseSend() |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 797 | for { |
| 798 | // drain the stream, this covers io.EOF too |
| 799 | if _, err := cr.stream.Recv(); err != nil { |
| 800 | break |
| 801 | } |
| 802 | } |
| 803 | cr.stream = nil |
| 804 | } |
| 805 | if cr.cancel != nil { |
| 806 | cr.cancel() |
| 807 | cr.cancel = nil |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | // ResolveService asks the server to resolve the given fully-qualified service |
| 812 | // name into a service descriptor. |
| 813 | func (cr *Client) ResolveService(serviceName string) (*desc.ServiceDescriptor, error) { |
| 814 | file, err := cr.FileContainingSymbol(serviceName) |
| 815 | if err != nil { |
| 816 | return nil, setSymbolType(err, serviceName, symbolTypeService) |
| 817 | } |
| 818 | d := file.FindSymbol(serviceName) |
| 819 | if d == nil { |
| 820 | return nil, symbolNotFound(serviceName, symbolTypeService, nil) |
| 821 | } |
| 822 | if s, ok := d.(*desc.ServiceDescriptor); ok { |
| 823 | return s, nil |
| 824 | } else { |
| 825 | return nil, symbolNotFound(serviceName, symbolTypeService, nil) |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | // ResolveMessage asks the server to resolve the given fully-qualified message |
| 830 | // name into a message descriptor. |
| 831 | func (cr *Client) ResolveMessage(messageName string) (*desc.MessageDescriptor, error) { |
| 832 | file, err := cr.FileContainingSymbol(messageName) |
| 833 | if err != nil { |
| 834 | return nil, setSymbolType(err, messageName, symbolTypeMessage) |
| 835 | } |
| 836 | d := file.FindSymbol(messageName) |
| 837 | if d == nil { |
| 838 | return nil, symbolNotFound(messageName, symbolTypeMessage, nil) |
| 839 | } |
| 840 | if s, ok := d.(*desc.MessageDescriptor); ok { |
| 841 | return s, nil |
| 842 | } else { |
| 843 | return nil, symbolNotFound(messageName, symbolTypeMessage, nil) |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | // ResolveEnum asks the server to resolve the given fully-qualified enum name |
| 848 | // into an enum descriptor. |
| 849 | func (cr *Client) ResolveEnum(enumName string) (*desc.EnumDescriptor, error) { |
| 850 | file, err := cr.FileContainingSymbol(enumName) |
| 851 | if err != nil { |
| 852 | return nil, setSymbolType(err, enumName, symbolTypeEnum) |
| 853 | } |
| 854 | d := file.FindSymbol(enumName) |
| 855 | if d == nil { |
| 856 | return nil, symbolNotFound(enumName, symbolTypeEnum, nil) |
| 857 | } |
| 858 | if s, ok := d.(*desc.EnumDescriptor); ok { |
| 859 | return s, nil |
| 860 | } else { |
| 861 | return nil, symbolNotFound(enumName, symbolTypeEnum, nil) |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | func setSymbolType(err error, name string, symType symbolType) error { |
| 866 | if e, ok := err.(*elementNotFoundError); ok { |
| 867 | if e.kind == elementKindSymbol && e.name == name && e.symType == symbolTypeUnknown { |
| 868 | e.symType = symType |
| 869 | } |
| 870 | } |
| 871 | return err |
| 872 | } |
| 873 | |
| 874 | // ResolveEnumValues asks the server to resolve the given fully-qualified enum |
| 875 | // name into a map of names to numbers that represents the enum's values. |
| 876 | func (cr *Client) ResolveEnumValues(enumName string) (map[string]int32, error) { |
| 877 | enumDesc, err := cr.ResolveEnum(enumName) |
| 878 | if err != nil { |
| 879 | return nil, err |
| 880 | } |
| 881 | vals := map[string]int32{} |
| 882 | for _, valDesc := range enumDesc.GetValues() { |
| 883 | vals[valDesc.GetName()] = valDesc.GetNumber() |
| 884 | } |
| 885 | return vals, nil |
| 886 | } |
| 887 | |
| 888 | // ResolveExtension asks the server to resolve the given extension number and |
| 889 | // fully-qualified message name into a field descriptor. |
| 890 | func (cr *Client) ResolveExtension(extendedType string, extensionNumber int32) (*desc.FieldDescriptor, error) { |
| 891 | file, err := cr.FileContainingExtension(extendedType, extensionNumber) |
| 892 | if err != nil { |
| 893 | return nil, err |
| 894 | } |
| 895 | d := findExtension(extendedType, extensionNumber, fileDescriptorExtensions{file}) |
| 896 | if d == nil { |
| 897 | return nil, extensionNotFound(extendedType, extensionNumber, nil) |
| 898 | } else { |
| 899 | return d, nil |
| 900 | } |
| 901 | } |
| 902 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 903 | func fileWithoutDeps(fd *descriptorpb.FileDescriptorProto, missingDeps []int) *descriptorpb.FileDescriptorProto { |
| 904 | // We need to rebuild the file without the missing deps. |
| 905 | fd = proto.Clone(fd).(*descriptorpb.FileDescriptorProto) |
| 906 | newNumDeps := len(fd.GetDependency()) - len(missingDeps) |
| 907 | newDeps := make([]string, 0, newNumDeps) |
| 908 | remapped := make(map[int]int, newNumDeps) |
| 909 | missingIdx := 0 |
| 910 | for i, dep := range fd.GetDependency() { |
| 911 | if missingIdx < len(missingDeps) { |
| 912 | if i == missingDeps[missingIdx] { |
| 913 | // This dep was missing. Skip it. |
| 914 | missingIdx++ |
| 915 | continue |
| 916 | } |
| 917 | } |
| 918 | remapped[i] = len(newDeps) |
| 919 | newDeps = append(newDeps, dep) |
| 920 | } |
| 921 | // Also rebuild public and weak import slices. |
| 922 | newPublic := make([]int32, 0, len(fd.GetPublicDependency())) |
| 923 | for _, idx := range fd.GetPublicDependency() { |
| 924 | newIdx, ok := remapped[int(idx)] |
| 925 | if ok { |
| 926 | newPublic = append(newPublic, int32(newIdx)) |
| 927 | } |
| 928 | } |
| 929 | newWeak := make([]int32, 0, len(fd.GetWeakDependency())) |
| 930 | for _, idx := range fd.GetWeakDependency() { |
| 931 | newIdx, ok := remapped[int(idx)] |
| 932 | if ok { |
| 933 | newWeak = append(newWeak, int32(newIdx)) |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | fd.Dependency = newDeps |
| 938 | fd.PublicDependency = newPublic |
| 939 | fd.WeakDependency = newWeak |
| 940 | return fd |
| 941 | } |
| 942 | |
| khenaidoo | 0927c72 | 2021-12-15 16:49:32 -0500 | [diff] [blame] | 943 | func findExtension(extendedType string, extensionNumber int32, scope extensionScope) *desc.FieldDescriptor { |
| 944 | // search extensions in this scope |
| 945 | for _, ext := range scope.extensions() { |
| 946 | if ext.GetNumber() == extensionNumber && ext.GetOwner().GetFullyQualifiedName() == extendedType { |
| 947 | return ext |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | // if not found, search nested scopes |
| 952 | for _, nested := range scope.nestedScopes() { |
| 953 | ext := findExtension(extendedType, extensionNumber, nested) |
| 954 | if ext != nil { |
| 955 | return ext |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | return nil |
| 960 | } |
| 961 | |
| 962 | type extensionScope interface { |
| 963 | extensions() []*desc.FieldDescriptor |
| 964 | nestedScopes() []extensionScope |
| 965 | } |
| 966 | |
| 967 | // fileDescriptorExtensions implements extensionHolder interface on top of |
| 968 | // FileDescriptorProto |
| 969 | type fileDescriptorExtensions struct { |
| 970 | proto *desc.FileDescriptor |
| 971 | } |
| 972 | |
| 973 | func (fde fileDescriptorExtensions) extensions() []*desc.FieldDescriptor { |
| 974 | return fde.proto.GetExtensions() |
| 975 | } |
| 976 | |
| 977 | func (fde fileDescriptorExtensions) nestedScopes() []extensionScope { |
| 978 | scopes := make([]extensionScope, len(fde.proto.GetMessageTypes())) |
| 979 | for i, m := range fde.proto.GetMessageTypes() { |
| 980 | scopes[i] = msgDescriptorExtensions{m} |
| 981 | } |
| 982 | return scopes |
| 983 | } |
| 984 | |
| 985 | // msgDescriptorExtensions implements extensionHolder interface on top of |
| 986 | // DescriptorProto |
| 987 | type msgDescriptorExtensions struct { |
| 988 | proto *desc.MessageDescriptor |
| 989 | } |
| 990 | |
| 991 | func (mde msgDescriptorExtensions) extensions() []*desc.FieldDescriptor { |
| 992 | return mde.proto.GetNestedExtensions() |
| 993 | } |
| 994 | |
| 995 | func (mde msgDescriptorExtensions) nestedScopes() []extensionScope { |
| 996 | scopes := make([]extensionScope, len(mde.proto.GetNestedMessageTypes())) |
| 997 | for i, m := range mde.proto.GetNestedMessageTypes() { |
| 998 | scopes[i] = msgDescriptorExtensions{m} |
| 999 | } |
| 1000 | return scopes |
| 1001 | } |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame^] | 1002 | |
| 1003 | type adaptStreamFromV1Alpha struct { |
| 1004 | refv1alpha.ServerReflection_ServerReflectionInfoClient |
| 1005 | } |
| 1006 | |
| 1007 | func (a adaptStreamFromV1Alpha) Send(request *refv1.ServerReflectionRequest) error { |
| 1008 | v1req := toV1AlphaRequest(request) |
| 1009 | return a.ServerReflection_ServerReflectionInfoClient.Send(v1req) |
| 1010 | } |
| 1011 | |
| 1012 | func (a adaptStreamFromV1Alpha) Recv() (*refv1.ServerReflectionResponse, error) { |
| 1013 | v1resp, err := a.ServerReflection_ServerReflectionInfoClient.Recv() |
| 1014 | if err != nil { |
| 1015 | return nil, err |
| 1016 | } |
| 1017 | return toV1Response(v1resp), nil |
| 1018 | } |