34#include "BESTimeoutError.h"
35#include "RequestServiceTimer.h"
45using namespace std::chrono;
48#define prolog string("RequestServiceTimer::").append(__func__).append("() - ")
51static std::once_flag d_rst_init_once;
60 std::call_once(d_rst_init_once,RequestServiceTimer::initialize_instance);
64void RequestServiceTimer::initialize_instance() {
67 atexit(delete_instance);
71void RequestServiceTimer::delete_instance() {
85 std::lock_guard<std::recursive_mutex> lock_me(d_rst_lock_mutex);
87 if(timeout_ms > milliseconds{0}){
88 timeout_enabled =
true;
89 d_bes_timeout = timeout_ms;
92 timeout_enabled =
false;
93 d_bes_timeout = milliseconds{0};
95 start_time = steady_clock::now();
103 std::lock_guard<std::recursive_mutex> lock_me(d_rst_lock_mutex);
104 return duration_cast<milliseconds>(steady_clock::now() - start_time);
115 std::lock_guard<std::recursive_mutex> lock_me(d_rst_lock_mutex);
117 BESDEBUG(MODULE, prolog <<
"init remaining: " <<
remaining.count() << endl);
118 if (timeout_enabled) {
119 BESDEBUG(MODULE, prolog <<
"timeout enabled" << endl);
123 BESDEBUG(MODULE, prolog <<
"timeout disabled" << endl);
134 std::lock_guard<std::recursive_mutex> lock_me(d_rst_lock_mutex);
135 return timeout_enabled && (
remaining() <= milliseconds {0});
142 std::lock_guard<std::recursive_mutex> lock_me(d_rst_lock_mutex);
143 timeout_enabled =
false;
146string RequestServiceTimer::dump(
bool pretty)
const {
147 std::stringstream ss;
148 if(!pretty){ ss<<
"["; }
149 ss <<
"RequestServiceTimer(" << (
void *)
this <<
") - ";
150 if(pretty){ ss << endl <<
" "; }
151 ss <<
"bes_timeout: " << d_bes_timeout.count() <<
"ms ";
152 if(pretty){ ss << endl <<
" "; }
153 ss <<
"start_time: " << duration_cast<seconds>(start_time.time_since_epoch()).count() <<
"s ";
154 if(pretty){ ss << endl <<
" "; }
155 ss <<
"is_timeout_enabled(): " << (is_timeout_enabled() ?
"true ":
"false ");
156 if(pretty){ ss << endl <<
" "; }
157 ss <<
"elapsed(): " <<
elapsed().count() <<
"ms ";
158 if(pretty){ ss << endl <<
" "; }
159 ss <<
"remaining(): " <<
remaining().count() <<
"ms ";
160 if(pretty){ ss << endl <<
" "; }
161 ss <<
"is_expired(): " << (
is_expired()?
"true":
"false");
162 if(pretty){ ss << endl; }
else{ ss <<
"]"; }
172void RequestServiceTimer::dump( ostream &strm )
const
174 strm << dump() << endl;
190 double time_out_seconds = d_bes_timeout.count()/1000.00;
191 std::stringstream errMsg;
192 errMsg <<
"The request that you submitted timed out. The server was unable to begin transmitting a response in ";
193 errMsg <<
"the time allowed. Requests processed by this server must begin transmitting a response in less ";
194 errMsg <<
"than " << time_out_seconds <<
" seconds. ";
196 errMsg <<
"Some things you can try: Reissue the request but change the amount of data requested. ";
197 errMsg <<
"You may reduce the size of the request by choosing just the variables you need and/or by ";
198 errMsg <<
"using the DAP index based array sub-setting syntax to additionally limit the amount of data requested. ";
199 errMsg <<
"You can also try requesting a different encoding for the response. If you asked for the response ";
200 errMsg <<
"to be encoded as a NetCDF-3 or NetCDF-4 file be aware that these response encodings are not " <<
201 "streamable. In order ";
202 errMsg <<
"to build these responses the server must write the entire response to a temporary file before it can ";
203 errMsg <<
"begin to send the response to the requesting client. Changing to a different encoding, such as DAP4 ";
204 errMsg <<
"data, may allow the server to successfully respond to your request. ";
206 errMsg <<
"The service component that ran out of time said: " << message;
error thrown if there is a user syntax error in the request or any other user error
The master request service timer for this server; a singleton.
std::chrono::milliseconds remaining() const
If the time_out is enabled returns the time remaining. If the time_out is disabled returns 0.
static RequestServiceTimer * TheTimer()
Return a pointer to a singleton timer instance. If an instance does not exist it will create and init...
std::chrono::milliseconds elapsed() const
Return the time duration in milliseconds since the timer was started.
void throw_if_timeout_expired(const std::string &message, const std::string &file, const int line)
Checks the RequestServiceTimer to determine if the time spent servicing the request at this point has...
bool is_expired() const
if the time_out is disabled return false.
void start(std::chrono::milliseconds timeout_ms)
Set/Reset the timer start_time to now().
void disable_timeout()
Set the time_out is disabled.