diff --git a/lib/httpcheck.js b/lib/httpcheck.js index 37980d6..b56bda6 100644 --- a/lib/httpcheck.js +++ b/lib/httpcheck.js @@ -78,12 +78,12 @@ var HttpChecker = { } }, - processResultsCallback: function( serverArrayIndex, rtt, http_code ) { + processResultsCallback: function( serverArrayIndex, rtt, ttfb, http_code ) { var server = arrCheck[ serverArrayIndex ]; server.processed = true; server.lastCheck = new Date().valueOf(); // we use set the value to the milliseconds value - if ( rtt > 0 && 400 > http_code && 0 != http_code ) + if ( rtt > 0 && ttfb > 0 && 400 > http_code && 0 != http_code ) server.site_status = SITE_RUNNING; else if ( ( SITE_RUNNING == server.oldStatus ) || ( SITE_CONFIRMED_DOWN != server.oldStatus ) && ( new Date().valueOf() < ( server.last_status_change + ( config.get( 'TIME_BETWEEN_NOTICES_MIN' ) * MINUTES ) ) ) ) @@ -99,6 +99,7 @@ var HttpChecker = { resO.status = server.site_status; resO.rtt = Math.round( rtt / 1000 ); resO.code = http_code; + resO.ttfb = Math.round( ttfb / 1000 ); server.checks.push( resO ); // if site is down and it has not been confirmed diff --git a/src/http_checker.cpp b/src/http_checker.cpp index 36afb67..8c77a78 100644 --- a/src/http_checker.cpp +++ b/src/http_checker.cpp @@ -2,11 +2,13 @@ #include "http_checker.h" #include +using namespace std::chrono; using namespace std; + HTTP_Checker::HTTP_Checker() : m_sock( -1 ), m_host_name( "" ), m_host_dir( "" ), m_port( HTTP_DEFAULT_PORT ), m_is_ssl( false ), m_triptime( 0 ), m_response_code( 0 ), m_ctx( NULL ), m_ssl( NULL ), m_sbio( NULL ) { - gettimeofday( &m_tstart, &m_tzone ); + m_tstart = high_resolution_clock::now(); memset( m_buf, 0, MAX_TCP_BUFFER ); m_cutofftime = time( NULL ); m_cutofftime += NET_COMMS_TIMEOUT; @@ -16,16 +18,8 @@ HTTP_Checker::~HTTP_Checker() { this->disconnect(); } -time_t HTTP_Checker::get_rtt() { - struct timeval m_tend; - gettimeofday( &m_tend, &m_tzone ); - - if ( (m_tend.tv_usec -= m_tstart.tv_usec) < 0 ) { - m_tend.tv_sec--; - m_tend.tv_usec += 1000000; - } - m_tend.tv_sec -= m_tstart.tv_sec; - return m_tend.tv_sec * 1000000 + ( m_tend.tv_usec ); +int HTTP_Checker::get_rtt() { + return duration_cast(high_resolution_clock::now() - m_tstart).count(); } void HTTP_Checker::check( string p_host_name, int p_port ) { @@ -215,6 +209,7 @@ string HTTP_Checker::get_response() { } while ( ( FD_ISSET( m_sock, &read_fds ) == 0) && ( m_cutofftime > time( NULL ) ) ); if ( FD_ISSET( m_sock, &read_fds) ) { + m_ttfb = duration_cast(high_resolution_clock::now() - m_tstart).count(); if ( m_is_ssl ) received = SSL_read( m_ssl, m_buf, MAX_TCP_BUFFER - 1 ); else diff --git a/src/http_checker.h b/src/http_checker.h index c9e4213..d7f71a6 100644 --- a/src/http_checker.h +++ b/src/http_checker.h @@ -16,6 +16,9 @@ #include #include #include +#include + + #include #include @@ -56,7 +59,8 @@ class HTTP_Checker { ~HTTP_Checker(); void check( std::string p_host_name, int p_port = HTTP_DEFAULT_PORT ); - time_t get_rtt(); + int get_rtt(); + int get_ttfb() { return m_ttfb; }; int get_response_code() { return m_response_code; } private: @@ -66,11 +70,11 @@ class HTTP_Checker { std::string m_host_dir; int m_port; bool m_is_ssl; - struct timezone m_tzone; - struct timeval m_tstart; + std::chrono::_V2::system_clock::time_point m_tstart; time_t m_triptime; time_t m_cutofftime; int m_response_code; + int m_ttfb; SSL_CTX *m_ctx; SSL *m_ssl; diff --git a/src/main.cpp b/src/main.cpp index 088edba..fbe76e5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,6 +9,7 @@ #include #include +#include using namespace v8; using namespace node; @@ -28,12 +29,13 @@ static void http_check_async_fin( uv_work_t *req, int status ) { HandleScope scope( isolate ); HTTP_Check_Baton *baton = static_cast(req->data); - Local argv[3] = { Number::New( isolate, baton->server_id ), + Local argv[4] = { Number::New( isolate, baton->server_id ), Number::New( isolate, baton->http_checker->get_rtt() ), + Number::New( isolate, baton->http_checker->get_ttfb() ), Number::New( isolate, baton->http_checker->get_response_code() ) }; Local cb_func = Local::New( isolate, baton->callback ); - cb_func->Call( isolate->GetCurrentContext(), isolate->GetCurrentContext()->Global(), 3, argv ); + cb_func->Call( isolate->GetCurrentContext(), isolate->GetCurrentContext()->Global(), 4, argv ); baton->callback.Reset(); delete baton->http_checker; delete baton; @@ -73,7 +75,7 @@ void http_check( const FunctionCallbackInfo& args ) { String::NewFromUtf8( isolate, "You have not provided a callback function as the 4th parameter" ).ToLocalChecked() ) ); return; } - + HTTP_Check_Baton *baton = new HTTP_Check_Baton(); HTTP_Checker *checker = new HTTP_Checker(); baton->http_checker = checker;