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