1 // 2 // Copyright 2015 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_SRC_CORE_RESOLVER_POLLING_RESOLVER_H 18 #define GRPC_SRC_CORE_RESOLVER_POLLING_RESOLVER_H 19 20 #include <grpc/support/port_platform.h> 21 22 #include <memory> 23 #include <string> 24 25 #include "absl/status/status.h" 26 #include "absl/types/optional.h" 27 28 #include <grpc/event_engine/event_engine.h> 29 30 #include "src/core/lib/backoff/backoff.h" 31 #include "src/core/lib/channel/channel_args.h" 32 #include "src/core/lib/debug/trace.h" 33 #include "src/core/lib/gprpp/orphanable.h" 34 #include "src/core/lib/gprpp/time.h" 35 #include "src/core/lib/gprpp/work_serializer.h" 36 #include "src/core/lib/iomgr/iomgr_fwd.h" 37 #include "src/core/resolver/resolver.h" 38 #include "src/core/resolver/resolver_factory.h" 39 40 namespace grpc_core { 41 42 // A base class for polling-based resolvers. 43 // Handles cooldown and backoff timers. 44 // Implementations need only to implement StartRequest(). 45 class PollingResolver : public Resolver { 46 public: 47 PollingResolver(ResolverArgs args, Duration min_time_between_resolutions, 48 BackOff::Options backoff_options, TraceFlag* tracer); 49 ~PollingResolver() override; 50 51 void StartLocked() override; 52 void RequestReresolutionLocked() override; 53 void ResetBackoffLocked() override; 54 void ShutdownLocked() override; 55 56 protected: 57 // Implemented by subclass. 58 // Starts a request, returning an object representing the pending 59 // request. Orphaning that object should cancel the request. 60 // When the request is complete, the implementation must call 61 // OnRequestComplete() with the result. 62 virtual OrphanablePtr<Orphanable> StartRequest() = 0; 63 64 // To be invoked by the subclass when a request is complete. 65 void OnRequestComplete(Result result); 66 67 // Convenient accessor methods for subclasses. authority()68 const std::string& authority() const { return authority_; } name_to_resolve()69 const std::string& name_to_resolve() const { return name_to_resolve_; } interested_parties()70 grpc_pollset_set* interested_parties() const { return interested_parties_; } channel_args()71 const ChannelArgs& channel_args() const { return channel_args_; } work_serializer()72 WorkSerializer* work_serializer() { return work_serializer_.get(); } 73 74 private: 75 void MaybeStartResolvingLocked(); 76 void StartResolvingLocked(); 77 78 void OnRequestCompleteLocked(Result result); 79 void GetResultStatus(absl::Status status); 80 81 void ScheduleNextResolutionTimer(const Duration& timeout); 82 void OnNextResolutionLocked(); 83 void MaybeCancelNextResolutionTimer(); 84 85 /// authority 86 std::string authority_; 87 /// name to resolve 88 std::string name_to_resolve_; 89 /// channel args 90 ChannelArgs channel_args_; 91 std::shared_ptr<WorkSerializer> work_serializer_; 92 std::unique_ptr<ResultHandler> result_handler_; 93 TraceFlag* tracer_; 94 /// pollset_set to drive the name resolution process 95 grpc_pollset_set* interested_parties_ = nullptr; 96 /// are we shutting down? 97 bool shutdown_ = false; 98 /// are we currently resolving? 99 OrphanablePtr<Orphanable> request_; 100 /// min time between DNS requests 101 Duration min_time_between_resolutions_; 102 /// timestamp of last DNS request 103 absl::optional<Timestamp> last_resolution_timestamp_; 104 /// retry backoff state 105 BackOff backoff_; 106 /// state for handling interactions between re-resolution requests and 107 /// result health callbacks 108 enum class ResultStatusState { 109 kNone, 110 kResultHealthCallbackPending, 111 kReresolutionRequestedWhileCallbackWasPending, 112 }; 113 ResultStatusState result_status_state_ = ResultStatusState::kNone; 114 /// next resolution timer 115 absl::optional<grpc_event_engine::experimental::EventEngine::TaskHandle> 116 next_resolution_timer_handle_; 117 }; 118 119 } // namespace grpc_core 120 121 #endif // GRPC_SRC_CORE_RESOLVER_POLLING_RESOLVER_H 122