proxygen
OutputBufferingHandler.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 
17 #pragma once
18 
20 #include <folly/io/IOBuf.h>
21 #include <folly/io/IOBufQueue.h>
24 #include <wangle/channel/Handler.h>
25 
26 namespace wangle {
27 
28 /*
29  * OutputBufferingHandler buffers writes in order to minimize syscalls. The
30  * transport will be written to once per event loop instead of on every write.
31  *
32  * This handler may only be used in a single Pipeline.
33  */
36  public:
38  Context* ctx,
39  std::unique_ptr<folly::IOBuf> buf) override {
40  CHECK(buf);
41  if (!queueSends_) {
42  return ctx->fireWrite(std::move(buf));
43  } else {
44  // Delay sends to optimize for fewer syscalls
45  if (!sends_) {
46  DCHECK(!isLoopCallbackScheduled());
47  // Buffer all the sends, and call writev once per event loop.
48  sends_ = std::move(buf);
49  ctx->getTransport()->getEventBase()->runInLoop(this);
50  } else {
51  DCHECK(isLoopCallbackScheduled());
52  sends_->prependChain(std::move(buf));
53  }
54  return sharedPromise_.getFuture();
55  }
56  }
57 
58  void runLoopCallback() noexcept override {
60  std::swap(sharedPromise, sharedPromise_);
61  getContext()
63  .thenTry([sharedPromise = std::move(sharedPromise)](
64  folly::Try<folly::Unit> t) mutable {
65  sharedPromise.setTry(std::move(t));
66  });
67  }
68 
69  void cleanUp() {
72  }
73 
74  sends_.reset();
76  }
77 
81  }
82 
83  // If there are sends queued, cancel them
85  folly::make_exception_wrapper<std::runtime_error>(
86  "close() called while sends still pending"));
87  sends_.reset();
89  return ctx->fireClose();
90  }
91 
93  std::unique_ptr<folly::IOBuf> sends_{nullptr};
94  bool queueSends_{true};
95 };
96 
97 } // namespace wangle
virtual folly::Future< folly::Unit > fireWrite(Out msg)=0
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::unique_ptr< folly::IOBuf > sends_
requires E e noexcept(noexcept(s.error(std::move(e))))
void setTry(Try< T > &&t)
OutboundHandlerContext< Wout > * getContext()
Definition: Handler.h:34
void setException(exception_wrapper ew)
bool isLoopCallbackScheduled() const
Definition: EventBase.h:160
void runLoopCallback() noexceptoverride
folly::SharedPromise< folly::Unit > sharedPromise_
folly::Future< folly::Unit > close(Context *ctx) override
virtual folly::Future< folly::Unit > fireClose()=0
folly::Future< folly::Unit > write(Context *ctx, std::unique_ptr< folly::IOBuf > buf) override
void swap(SwapTrackingAlloc< T > &, SwapTrackingAlloc< T > &)
Definition: F14TestUtil.h:414
std::shared_ptr< folly::AsyncTransport > getTransport()