bes  Updated for version 3.20.13
RequestServiceTimer.h
1 // RequestServiceTimer.h
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2022 OPeNDAP, Inc
7 // Authors:
8 // ndp Nathan Potter <ndp@opendap.org>
9 // dan Dan Holloway <dholloway@opendap.org>
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Lesser General Public
13 // License as published by the Free Software Foundation; either
14 // version 2.1 of the License, or (at your option) any later version.
15 //
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 // Lesser General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License along with this library; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 //
25 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
26 #ifndef I_RequestServiceTimer_h
27 #define I_RequestServiceTimer_h 1
28 
29 #include <map>
30 #include <string>
31 #include <mutex>
32 #include <chrono>
33 
34 
35 //#include "BESObj.h"
36 
42 private:
43  static RequestServiceTimer *d_instance;
44  mutable std::recursive_mutex d_rst_lock_mutex;
45 
46  std::chrono::milliseconds d_bes_timeout{0};
47  std::chrono::steady_clock::time_point start_time{std::chrono::steady_clock::now()};
48  bool timeout_enabled{false};
49 
50  RequestServiceTimer()=default;
51  ~RequestServiceTimer()=default;
52 
53  static void delete_instance();
54  static void initialize_instance();
55 
56 public:
57 
58  static RequestServiceTimer *TheTimer();
59 
60  void start(std::chrono::milliseconds timeout_ms);
61 
62  std::chrono::steady_clock::time_point get_start_time() const { return start_time; }
63 
64  std::chrono::milliseconds elapsed() const;
65 
66  std::chrono::milliseconds remaining() const;
67 
68  bool is_timeout_enabled() const { return timeout_enabled; }
69 
70  bool is_expired() const;
71 
72  void disable_timeout();
73 
74  std::string dump(bool pretty=false) const ;
75 
76  void dump( std::ostream &strm ) const ;
77 
78  void throw_if_timeout_expired(const std::string &message, const std::string &file, const int line);
79 
80 };
81 
82 #endif // I_RequestServiceTimer_h
83 
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.