18 #ifndef TENSORFLOW_SERVING_UTIL_NET_HTTP_SERVER_INTERNAL_EVHTTP_SERVER_H_
19 #define TENSORFLOW_SERVING_UTIL_NET_HTTP_SERVER_INTERNAL_EVHTTP_SERVER_H_
24 #include <unordered_map>
27 #include "absl/base/thread_annotations.h"
28 #include "absl/synchronization/mutex.h"
30 #include "absl/synchronization/notification.h"
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"
38 struct evhttp_bound_socket;
39 struct evhttp_request;
41 namespace tensorflow {
52 explicit EvHTTPServer(std::unique_ptr<ServerOptions> options);
56 bool StartAcceptingRequests()
override;
58 bool is_accepting_requests()
const override;
60 int listen_port()
const override;
62 void Terminate()
override;
64 bool is_terminating()
const override;
66 void WaitForTermination()
override;
68 bool WaitForTerminationWithTimeout(absl::Duration timeout)
override;
70 void RegisterRequestHandler(absl::string_view uri, RequestHandler handler,
73 void RegisterRequestDispatcher(RequestDispatcher dispatcher,
76 void IncOps()
override;
77 void DecOps()
override;
79 bool EventLoopSchedule(std::function<
void()> fn)
override;
82 static void DispatchEvRequestFn(
struct evhttp_request* req,
void* server);
84 void DispatchEvRequest(
struct evhttp_request* req);
86 void ScheduleHandlerReference(
const RequestHandler& handler,
88 ABSL_EXCLUSIVE_LOCKS_REQUIRED(request_mu_);
89 void ScheduleHandler(RequestHandler&& handler,
EvHTTPRequest* ev_request)
90 ABSL_EXCLUSIVE_LOCKS_REQUIRED(request_mu_);
92 struct UriHandlerInfo {
94 UriHandlerInfo(absl::string_view uri_in, RequestHandler handler_in,
96 const std::string uri;
97 const RequestHandler handler;
101 struct DispatcherInfo {
103 DispatcherInfo(RequestDispatcher dispatcher_in,
106 const RequestDispatcher dispatcher;
110 std::unique_ptr<ServerOptions> server_options_;
113 absl::Notification accepting_requests_;
118 absl::Notification terminating_;
121 mutable absl::Mutex ops_mu_;
122 int64_t num_pending_ops_ ABSL_GUARDED_BY(ops_mu_) = 0;
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_);
130 event_base* ev_base_ =
nullptr;
131 evhttp* ev_http_ =
nullptr;
132 evhttp_bound_socket* ev_listener_ =
nullptr;
136 const timeval* immediate_;