proxygen
Service.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree. An additional grant
7  * of patent rights can be found in the PATENTS file in the same directory.
8  *
9  */
10 #pragma once
11 
13 #include <list>
14 #include <memory>
15 
16 namespace proxygen {
17 
18 class ServiceWorker;
19 class RequestWorker;
20 
21 /*
22  * A Service object represents a network service running in proxygen.
23  *
24  * The Service object is primarily a construct used for managing startup and
25  * shutdown. The RunProxygen() call will accept a list of Service objects, and
26  * will invoke start() on each service to start them. When shutdown has been
27  * requested, it will invoke failHealthChecks() on each service followed (after
28  * some period of time) by stopAccepting().
29  *
30  * All of these methods are invoked from the main thread.
31  */
32 class Service {
33  public:
34  Service();
35  virtual ~Service();
36 
48  virtual void init(folly::EventBase* mainEventBase,
49  const std::list<RequestWorker*>& workers) = 0;
50 
51 
56  virtual void finishInit() {}
57 
58 
67  virtual void startAccepting() {}
68 
76  virtual void failHealthChecks() {}
77 
86  virtual void stopAccepting() = 0;
87 
95  virtual void pauseListening() {}
96 
104  virtual void dropConnections(double /*pct*/) {}
105 
118  virtual void forceStop() {}
119 
126  virtual void initWorkerState(RequestWorker*) {}
127 
143  virtual void cleanupWorkerState(RequestWorker* /*worker*/) {}
144 
149  void addServiceWorker(std::unique_ptr<ServiceWorker> worker,
150  RequestWorker* reqWorker);
151 
155  const std::list<std::unique_ptr<ServiceWorker>>& getServiceWorkers() const {
156  return workers_;
157  }
158 
162  void clearServiceWorkers();
163 
165  workerEvbs_.push_back(evb);
166  }
167 
168  const std::vector<folly::EventBase*>& getWorkerEventBases() {
169  return workerEvbs_;
170  }
171 
172  private:
173  // Forbidden copy constructor and assignment opererator
174  Service(Service const &) = delete;
175  Service& operator=(Service const &) = delete;
176 
177  // Workers
178  std::list<std::unique_ptr<ServiceWorker>> workers_;
179  std::vector<folly::EventBase*> workerEvbs_;
180 };
181 
182 } // proxygen
virtual ~Service()
Definition: Service.cpp:20
virtual void pauseListening()
Definition: Service.h:95
const std::vector< folly::EventBase * > & getWorkerEventBases()
Definition: Service.h:168
const std::list< std::unique_ptr< ServiceWorker > > & getServiceWorkers() const
Definition: Service.h:155
virtual void init(folly::EventBase *mainEventBase, const std::list< RequestWorker * > &workers)=0
virtual void failHealthChecks()
Definition: Service.h:76
void addWorkerEventBase(folly::EventBase *evb)
Definition: Service.h:164
Service & operator=(Service const &)=delete
virtual void initWorkerState(RequestWorker *)
Definition: Service.h:126
std::list< std::unique_ptr< ServiceWorker > > workers_
Definition: Service.h:178
std::vector< folly::EventBase * > workerEvbs_
Definition: Service.h:179
virtual void finishInit()
Definition: Service.h:56
void addServiceWorker(std::unique_ptr< ServiceWorker > worker, RequestWorker *reqWorker)
Definition: Service.cpp:23
void clearServiceWorkers()
Definition: Service.cpp:29
virtual void stopAccepting()=0
virtual void startAccepting()
Definition: Service.h:67
virtual void cleanupWorkerState(RequestWorker *)
Definition: Service.h:143
virtual void dropConnections(double)
Definition: Service.h:104
virtual void forceStop()
Definition: Service.h:118