proxygen
BroadcastPool-inl.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 namespace wangle {
19 
20 template <typename T, typename R, typename P>
23  // getFuture() returns a completed future if we are already connected
24  // Set the executor to the InlineExecutor because subsequent code depends
25  // on the future callback being called inline to ensure that the handler
26  // is not garbage collected before use.
27  auto future = sharedPromise_.getFuture().via(
29 
30  if (connectStarted_) {
31  // Either already connected, in which case the future has the handler,
32  // or there's an outstanding connect request and the promise will be
33  // fulfilled when the connect request completes.
34  return future;
35  }
36 
37  // Kickoff connect request and fulfill all pending promises on completion
38  connectStarted_ = true;
39 
40  broadcastPool_->serverPool_->connect(client_.get(), routingData_)
41  .thenValue([this](DefaultPipeline* pipeline) {
42  DestructorGuard dg(this);
43  pipeline->setPipelineManager(this);
44 
45  auto pipelineFactory = broadcastPool_->broadcastPipelineFactory_;
46  try {
47  pipelineFactory->setRoutingData(pipeline, routingData_);
48  } catch (const std::exception& ex) {
50  return;
51  }
52 
53  if (deletingBroadcast_) {
54  // setRoutingData() could result in an error that would cause the
55  // BroadcastPipeline to get deleted.
56  handleConnectError(std::runtime_error(
57  "Broadcast deleted due to upstream connection error"));
58  return;
59  }
60 
61  auto handler = pipelineFactory->getBroadcastHandler(pipeline);
62  CHECK(handler);
63  sharedPromise_.setValue(handler);
64 
65  // If all the observers go away before connect returns, then the
66  // BroadcastHandler will be idle without any subscribers. Close
67  // the pipeline and remove the broadcast from the pool so that
68  // connections are not leaked.
69  handler->closeIfIdle();
70  })
71  .onError([this](const std::exception& ex) { handleConnectError(ex); });
72 
73  return future;
74 }
75 
76 template <typename T, typename R, typename P>
78  PipelineBase* pipeline) {
79  CHECK(client_->getPipeline() == pipeline);
80  deletingBroadcast_ = true;
81  broadcastPool_->deleteBroadcast(routingData_);
82 }
83 
84 template <typename T, typename R, typename P>
86  const std::exception& ex) noexcept {
87  LOG(ERROR) << "Error connecting to upstream: " << ex.what();
88 
89  auto sharedPromise = std::move(sharedPromise_);
90  broadcastPool_->deleteBroadcast(routingData_);
91  sharedPromise.setException(folly::make_exception_wrapper<std::exception>(ex));
92 }
93 
94 template <typename T, typename R, typename P>
96  const R& routingData) {
97  const auto& iter = broadcasts_.find(routingData);
98  if (iter != broadcasts_.end()) {
99  return iter->second->getHandler();
100  }
101 
102  typename BroadcastManager::UniquePtr broadcast(
103  new BroadcastManager(this, routingData));
104 
105  auto broadcastPtr = broadcast.get();
106  broadcasts_.insert(std::make_pair(routingData, std::move(broadcast)));
107 
108  // The executor on this future is set to be an InlineExecutor to ensure that
109  // the continuation can be run inline and satisfy the lifetime requirement
110  // on the return value of this function.
111  return broadcastPtr->getHandler();
112 }
113 
114 } // namespace wangle
void deletePipeline(PipelineBase *pipeline) override
folly::Future< BroadcastHandler< T, R > * > getHandler()
void handleConnectError(const std::exception &ex) noexcept
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
requires E e noexcept(noexcept(s.error(std::move(e))))
void handler(int, siginfo_t *, void *)
std::map< R, typename BroadcastManager::UniquePtr > broadcasts_
BroadcastManager(BroadcastPool< T, R, P > *broadcastPool, const R &routingData)
Definition: BroadcastPool.h:58
FOLLY_ATTR_VISIBILITY_HIDDEN static FOLLY_ALWAYS_INLINE InlineExecutor & instance() noexcept
std::unique_ptr< BaseClientBootstrap< P > > client_
Definition: BroadcastPool.h:84
virtual folly::Future< BroadcastHandler< T, R > * > getHandler(const R &routingData)
std::unique_ptr< BroadcastManager, folly::DelayedDestruction::Destructor > UniquePtr
Definition: BroadcastPool.h:56
folly::SharedPromise< BroadcastHandler< T, R > * > sharedPromise_
Definition: BroadcastPool.h:88
BroadcastPool< T, R, P > * broadcastPool_
Definition: BroadcastPool.h:81
void setPipelineManager(PipelineManager *manager)
Definition: Pipeline.h:47