TensorFlow Serving C++ API Documentation
evhttp_server.h
1 /* Copyright 2018 Google Inc. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7  http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 // libevent based server implementation
17 
18 #ifndef TENSORFLOW_SERVING_UTIL_NET_HTTP_SERVER_INTERNAL_EVHTTP_SERVER_H_
19 #define TENSORFLOW_SERVING_UTIL_NET_HTTP_SERVER_INTERNAL_EVHTTP_SERVER_H_
20 
21 #include <cstdint>
22 #include <ctime>
23 #include <memory>
24 #include <unordered_map>
25 #include <vector>
26 
27 #include "absl/base/thread_annotations.h"
28 #include "absl/synchronization/mutex.h"
29 
30 #include "absl/synchronization/notification.h"
31 
32 #include "tensorflow_serving/util/net_http/server/internal/evhttp_request.h"
33 #include "tensorflow_serving/util/net_http/server/internal/server_support.h"
34 #include "tensorflow_serving/util/net_http/server/public/httpserver_interface.h"
35 
36 struct event_base;
37 struct evhttp;
38 struct evhttp_bound_socket;
39 struct evhttp_request;
40 
41 namespace tensorflow {
42 namespace serving {
43 namespace net_http {
44 
46  public:
47  virtual ~EvHTTPServer();
48 
49  EvHTTPServer(const EvHTTPServer& other) = delete;
50  EvHTTPServer& operator=(const EvHTTPServer& other) = delete;
51 
52  explicit EvHTTPServer(std::unique_ptr<ServerOptions> options);
53 
54  bool Initialize();
55 
56  bool StartAcceptingRequests() override;
57 
58  bool is_accepting_requests() const override;
59 
60  int listen_port() const override;
61 
62  void Terminate() override;
63 
64  bool is_terminating() const override;
65 
66  void WaitForTermination() override;
67 
68  bool WaitForTerminationWithTimeout(absl::Duration timeout) override;
69 
70  void RegisterRequestHandler(absl::string_view uri, RequestHandler handler,
71  const RequestHandlerOptions& options) override;
72 
73  void RegisterRequestDispatcher(RequestDispatcher dispatcher,
74  const RequestHandlerOptions& options) override;
75 
76  void IncOps() override;
77  void DecOps() override;
78 
79  bool EventLoopSchedule(std::function<void()> fn) override;
80 
81  private:
82  static void DispatchEvRequestFn(struct evhttp_request* req, void* server);
83 
84  void DispatchEvRequest(struct evhttp_request* req);
85 
86  void ScheduleHandlerReference(const RequestHandler& handler,
87  EvHTTPRequest* ev_request)
88  ABSL_EXCLUSIVE_LOCKS_REQUIRED(request_mu_);
89  void ScheduleHandler(RequestHandler&& handler, EvHTTPRequest* ev_request)
90  ABSL_EXCLUSIVE_LOCKS_REQUIRED(request_mu_);
91 
92  struct UriHandlerInfo {
93  public:
94  UriHandlerInfo(absl::string_view uri_in, RequestHandler handler_in,
95  const RequestHandlerOptions& options_in);
96  const std::string uri;
97  const RequestHandler handler;
98  const RequestHandlerOptions options;
99  };
100 
101  struct DispatcherInfo {
102  public:
103  DispatcherInfo(RequestDispatcher dispatcher_in,
104  const RequestHandlerOptions& options_in);
105 
106  const RequestDispatcher dispatcher;
107  const RequestHandlerOptions options;
108  };
109 
110  std::unique_ptr<ServerOptions> server_options_;
111 
112  // Started accepting requests.
113  absl::Notification accepting_requests_;
114  // Listener port
115  int port_ = 0;
116 
117  // Started terminating the server, i.e. Terminate() has been called
118  absl::Notification terminating_;
119 
120  // Tracks the # of pending operations
121  mutable absl::Mutex ops_mu_;
122  int64_t num_pending_ops_ ABSL_GUARDED_BY(ops_mu_) = 0;
123 
124  mutable absl::Mutex request_mu_;
125  std::unordered_map<std::string, UriHandlerInfo> uri_handlers_
126  ABSL_GUARDED_BY(request_mu_);
127  std::vector<DispatcherInfo> dispatchers_ ABSL_GUARDED_BY(request_mu_);
128 
129  // ev instances
130  event_base* ev_base_ = nullptr;
131  evhttp* ev_http_ = nullptr;
132  evhttp_bound_socket* ev_listener_ = nullptr;
133 
134  // Timeval used to register immediate callbacks, which are called
135  // in the order that they are registered.
136  const timeval* immediate_;
137 };
138 
139 } // namespace net_http
140 } // namespace serving
141 } // namespace tensorflow
142 
143 #endif // TENSORFLOW_SERVING_UTIL_NET_HTTP_SERVER_INTERNAL_EVHTTP_SERVER_H_