1 // 2 // Copyright 2016 gRPC authors. 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 #ifndef GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_RESOLVER_FAKE_FAKE_RESOLVER_H 18 #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_RESOLVER_FAKE_FAKE_RESOLVER_H 19 20 #include <grpc/support/port_platform.h> 21 22 #include "src/core/ext/filters/client_channel/resolver.h" 23 #include "src/core/lib/channel/channel_args.h" 24 #include "src/core/lib/gprpp/ref_counted.h" 25 #include "src/core/lib/iomgr/error.h" 26 27 #define GRPC_ARG_FAKE_RESOLVER_RESPONSE_GENERATOR \ 28 "grpc.fake_resolver.response_generator" 29 30 namespace grpc_core { 31 32 class FakeResolver; 33 34 /// A mechanism for generating responses for the fake resolver. 35 /// An instance of this class is passed to the fake resolver via a channel 36 /// argument (see \a MakeChannelArg()) and used to inject and trigger custom 37 /// resolutions. 38 // TODO(roth): I would ideally like this to be InternallyRefCounted 39 // instead of RefCounted, but external refs are currently needed to 40 // encode this in channel args. Once channel_args are converted to C++, 41 // see if we can find a way to fix this. 42 class FakeResolverResponseGenerator 43 : public RefCounted<FakeResolverResponseGenerator> { 44 public: 45 static const grpc_arg_pointer_vtable kChannelArgPointerVtable; 46 47 FakeResolverResponseGenerator(); 48 ~FakeResolverResponseGenerator() override; 49 50 // Instructs the fake resolver associated with the response generator 51 // instance to trigger a new resolution with the specified result. If the 52 // resolver is not available yet, delays response setting until it is. This 53 // can be called at most once before the resolver is available. 54 void SetResponse(Resolver::Result result); 55 56 // Sets the re-resolution response, which is returned by the fake resolver 57 // when re-resolution is requested (via \a RequestReresolutionLocked()). 58 // The new re-resolution response replaces any previous re-resolution 59 // response that may have been set by a previous call. 60 void SetReresolutionResponse(Resolver::Result result); 61 62 // Unsets the re-resolution response. After this, the fake resolver will 63 // not return anything when \a RequestReresolutionLocked() is called. 64 void UnsetReresolutionResponse(); 65 66 // Tells the resolver to return a transient failure. 67 void SetFailure(); 68 69 // Same as SetFailure(), but instead of returning the error 70 // immediately, waits for the next call to RequestReresolutionLocked(). 71 void SetFailureOnReresolution(); 72 73 // Returns a channel arg containing \a generator. 74 // TODO(roth): When we have time, make this a non-static method. 75 static grpc_arg MakeChannelArg(FakeResolverResponseGenerator* generator); 76 77 // Returns the response generator in \a args, or null if not found. 78 static RefCountedPtr<FakeResolverResponseGenerator> GetFromArgs( 79 const grpc_channel_args* args); 80 81 private: 82 friend class FakeResolver; 83 // Set the corresponding FakeResolver to this generator. 84 void SetFakeResolver(RefCountedPtr<FakeResolver> resolver); 85 86 // Mutex protecting the members below. 87 Mutex mu_; 88 RefCountedPtr<FakeResolver> resolver_ ABSL_GUARDED_BY(mu_); 89 Resolver::Result result_ ABSL_GUARDED_BY(mu_); 90 bool has_result_ ABSL_GUARDED_BY(mu_) = false; 91 }; 92 93 } // namespace grpc_core 94 95 #endif // GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_RESOLVER_FAKE_FAKE_RESOLVER_H 96