1 /* 2 * nghttp2 - HTTP/2 C Library 3 * 4 * Copyright (c) 2016 Tatsuhiro Tsujikawa 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining 7 * a copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sublicense, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be 15 * included in all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 */ 25 #ifndef SHRPX_LIVE_CHECK_H 26 #define SHRPX_LIVE_CHECK_H 27 28 #include "shrpx.h" 29 30 #include <functional> 31 #include <random> 32 33 #include <openssl/ssl.h> 34 35 #include <ev.h> 36 37 #include <nghttp2/nghttp2.h> 38 39 #include "shrpx_connection.h" 40 41 namespace shrpx { 42 43 class Worker; 44 struct DownstreamAddr; 45 struct DNSQuery; 46 47 class LiveCheck { 48 public: 49 LiveCheck(struct ev_loop *loop, SSL_CTX *ssl_ctx, Worker *worker, 50 DownstreamAddr *addr, std::mt19937 &gen); 51 ~LiveCheck(); 52 53 void disconnect(); 54 55 void on_success(); 56 void on_failure(); 57 58 int initiate_connection(); 59 60 // Schedules next connection attempt 61 void schedule(); 62 63 // Low level I/O operation callback; they are called from do_read() 64 // or do_write(). 65 int noop(); 66 int connected(); 67 int tls_handshake(); 68 int read_tls(); 69 int write_tls(); 70 int read_clear(); 71 int write_clear(); 72 73 int do_read(); 74 int do_write(); 75 76 // These functions are used to feed / extract data to 77 // nghttp2_session object. 78 int on_read(const uint8_t *data, size_t len); 79 int on_write(); 80 81 // Call this function when HTTP/2 connection was established. We 82 // don't call this function for HTTP/1 at the moment. 83 int connection_made(); 84 85 void start_settings_timer(); 86 void stop_settings_timer(); 87 88 // Call this function when SETTINGS ACK was received from server. 89 void settings_ack_received(); 90 91 void signal_write(); 92 93 private: 94 Connection conn_; 95 DefaultMemchunks wb_; 96 std::mt19937 &gen_; 97 ev_timer backoff_timer_; 98 ev_timer settings_timer_; 99 std::function<int(LiveCheck &)> read_, write_; 100 Worker *worker_; 101 // nullptr if no TLS is configured 102 SSL_CTX *ssl_ctx_; 103 // Address of remote endpoint 104 DownstreamAddr *addr_; 105 nghttp2_session *session_; 106 // Actual remote address used to contact backend. This is initially 107 // nullptr, and may point to either &addr_->addr, or 108 // resolved_addr_.get(). 109 const Address *raddr_; 110 // Resolved IP address if dns parameter is used 111 std::unique_ptr<Address> resolved_addr_; 112 std::unique_ptr<DNSQuery> dns_query_; 113 // The number of successful connect attempt in a row. 114 size_t success_count_; 115 // The number of unsuccessful connect attempt in a row. 116 size_t fail_count_; 117 // true when SETTINGS ACK has been received from server. 118 bool settings_ack_received_; 119 // true when GOAWAY has been queued. 120 bool session_closing_; 121 }; 122 123 } // namespace shrpx 124 125 #endif // SHRPX_LIVE_CHECK_H 126