31abbdb08496492639a8a34cb29ce0ea68b235a0
[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/wangle/channel/Handler.h>
20 #include <folly/io/async/EventBase.h>
21 #include <folly/io/async/EventBaseManager.h>
22 #include <folly/io/IOBuf.h>
23 #include <folly/io/IOBufQueue.h>
24
25 namespace folly { namespace wangle {
26
27 /*
28  * OutputBufferingHandler buffers writes in order to minimize syscalls. The
29  * transport will be written to once per event loop instead of on every write.
30  *
31  * This handler may only be used in a single Pipeline.
32  */
33 class OutputBufferingHandler : public OutboundBytesToBytesHandler,
34                                protected EventBase::LoopCallback {
35  public:
36   Future<void> write(Context* ctx, std::unique_ptr<IOBuf> buf) override {
37     CHECK(buf);
38     if (!queueSends_) {
39       return ctx->fireWrite(std::move(buf));
40     } else {
41       // Delay sends to optimize for fewer syscalls
42       if (!sends_) {
43         DCHECK(!isLoopCallbackScheduled());
44         // Buffer all the sends, and call writev once per event loop.
45         sends_ = std::move(buf);
46         ctx->getTransport()->getEventBase()->runInLoop(this);
47       } else {
48         DCHECK(isLoopCallbackScheduled());
49         sends_->prependChain(std::move(buf));
50       }
51       Promise<void> p;
52       auto f = p.getFuture();
53       promises_.push_back(std::move(p));
54       return f;
55     }
56   }
57
58   void runLoopCallback() noexcept override {
59     MoveWrapper<std::vector<Promise<void>>> promises(std::move(promises_));
60     getContext()->fireWrite(std::move(sends_))
61       .then([promises](Try<void> t) mutable {
62         for (auto& p : *promises) {
63           p.setTry(Try<void>(t));
64         }
65       });
66   }
67
68   Future<void> close(Context* ctx) override {
69     if (isLoopCallbackScheduled()) {
70       cancelLoopCallback();
71     }
72
73     // If there are sends queued, cancel them
74     for (auto& promise : promises_) {
75       promise.setException(
76         folly::make_exception_wrapper<std::runtime_error>(
77           "close() called while sends still pending"));
78     }
79     sends_.reset();
80     promises_.clear();
81     return ctx->fireClose();
82   }
83
84   std::vector<Promise<void>> promises_;
85   std::unique_ptr<IOBuf> sends_{nullptr};
86   bool queueSends_{true};
87 };
88
89 }}