/Users/deen/code/yugabyte-db/src/yb/client/async_initializer.h
Line | Count | Source |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | |
13 | | #ifndef YB_CLIENT_ASYNC_INITIALIZER_H_ |
14 | | #define YB_CLIENT_ASYNC_INITIALIZER_H_ |
15 | | |
16 | | #include <future> |
17 | | |
18 | | #include "yb/client/client_fwd.h" |
19 | | |
20 | | #include "yb/server/server_base_options.h" |
21 | | |
22 | | #include "yb/util/atomic.h" |
23 | | |
24 | | namespace yb { |
25 | | namespace client { |
26 | | |
27 | | YB_STRONGLY_TYPED_BOOL(AutoStart); |
28 | | |
29 | | class AsyncClientInitialiser { |
30 | | public: |
31 | | AsyncClientInitialiser( |
32 | | const std::string& client_name, const uint32_t num_reactors, |
33 | | const uint32_t timeout_seconds, const std::string& tserver_uuid, |
34 | | const server::ServerBaseOptions* opts, scoped_refptr<MetricEntity> metric_entity, |
35 | | const std::shared_ptr<MemTracker>& parent_mem_tracker, |
36 | | rpc::Messenger* messenger = nullptr); |
37 | | |
38 | | ~AsyncClientInitialiser(); |
39 | | |
40 | 2.29k | void Shutdown() { stopping_ = true; } |
41 | | |
42 | | void Start(); |
43 | | |
44 | | YBClient* client() const; |
45 | | |
46 | 10.8k | YBClientBuilder& builder() { |
47 | 10.8k | return *client_builder_; |
48 | 10.8k | } |
49 | | |
50 | 245k | const std::shared_future<client::YBClient*>& get_client_future() const { |
51 | 245k | return client_future_; |
52 | 245k | } |
53 | | |
54 | 5.81k | void AddPostCreateHook(std::function<void(client::YBClient*)> functor) { |
55 | 5.81k | post_create_hooks_.push_back(std::move(functor)); |
56 | 5.81k | } |
57 | | |
58 | | private: |
59 | | void InitClient(); |
60 | | |
61 | | std::unique_ptr<YBClientBuilder> client_builder_; |
62 | | rpc::Messenger* messenger_ = nullptr; |
63 | | std::promise<client::YBClient*> client_promise_; |
64 | | mutable std::shared_future<client::YBClient*> client_future_; |
65 | | AtomicUniquePtr<client::YBClient> client_holder_; |
66 | | std::vector<std::function<void(client::YBClient*)>> post_create_hooks_; |
67 | | |
68 | | std::thread init_client_thread_; |
69 | | std::atomic<bool> stopping_ = {false}; |
70 | | }; |
71 | | |
72 | | } // namespace client |
73 | | } // namespace yb |
74 | | |
75 | | #endif // YB_CLIENT_ASYNC_INITIALIZER_H_ |