proxygen
BroadcastPool.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present Facebook, Inc.
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 #pragma once
17 
18 #include <folly/ThreadLocal.h>
25 
26 namespace wangle {
27 
28 template <typename R, typename P = DefaultPipeline>
29 class ServerPool {
30  public:
31  virtual ~ServerPool() {}
32 
37  virtual folly::Future<P*> connect(
38  BaseClientBootstrap<P>* client,
39  const R& routingData) noexcept = 0;
40 };
41 
49 template <typename T, typename R, typename P = DefaultPipeline>
51  public:
54  public:
55  using UniquePtr = std::unique_ptr<
57 
59  BroadcastPool<T, R, P>* broadcastPool,
60  const R& routingData)
61  : broadcastPool_(broadcastPool),
62  routingData_(routingData),
63  client_(broadcastPool_->clientBootstrapFactory_->newClient()) {
64  client_->pipelineFactory(broadcastPool_->broadcastPipelineFactory_);
65  }
66 
67  ~BroadcastManager() override {
68  if (client_->getPipeline()) {
69  client_->getPipeline()->setPipelineManager(nullptr);
70  }
71  }
72 
74 
75  // PipelineManager implementation
76  void deletePipeline(PipelineBase* pipeline) override;
77 
78  private:
79  void handleConnectError(const std::exception& ex) noexcept;
80 
81  BroadcastPool<T, R, P>* broadcastPool_{nullptr};
83 
84  std::unique_ptr<BaseClientBootstrap<P>> client_;
85 
86  bool connectStarted_{false};
87  bool deletingBroadcast_{false};
89  };
90 
92  std::shared_ptr<ServerPool<R, P>> serverPool,
93  std::shared_ptr<BroadcastPipelineFactory<T, R>> pipelineFactory,
94  std::shared_ptr<BaseClientBootstrapFactory<>> clientFactory =
95  std::make_shared<ClientBootstrapFactory>())
96  : serverPool_(serverPool),
97  broadcastPipelineFactory_(pipelineFactory),
98  clientBootstrapFactory_(clientFactory) {}
99 
100  virtual ~BroadcastPool() {}
101 
102  // Non-copyable
103  BroadcastPool(const BroadcastPool&) = delete;
104  BroadcastPool& operator=(const BroadcastPool&) = delete;
105 
106  // Movable
107  BroadcastPool(BroadcastPool&&) = default;
108  BroadcastPool& operator=(BroadcastPool&&) = default;
109 
125  virtual folly::Future<BroadcastHandler<T, R>*> getHandler(
126  const R& routingData);
127 
131  bool isBroadcasting(const R& routingData) {
132  return (broadcasts_.find(routingData) != broadcasts_.end());
133  }
134 
135  virtual void deleteBroadcast(const R& routingData) {
136  broadcasts_.erase(routingData);
137  }
138 
139  private:
140  std::shared_ptr<ServerPool<R, P>> serverPool_;
141  std::shared_ptr<BroadcastPipelineFactory<T, R>> broadcastPipelineFactory_;
142  std::shared_ptr<BaseClientBootstrapFactory<>> clientBootstrapFactory_;
143  std::map<R, typename BroadcastManager::UniquePtr> broadcasts_;
144 };
145 
146 } // namespace wangle
147 
virtual ~ServerPool()
Definition: BroadcastPool.h:31
virtual folly::Future< P * > connect(BaseClientBootstrap< P > *client, const R &routingData) noexcept=0
std::shared_ptr< BroadcastPipelineFactory< T, R > > broadcastPipelineFactory_
BroadcastPool(std::shared_ptr< ServerPool< R, P >> serverPool, std::shared_ptr< BroadcastPipelineFactory< T, R >> pipelineFactory, std::shared_ptr< BaseClientBootstrapFactory<>> clientFactory=std::make_shared< ClientBootstrapFactory >())
Definition: BroadcastPool.h:91
std::shared_ptr< BaseClientBootstrapFactory<> > clientBootstrapFactory_
requires E e noexcept(noexcept(s.error(std::move(e))))
std::map< R, typename BroadcastManager::UniquePtr > broadcasts_
BroadcastManager(BroadcastPool< T, R, P > *broadcastPool, const R &routingData)
Definition: BroadcastPool.h:58
std::unique_ptr< BaseClientBootstrap< P > > client_
Definition: BroadcastPool.h:84
std::unique_ptr< BroadcastManager, folly::DelayedDestruction::Destructor > UniquePtr
Definition: BroadcastPool.h:56
folly::SharedPromise< BroadcastHandler< T, R > * > sharedPromise_
Definition: BroadcastPool.h:88
bool isBroadcasting(const R &routingData)
virtual void deleteBroadcast(const R &routingData)
std::shared_ptr< ServerPool< R, P > > serverPool_