2016-04-07 18:04:16 +02:00
|
|
|
/*
|
|
|
|
* 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_LIVE_CHECK_H
|
|
|
|
#define SHRPX_LIVE_CHECK_H
|
|
|
|
|
|
|
|
#include "shrpx.h"
|
|
|
|
|
|
|
|
#include <functional>
|
2016-04-08 15:46:00 +02:00
|
|
|
#include <random>
|
2016-04-07 18:04:16 +02:00
|
|
|
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
|
|
|
|
#include <ev.h>
|
|
|
|
|
2016-05-20 17:30:54 +02:00
|
|
|
#include <nghttp2/nghttp2.h>
|
|
|
|
|
2016-04-07 18:04:16 +02:00
|
|
|
#include "shrpx_connection.h"
|
|
|
|
|
|
|
|
namespace shrpx {
|
|
|
|
|
|
|
|
class Worker;
|
|
|
|
struct DownstreamAddr;
|
2016-12-04 15:43:41 +01:00
|
|
|
struct DNSQuery;
|
2016-04-07 18:04:16 +02:00
|
|
|
|
|
|
|
class LiveCheck {
|
|
|
|
public:
|
|
|
|
LiveCheck(struct ev_loop *loop, SSL_CTX *ssl_ctx, Worker *worker,
|
2016-05-24 16:36:43 +02:00
|
|
|
DownstreamAddr *addr, std::mt19937 &gen);
|
2016-04-07 18:04:16 +02:00
|
|
|
~LiveCheck();
|
|
|
|
|
|
|
|
void disconnect();
|
|
|
|
|
|
|
|
void on_success();
|
|
|
|
void on_failure();
|
|
|
|
|
|
|
|
int initiate_connection();
|
|
|
|
|
2016-04-08 15:48:09 +02:00
|
|
|
// Schedules next connection attempt
|
2016-04-07 18:04:16 +02:00
|
|
|
void schedule();
|
|
|
|
|
|
|
|
// Low level I/O operation callback; they are called from do_read()
|
|
|
|
// or do_write().
|
|
|
|
int noop();
|
|
|
|
int connected();
|
|
|
|
int tls_handshake();
|
2016-05-20 17:30:54 +02:00
|
|
|
int read_tls();
|
|
|
|
int write_tls();
|
|
|
|
int read_clear();
|
|
|
|
int write_clear();
|
2016-04-07 18:04:16 +02:00
|
|
|
|
|
|
|
int do_read();
|
|
|
|
int do_write();
|
|
|
|
|
2016-05-20 17:30:54 +02:00
|
|
|
// These functions are used to feed / extract data to
|
|
|
|
// nghttp2_session object.
|
|
|
|
int on_read(const uint8_t *data, size_t len);
|
|
|
|
int on_write();
|
|
|
|
|
|
|
|
// Call this function when HTTP/2 connection was established. We
|
|
|
|
// don't call this function for HTTP/1 at the moment.
|
|
|
|
int connection_made();
|
|
|
|
|
|
|
|
void start_settings_timer();
|
|
|
|
void stop_settings_timer();
|
|
|
|
|
|
|
|
// Call this function when SETTINGS ACK was received from server.
|
|
|
|
void settings_ack_received();
|
|
|
|
|
|
|
|
void signal_write();
|
|
|
|
|
2016-04-07 18:04:16 +02:00
|
|
|
private:
|
|
|
|
Connection conn_;
|
2016-05-20 17:30:54 +02:00
|
|
|
DefaultMemchunks wb_;
|
2016-04-08 15:46:00 +02:00
|
|
|
std::mt19937 &gen_;
|
2016-04-07 18:04:16 +02:00
|
|
|
ev_timer backoff_timer_;
|
2016-05-20 17:30:54 +02:00
|
|
|
ev_timer settings_timer_;
|
2016-04-07 18:04:16 +02:00
|
|
|
std::function<int(LiveCheck &)> read_, write_;
|
|
|
|
Worker *worker_;
|
|
|
|
// nullptr if no TLS is configured
|
|
|
|
SSL_CTX *ssl_ctx_;
|
|
|
|
// Address of remote endpoint
|
|
|
|
DownstreamAddr *addr_;
|
2016-05-20 17:30:54 +02:00
|
|
|
nghttp2_session *session_;
|
2016-12-04 15:43:41 +01:00
|
|
|
// Actual remote address used to contact backend. This is initially
|
|
|
|
// nullptr, and may point to either &addr_->addr, or
|
|
|
|
// resolved_addr_.get().
|
|
|
|
const Address *raddr_;
|
|
|
|
// Resolved IP address if dns parameter is used
|
|
|
|
std::unique_ptr<Address> resolved_addr_;
|
|
|
|
std::unique_ptr<DNSQuery> dns_query_;
|
2016-04-07 18:04:16 +02:00
|
|
|
// The number of successful connect attempt in a row.
|
|
|
|
size_t success_count_;
|
|
|
|
// The number of unsuccessful connect attempt in a row.
|
|
|
|
size_t fail_count_;
|
2016-05-20 17:30:54 +02:00
|
|
|
// true when SETTINGS ACK has been received from server.
|
|
|
|
bool settings_ack_received_;
|
|
|
|
// true when GOAWAY has been queued.
|
|
|
|
bool session_closing_;
|
2016-04-07 18:04:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace shrpx
|
|
|
|
|
|
|
|
#endif // SHRPX_LIVE_CHECK_H
|