Debian Codegraph @

src/core/lib/iomgr/resolve_address_impl.h

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