diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 41fbb68f..8e6b7d7b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -110,6 +110,7 @@ if(ENABLE_APP) shrpx_signal.cc shrpx_router.cc shrpx_api_downstream_connection.cc + shrpx_health_monitor_downstream_connection.cc ) if(HAVE_SPDYLAY) list(APPEND NGHTTPX_SRCS diff --git a/src/Makefile.am b/src/Makefile.am index 58f5f564..587b9e4b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -133,6 +133,8 @@ NGHTTPX_SRCS = \ shrpx_signal.cc shrpx_signal.h \ shrpx_router.cc shrpx_router.h \ shrpx_api_downstream_connection.cc shrpx_api_downstream_connection.h \ + shrpx_health_monitor_downstream_connection.cc \ + shrpx_health_monitor_downstream_connection.h \ buffer.h memchunk.h template.h allocator.h if HAVE_SPDYLAY diff --git a/src/shrpx.cc b/src/shrpx.cc index f6847bd3..ef95399c 100644 --- a/src/shrpx.cc +++ b/src/shrpx.cc @@ -1345,7 +1345,7 @@ Connections: Default: )" << DEFAULT_DOWNSTREAM_HOST << "," << DEFAULT_DOWNSTREAM_PORT << R"( - -f, --frontend=(,|unix:)[;no-tls] + -f, --frontend=(,|unix:)[[;PARAM]...] Set frontend host and port. If is '*', it assumes all addresses including both IPv4 and IPv6. UNIX domain socket can be specified by prefixing path @@ -1353,6 +1353,9 @@ Connections: This option can be used multiple times to listen to multiple addresses. + This option can take 0 or more parameters, which are + described below. + Optionally, TLS can be disabled by specifying "no-tls" parameter. TLS is enabled by default. @@ -1363,6 +1366,11 @@ Connections: break your services, or expose confidential information to the outside the world. + To make this frontend as health monitor endpoint, + specify "healthmon" parameter. This is disabled by + default. Any requests which come through this address + are replied with 200 HTTP status, without no body. + Default: *,3000 --backlog= Set listen backlog size. diff --git a/src/shrpx_client_handler.cc b/src/shrpx_client_handler.cc index 9ba99b9b..edd63e08 100644 --- a/src/shrpx_client_handler.cc +++ b/src/shrpx_client_handler.cc @@ -49,6 +49,7 @@ #include "shrpx_http2_session.h" #include "shrpx_connect_blocker.h" #include "shrpx_api_downstream_connection.h" +#include "shrpx_health_monitor_downstream_connection.h" #ifdef HAVE_SPDYLAY #include "shrpx_spdy_upstream.h" #endif // HAVE_SPDYLAY @@ -899,6 +900,8 @@ ClientHandler::get_downstream_connection(Downstream *downstream) { switch (faddr_->alt_mode) { case ALTMODE_API: return make_unique(worker_); + case ALTMODE_HEALTHMON: + return make_unique(); } // Fast path. If we have one group, it must be catch-all group. diff --git a/src/shrpx_config.cc b/src/shrpx_config.cc index 21f81232..3888aceb 100644 --- a/src/shrpx_config.cc +++ b/src/shrpx_config.cc @@ -630,6 +630,8 @@ int parse_upstream_params(UpstreamParams &out, const StringRef &src_params) { out.tls = false; } else if (util::strieq_l("api", param)) { out.alt_mode = ALTMODE_API; + } else if (util::strieq_l("healthmon", param)) { + out.alt_mode = ALTMODE_HEALTHMON; } else if (!param.empty()) { LOG(ERROR) << "frontend: " << param << ": unknown keyword"; return -1; diff --git a/src/shrpx_config.h b/src/shrpx_config.h index 347aa0b1..a60e6ea3 100644 --- a/src/shrpx_config.h +++ b/src/shrpx_config.h @@ -321,6 +321,8 @@ enum UpstreamAltMode { ALTMODE_NONE, // API processing mode ALTMODE_API, + // Health monitor mode + ALTMODE_HEALTHMON, }; struct UpstreamAddr { diff --git a/src/shrpx_health_monitor_downstream_connection.cc b/src/shrpx_health_monitor_downstream_connection.cc new file mode 100644 index 00000000..ddaf6db5 --- /dev/null +++ b/src/shrpx_health_monitor_downstream_connection.cc @@ -0,0 +1,107 @@ +/* + * nghttp2 - HTTP/2 C Library + * + * Copyright (c) 2016 Tatsuhiro Tsujikawa + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#include "shrpx_health_monitor_downstream_connection.h" + +#include "shrpx_client_handler.h" +#include "shrpx_upstream.h" +#include "shrpx_downstream.h" +//#include "shrpx_connection_handler.h" + +namespace shrpx { + +HealthMonitorDownstreamConnection::HealthMonitorDownstreamConnection() {} + +HealthMonitorDownstreamConnection::~HealthMonitorDownstreamConnection() {} + +int HealthMonitorDownstreamConnection::attach_downstream( + Downstream *downstream) { + if (LOG_ENABLED(INFO)) { + DCLOG(INFO, this) << "Attaching to DOWNSTREAM:" << downstream; + } + + downstream_ = downstream; + + return 0; +} + +void HealthMonitorDownstreamConnection::detach_downstream( + Downstream *downstream) { + if (LOG_ENABLED(INFO)) { + DCLOG(INFO, this) << "Detaching from DOWNSTREAM:" << downstream; + } + downstream_ = nullptr; +} + +int HealthMonitorDownstreamConnection::push_request_headers() { return 0; } + +int HealthMonitorDownstreamConnection::push_upload_data_chunk( + const uint8_t *data, size_t datalen) { + return 0; +} + +int HealthMonitorDownstreamConnection::end_upload_data() { + auto upstream = downstream_->get_upstream(); + auto &resp = downstream_->response(); + + resp.http_status = 200; + + resp.fs.add_header_token(StringRef::from_lit("content-length"), + StringRef::from_lit("0"), false, + http2::HD_CONTENT_LENGTH); + + if (upstream->send_reply(downstream_, nullptr, 0) != 0) { + return -1; + } + + return 0; +} + +void HealthMonitorDownstreamConnection::pause_read(IOCtrlReason reason) {} + +int HealthMonitorDownstreamConnection::resume_read(IOCtrlReason reason, + size_t consumed) { + return 0; +} + +void HealthMonitorDownstreamConnection::force_resume_read() {} + +int HealthMonitorDownstreamConnection::on_read() { return 0; } + +int HealthMonitorDownstreamConnection::on_write() { return 0; } + +void HealthMonitorDownstreamConnection::on_upstream_change(Upstream *uptream) {} + +bool HealthMonitorDownstreamConnection::poolable() const { return false; } + +DownstreamAddrGroup * +HealthMonitorDownstreamConnection::get_downstream_addr_group() const { + return nullptr; +} + +DownstreamAddr *HealthMonitorDownstreamConnection::get_addr() const { + return nullptr; +} + +} // namespace shrpx diff --git a/src/shrpx_health_monitor_downstream_connection.h b/src/shrpx_health_monitor_downstream_connection.h new file mode 100644 index 00000000..ddf30878 --- /dev/null +++ b/src/shrpx_health_monitor_downstream_connection.h @@ -0,0 +1,63 @@ +/* + * nghttp2 - HTTP/2 C Library + * + * Copyright (c) 2016 Tatsuhiro Tsujikawa + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#ifndef SHRPX_HEALTH_MONITOR_DOWNSTREAM_CONNECTION_H +#define SHRPX_HEALTH_MONITOR_DOWNSTREAM_CONNECTION_H + +#include "shrpx_downstream_connection.h" + +namespace shrpx { + +class Worker; + +class HealthMonitorDownstreamConnection : public DownstreamConnection { +public: + HealthMonitorDownstreamConnection(); + virtual ~HealthMonitorDownstreamConnection(); + virtual int attach_downstream(Downstream *downstream); + virtual void detach_downstream(Downstream *downstream); + + virtual int push_request_headers(); + virtual int push_upload_data_chunk(const uint8_t *data, size_t datalen); + virtual int end_upload_data(); + + virtual void pause_read(IOCtrlReason reason); + virtual int resume_read(IOCtrlReason reason, size_t consumed); + virtual void force_resume_read(); + + virtual int on_read(); + virtual int on_write(); + + virtual void on_upstream_change(Upstream *uptream); + + // true if this object is poolable. + virtual bool poolable() const; + + virtual DownstreamAddrGroup *get_downstream_addr_group() const; + virtual DownstreamAddr *get_addr() const; +}; + +} // namespace shrpx + +#endif // SHRPX_HEALTH_MONITOR_DOWNSTREAM_CONNECTION_H diff --git a/src/shrpx_http2_upstream.cc b/src/shrpx_http2_upstream.cc index 518335bb..08872fb5 100644 --- a/src/shrpx_http2_upstream.cc +++ b/src/shrpx_http2_upstream.cc @@ -415,7 +415,10 @@ void Http2Upstream::initiate_downstream(Downstream *downstream) { auto &req = downstream->request(); if (!req.http2_expect_body) { - downstream->end_upload_data(); + rv = downstream->end_upload_data(); + if (rv != 0) { + rst_stream(downstream, NGHTTP2_INTERNAL_ERROR); + } } return; @@ -443,7 +446,10 @@ int on_frame_recv_callback(nghttp2_session *session, const nghttp2_frame *frame, if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) { downstream->disable_upstream_rtimer(); - downstream->end_upload_data(); + if (downstream->end_upload_data() != 0) { + upstream->rst_stream(downstream, NGHTTP2_INTERNAL_ERROR); + } + downstream->set_request_state(Downstream::MSG_COMPLETE); } @@ -465,7 +471,10 @@ int on_frame_recv_callback(nghttp2_session *session, const nghttp2_frame *frame, if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) { downstream->disable_upstream_rtimer(); - downstream->end_upload_data(); + if (downstream->end_upload_data() != 0) { + upstream->rst_stream(downstream, NGHTTP2_INTERNAL_ERROR); + } + downstream->set_request_state(Downstream::MSG_COMPLETE); } diff --git a/src/shrpx_spdy_upstream.cc b/src/shrpx_spdy_upstream.cc index 0d09fccc..2d97100e 100644 --- a/src/shrpx_spdy_upstream.cc +++ b/src/shrpx_spdy_upstream.cc @@ -362,7 +362,9 @@ void SpdyUpstream::initiate_downstream(Downstream *downstream) { auto &req = downstream->request(); if (!req.http2_expect_body) { - downstream->end_upload_data(); + if (downstream->end_upload_data() != 0) { + rst_stream(downstream, SPDYLAY_INTERNAL_ERROR); + } } } @@ -445,7 +447,9 @@ void on_data_recv_callback(spdylay_session *session, uint8_t flags, } downstream->disable_upstream_rtimer(); - downstream->end_upload_data(); + if (downstream->end_upload_data() != 0) { + upstream->rst_stream(downstream, SPDYLAY_INTERNAL_ERROR); + } downstream->set_request_state(Downstream::MSG_COMPLETE); } }