• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_LIB_IOMGR_RESOLVE_ADDRESS_IMPL_H
18 #define GRPC_SRC_CORE_LIB_IOMGR_RESOLVE_ADDRESS_IMPL_H
19 
20 #include <grpc/support/port_platform.h>
21 #include <stddef.h>
22 
23 #include "src/core/lib/iomgr/exec_ctx.h"
24 #include "src/core/lib/iomgr/port.h"
25 #include "src/core/lib/iomgr/resolve_address.h"
26 
27 namespace grpc_core {
28 
29 // A fire and forget class to schedule DNS resolution callbacks on the ExecCtx,
30 // which is frequently necessary to avoid lock inversion related problems.
31 class DNSCallbackExecCtxScheduler {
32  public:
DNSCallbackExecCtxScheduler(std::function<void (absl::StatusOr<std::vector<grpc_resolved_address>>)> on_done,absl::StatusOr<std::vector<grpc_resolved_address>> param)33   DNSCallbackExecCtxScheduler(
34       std::function<void(absl::StatusOr<std::vector<grpc_resolved_address>>)>
35           on_done,
36       absl::StatusOr<std::vector<grpc_resolved_address>> param)
37       : on_done_(std::move(on_done)), param_(std::move(param)) {
38     GRPC_CLOSURE_INIT(&closure_, RunCallback, this, grpc_schedule_on_exec_ctx);
39     ExecCtx::Run(DEBUG_LOCATION, &closure_, absl::OkStatus());
40   }
41 
42  private:
RunCallback(void * arg,grpc_error_handle)43   static void RunCallback(void* arg, grpc_error_handle /* error */) {
44     DNSCallbackExecCtxScheduler* self =
45         static_cast<DNSCallbackExecCtxScheduler*>(arg);
46     self->on_done_(std::move(self->param_));
47     delete self;
48   }
49 
50   const std::function<void(absl::StatusOr<std::vector<grpc_resolved_address>>)>
51       on_done_;
52   absl::StatusOr<std::vector<grpc_resolved_address>> param_;
53   grpc_closure closure_;
54 };
55 
56 }  // namespace grpc_core
57 
58 #endif  // GRPC_SRC_CORE_LIB_IOMGR_RESOLVE_ADDRESS_IMPL_H
59