d712b8a0a11c1c2c7e561969b56dcbee937e0d4e
[folly.git] / folly / wangle / channel / OutputBufferingHandler.h
1 /*
2  * Copyright 2015 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
19 #include <folly/futures/SharedPromise.h>
20 #include <folly/wangle/channel/Handler.h>
21 #include <folly/io/async/EventBase.h>
22 #include <folly/io/async/EventBaseManager.h>
23 #include <folly/io/IOBuf.h>
24 #include <folly/io/IOBufQueue.h>
25
26 namespace folly { 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  */
34 class OutputBufferingHandler : public OutboundBytesToBytesHandler,
35                                protected EventBase::LoopCallback {
36  public:
37   Future<void> write(Context* ctx, std::unique_ptr<IOBuf> buf) override {
38     CHECK(buf);
39     if (!queueSends_) {
40       return ctx->fireWrite(std::move(buf));
41     } else {
42       // Delay sends to optimize for fewer syscalls
43       if (!sends_) {
44         DCHECK(!isLoopCallbackScheduled());
45         // Buffer all the sends, and call writev once per event loop.
46         sends_ = std::move(buf);
47         ctx->getTransport()->getEventBase()->runInLoop(this);
48       } else {
49         DCHECK(isLoopCallbackScheduled());
50         sends_->prependChain(std::move(buf));
51       }
52       return sharedPromise_.getFuture();
53     }
54   }
55
56   void runLoopCallback() noexcept override {
57     MoveWrapper<SharedPromise<void>> sharedPromise;
58     std::swap(*sharedPromise, sharedPromise_);
59     getContext()->fireWrite(std::move(sends_))
60       .then([sharedPromise](Try<void> t) mutable {
61         sharedPromise->setTry(std::move(t));
62       });
63   }
64
65   Future<void> close(Context* ctx) override {
66     if (isLoopCallbackScheduled()) {
67       cancelLoopCallback();
68     }
69
70     // If there are sends queued, cancel them
71     sharedPromise_.setException(
72       folly::make_exception_wrapper<std::runtime_error>(
73         "close() called while sends still pending"));
74     sends_.reset();
75     sharedPromise_ = SharedPromise<void>();
76     return ctx->fireClose();
77   }
78
79   SharedPromise<void> sharedPromise_;
80   std::unique_ptr<IOBuf> sends_{nullptr};
81   bool queueSends_{true};
82 };
83
84 }}