Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/httprequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,16 @@ std::string HttpRequest::url() const {
return _url;
}

double HttpRequest::timestamp() const {
// According to the std::chrono docs, we need at least 55 bit here.
long long since_epoch = std::chrono::duration_cast<std::chrono::microseconds>(
_timestamp.time_since_epoch()
).count();
// R's currentTime() returns a Unix timestamp with microseconds (or
// nanoseconds on supported platforms) tacked on.
return ((double) since_epoch) / 1e6;
}

const RequestHeaders& HttpRequest::headers() const {
return _headers;
}
Expand Down
11 changes: 10 additions & 1 deletion src/httprequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <map>
#include <iostream>
#include <chrono>

#include <functional>
#include <memory>
Expand Down Expand Up @@ -92,6 +93,10 @@ class HttpRequest : public WebSocketConnectionCallbacks,
};
LastHeaderState _last_header_state;

// Sys.time()-compatible timestamp, which can later be used to instrument
// roundtrip latency in R.
std::chrono::time_point<std::chrono::system_clock> _timestamp;

public:
HttpRequest(uv_loop_t* pLoop,
std::shared_ptr<WebApplication> pWebApplication,
Expand All @@ -106,7 +111,8 @@ class HttpRequest : public WebSocketConnectionCallbacks,
_is_upgrade(false),
_response_scheduled(false),
_handling_request(false),
_background_queue(backgroundQueue)
_background_queue(backgroundQueue),
_timestamp(std::chrono::system_clock::now())
{
ASSERT_BACKGROUND_THREAD()
uv_tcp_init(pLoop, &_handle.tcp);
Expand Down Expand Up @@ -166,6 +172,9 @@ class HttpRequest : public WebSocketConnectionCallbacks,
// pipelined HTTP requests.
void requestCompleted();

// Returns timestamp compatible with R/Def.h's currentTime().
double timestamp() const;

void _call_r_on_ws_open();
void _schedule_on_headers_complete_complete(std::shared_ptr<HttpResponse> pResponse);
void _on_headers_complete_complete(std::shared_ptr<HttpResponse> pResponse);
Expand Down
4 changes: 4 additions & 0 deletions src/webapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ void requestToEnv(std::shared_ptr<HttpRequest> pRequest, Rcpp::Environment* pEnv
env["rook.version"] = CharacterVector("1.1-0");
env["rook.url_scheme"] = CharacterVector("http");

NumericVector timestamp = NumericVector::create(pRequest->timestamp());
timestamp.attr("class") = CharacterVector({"POSIXct", "POSIXt"});
env["httpuv.timestamp"] = timestamp;

Address addr = pRequest->serverAddress();
env["SERVER_NAME"] = CharacterVector(addr.host);
std::ostringstream portstr;
Expand Down