proxygen
Service.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 <folly/futures/Future.h>
20 #include <folly/Memory.h>
21 
26 
27 namespace wangle {
28 
33 template <typename Req, typename Resp = Req>
34 class Service {
35  public:
36  virtual folly::Future<Resp> operator()(Req request) = 0;
37  virtual ~Service() = default;
39  return folly::makeFuture();
40  }
41  virtual bool isAvailable() {
42  return true;
43  }
44 };
45 
65 template <typename ReqA, typename RespA,
66  typename ReqB = ReqA, typename RespB = RespA>
67 class ServiceFilter : public Service<ReqA, RespA> {
68  public:
69  explicit ServiceFilter(std::shared_ptr<Service<ReqB, RespB>> service)
70  : service_(service) {}
71  ~ServiceFilter() override = default;
72 
74  return service_->close();
75  }
76 
77  bool isAvailable() override {
78  return service_->isAvailable();
79  }
80 
81  protected:
82  std::shared_ptr<Service<ReqB, RespB>> service_;
83 };
84 
91 template <typename Pipeline, typename Req, typename Resp>
93  public:
95  std::shared_ptr<ClientBootstrap<Pipeline>> client) = 0;
96 
97  virtual ~ServiceFactory() = default;
98 
99 };
100 
101 
102 template <typename Pipeline, typename Req, typename Resp>
103 class ConstFactory : public ServiceFactory<Pipeline, Req, Resp> {
104  public:
105  explicit ConstFactory(std::shared_ptr<Service<Req, Resp>> service)
106  : service_(service) {}
107 
109  std::shared_ptr<ClientBootstrap<Pipeline>> /* client */) override {
110  return service_;
111  }
112  private:
113  std::shared_ptr<Service<Req, Resp>> service_;
114 };
115 
116 template <typename Pipeline, typename ReqA, typename RespA,
117  typename ReqB = ReqA, typename RespB = RespA>
118 class ServiceFactoryFilter : public ServiceFactory<Pipeline, ReqA, RespA> {
119  public:
121  std::shared_ptr<ServiceFactory<Pipeline, ReqB, RespB>> serviceFactory)
122  : serviceFactory_(std::move(serviceFactory)) {}
123 
124  ~ServiceFactoryFilter() override = default;
125 
126  protected:
127  std::shared_ptr<ServiceFactory<Pipeline, ReqB, RespB>> serviceFactory_;
128 };
129 
130 template <typename Pipeline, typename Req, typename Resp = Req>
131 class FactoryToService : public Service<Req, Resp> {
132  public:
134  std::shared_ptr<ServiceFactory<Pipeline, Req, Resp>> factory)
135  : factory_(factory) {}
136  ~FactoryToService() override = default;
137 
138  folly::Future<Resp> operator()(Req request) override {
139  DCHECK(factory_);
140  return ((*factory_)(nullptr))
141  .thenValue([=](std::shared_ptr<Service<Req, Resp>> service) {
142  return (*service)(std::move(request)).ensure([this]() {
143  this->close();
144  });
145  });
146  }
147 
148  private:
149  std::shared_ptr<ServiceFactory<Pipeline, Req, Resp>> factory_;
150 };
151 
152 
153 } // namespace wangle
FactoryToService(std::shared_ptr< ServiceFactory< Pipeline, Req, Resp >> factory)
Definition: Service.h:133
folly::Future< std::shared_ptr< Service< Req, Resp > > > operator()(std::shared_ptr< ClientBootstrap< Pipeline >>) override
Definition: Service.h:108
folly::Future< folly::Unit > close() override
Definition: Service.h:73
std::shared_ptr< ServiceFactory< Pipeline, Req, Resp > > factory_
Definition: Service.h:149
virtual folly::Future< Resp > operator()(Req request)=0
virtual folly::Future< folly::Unit > close()
Definition: Service.h:38
ServiceFilter(std::shared_ptr< Service< ReqB, RespB >> service)
Definition: Service.h:69
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
STL namespace.
std::shared_ptr< ServiceFactory< Pipeline, ReqB, RespB > > serviceFactory_
Definition: Service.h:127
std::shared_ptr< Service< ReqB, RespB > > service_
Definition: Service.h:82
folly::Future< Resp > operator()(Req request) override
Definition: Service.h:138
std::shared_ptr< Service< Req, Resp > > service_
Definition: Service.h:113
virtual ~Service()=default
bool isAvailable() override
Definition: Service.h:77
ServiceFactoryFilter(std::shared_ptr< ServiceFactory< Pipeline, ReqB, RespB >> serviceFactory)
Definition: Service.h:120
ConstFactory(std::shared_ptr< Service< Req, Resp >> service)
Definition: Service.h:105
virtual bool isAvailable()
Definition: Service.h:41
Future< typename std::decay< T >::type > makeFuture(T &&t)
Definition: Future-inl.h:1310