proxygen
CloseOnReleaseFilter.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 
19 #include <wangle/service/Service.h>
20 
21 namespace wangle {
22 
27 template <typename Req, typename Resp = Req>
28 class CloseOnReleaseFilter : public ServiceFilter<Req, Resp> {
29  public:
30  explicit CloseOnReleaseFilter(std::shared_ptr<Service<Req, Resp>> service)
31  : ServiceFilter<Req, Resp>(service) {}
32 
34  if (!released ){
35  return (*this->service_)(std::move(req));
36  } else {
37  return folly::makeFuture<Resp>(
38  folly::make_exception_wrapper<std::runtime_error>("Service Closed"));
39  }
40  }
41 
43  if (!released.exchange(true)) {
44  return this->service_->close();
45  } else {
46  return folly::makeFuture();
47  }
48  }
49  private:
50  std::atomic<bool> released{false};
51 };
52 
53 } // namespace wangle
folly::Future< Resp > operator()(Req req) override
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
CloseOnReleaseFilter(std::shared_ptr< Service< Req, Resp >> service)
std::shared_ptr< Service< Req, Resp > > service_
Definition: Service.h:82
folly::Future< folly::Unit > close() override
Future< typename std::decay< T >::type > makeFuture(T &&t)
Definition: Future-inl.h:1310