Add timeout options to config file
This allows users to specify the minimum wait for each request. The (currently wrong) estimate of retry_after / rate_limit is calculated, and the max between that and the setting in the config file is used.
This commit is contained in:
parent
7da3f81c27
commit
e24ebcf8e8
6 changed files with 74 additions and 14 deletions
|
@ -7,3 +7,9 @@ backend=sqlite
|
|||
fetch_extra_delay=30
|
||||
api_key=my_key_here
|
||||
store_raw_json=true
|
||||
|
||||
[timing]
|
||||
items=604800
|
||||
icons=604800
|
||||
shops=310
|
||||
creators=360
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
|
||||
#include "app_config.hpp"
|
||||
#include "orotool_config.hpp"
|
||||
#include "duckhandy/int_conv.hpp"
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
@ -51,6 +52,22 @@ namespace {
|
|||
constexpr const char g_store_raw_json[] = "store_raw_json";
|
||||
constexpr const char g_store_raw_json_def[] = "false";
|
||||
|
||||
constexpr const char g_items_time_sect[] = "timing";
|
||||
constexpr const char g_items_time[] = "items";
|
||||
constexpr const char g_items_time_def[] = "604800";
|
||||
|
||||
constexpr const char g_icons_time_sect[] = "timing";
|
||||
constexpr const char g_icons_time[] = "icons";
|
||||
constexpr const char g_icons_time_def[] = "604800";
|
||||
|
||||
constexpr const char g_shops_time_sect[] = "timing";
|
||||
constexpr const char g_shops_time[] = "shops";
|
||||
constexpr const char g_shops_time_def[] = "600";
|
||||
|
||||
constexpr const char g_creators_time_sect[] = "timing";
|
||||
constexpr const char g_creators_time[] = "creators";
|
||||
constexpr const char g_creators_time_def[] = "600";
|
||||
|
||||
bool equal (std::string_view a, std::string_view b) {
|
||||
return a.size() == b.size() and std::equal(
|
||||
a.begin(), a.end(),
|
||||
|
@ -73,6 +90,11 @@ namespace {
|
|||
);
|
||||
}
|
||||
|
||||
double to_double (std::string_view val) {
|
||||
const auto seconds = dhandy::int_conv<unsigned int>(val);
|
||||
return static_cast<double>(seconds);
|
||||
}
|
||||
|
||||
std::string whole_ini() {
|
||||
std::ifstream input(g_config_file_path);
|
||||
input >> std::noskipws;
|
||||
|
@ -166,4 +188,24 @@ bool AppConfig::store_raw_json() const {
|
|||
return to_bool(val);
|
||||
}
|
||||
|
||||
double AppConfig::items_timeout() const {
|
||||
std::string_view val = value_ifp(m_ini, g_items_time_sect, g_items_time, g_items_time_def, false);
|
||||
return to_double(val);
|
||||
}
|
||||
|
||||
double AppConfig::icons_timeout() const {
|
||||
std::string_view val = value_ifp(m_ini, g_icons_time_sect, g_icons_time, g_icons_time_def, false);
|
||||
return to_double(val);
|
||||
}
|
||||
|
||||
double AppConfig::shops_timeout() const {
|
||||
std::string_view val = value_ifp(m_ini, g_shops_time_sect, g_shops_time, g_shops_time_def, false);
|
||||
return to_double(val);
|
||||
}
|
||||
|
||||
double AppConfig::creators_timeout() const {
|
||||
std::string_view val = value_ifp(m_ini, g_creators_time_sect, g_creators_time, g_creators_time_def, false);
|
||||
return to_double(val);
|
||||
}
|
||||
|
||||
} //namespace duck
|
||||
|
|
|
@ -35,6 +35,11 @@ public:
|
|||
std::string_view backend() const;
|
||||
bool store_raw_json() const;
|
||||
|
||||
double items_timeout() const;
|
||||
double icons_timeout() const;
|
||||
double shops_timeout() const;
|
||||
double creators_timeout() const;
|
||||
|
||||
private:
|
||||
kamokan::IniFile m_ini;
|
||||
};
|
||||
|
|
|
@ -74,10 +74,15 @@ void test(oro::Api* api, oro::OriginsDB* db, const AppConfig& app_conf) {
|
|||
auto sig_int = worker.make_event<SignalInt>(&worker);
|
||||
const bool rj = app_conf.store_raw_json();
|
||||
|
||||
auto timer_items = worker.make_event<TimerItems>(TSet{0.0, ed, rj}, &pool, api, db);
|
||||
auto timer_icons = worker.make_event<TimerIcons>(TSet{5.0, ed, rj}, &pool, api, db);
|
||||
auto timer_shops = worker.make_event<TimerShops>(TSet{10.0, ed, rj}, &pool, api, db);
|
||||
auto timer_creat = worker.make_event<TimerCreators>(TSet{15.0, ed, rj}, &pool, api, db);
|
||||
const double w1 = app_conf.items_timeout();
|
||||
const double w2 = app_conf.icons_timeout();
|
||||
const double w3 = app_conf.shops_timeout();
|
||||
const double w4 = app_conf.creators_timeout();
|
||||
|
||||
auto timer_items = worker.make_event<TimerItems>(TSet{w1, ed, rj}, &pool, api, db);
|
||||
auto timer_icons = worker.make_event<TimerIcons>(TSet{w2, ed, rj}, &pool, api, db);
|
||||
auto timer_shops = worker.make_event<TimerShops>(TSet{w3, ed, rj}, &pool, api, db);
|
||||
auto timer_creat = worker.make_event<TimerCreators>(TSet{w4, ed, rj}, &pool, api, db);
|
||||
|
||||
worker.wait();
|
||||
#if !defined(NDEBUG)
|
||||
|
|
|
@ -27,15 +27,15 @@ namespace duck {
|
|||
|
||||
namespace {
|
||||
[[gnu::pure]]
|
||||
unsigned long time_interval (const oro::Header& header, double extra) {
|
||||
return header.retry_after / header.rate_limit + extra;
|
||||
unsigned long time_interval (const oro::Header& header, double min_wait, double extra) {
|
||||
return std::max(header.retry_after / header.rate_limit + extra, min_wait);
|
||||
}
|
||||
|
||||
oro::Timestamp calc_next_update (const oro::Header& header, double extra) {
|
||||
oro::Timestamp calc_next_update (const oro::Header& header, double min_wait, double extra) {
|
||||
oro::Timestamp ret;
|
||||
ret.ts =
|
||||
std::chrono::time_point_cast<oro::timestamp_t::duration>(std::chrono::system_clock::now()) +
|
||||
std::chrono::seconds(time_interval(header, extra));
|
||||
std::chrono::seconds(time_interval(header, min_wait, extra));
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -60,8 +60,9 @@ TimerBase::TimerBase (
|
|||
oro::Api* oro_api,
|
||||
oro::OriginsDB* db
|
||||
) :
|
||||
eve::Timer(initial_timer(*db, type, settings.min_wait), ctx),
|
||||
eve::Timer(initial_timer(*db, type, 2.0), ctx),
|
||||
m_extra_delay(settings.extra_delay),
|
||||
m_min_wait(settings.min_wait),
|
||||
m_pool(pool),
|
||||
m_oro_api(oro_api),
|
||||
m_db(db),
|
||||
|
@ -76,7 +77,7 @@ void TimerBase::on_timer() {
|
|||
}
|
||||
|
||||
void TimerBase::set_next_timer (const oro::Header& header) {
|
||||
const unsigned long next_timer = time_interval(header, m_extra_delay);
|
||||
const unsigned long next_timer = time_interval(header, m_min_wait, m_extra_delay);
|
||||
std::cout << "Next timer in " << next_timer << " secs\n";
|
||||
this->set_timer(static_cast<double>(next_timer));
|
||||
}
|
||||
|
@ -97,19 +98,19 @@ oro::OriginsDB& TimerBase::db() {
|
|||
}
|
||||
|
||||
void TimerBase::update_db (const oro::Shops& shops, const oro::Header& header, const oro::Source& source) {
|
||||
db().update(shops, calc_next_update(header, m_extra_delay), source);
|
||||
db().update(shops, calc_next_update(header, m_min_wait, m_extra_delay), source);
|
||||
}
|
||||
|
||||
void TimerBase::update_db (const oro::Items& items, const oro::Header& header, const oro::Source& source) {
|
||||
db().update(items, calc_next_update(header, m_extra_delay), source);
|
||||
db().update(items, calc_next_update(header, m_min_wait, m_extra_delay), source);
|
||||
}
|
||||
|
||||
void TimerBase::update_db (const oro::Icons& icons, const oro::Header& header, const oro::Source& source) {
|
||||
db().update(icons, calc_next_update(header, m_extra_delay), source);
|
||||
db().update(icons, calc_next_update(header, m_min_wait, m_extra_delay), source);
|
||||
}
|
||||
|
||||
void TimerBase::update_db (const oro::Creators& creators, const oro::Header& header, const oro::Source& source) {
|
||||
db().update(creators, calc_next_update(header, m_extra_delay), source);
|
||||
db().update(creators, calc_next_update(header, m_min_wait, m_extra_delay), source);
|
||||
}
|
||||
|
||||
} //namespace duck
|
||||
|
|
|
@ -78,6 +78,7 @@ private:
|
|||
virtual void fetch_data(bool with_raw) = 0;
|
||||
|
||||
double m_extra_delay;
|
||||
double m_min_wait;
|
||||
roar11::ThreadPool* m_pool;
|
||||
oro::Api* m_oro_api;
|
||||
oro::OriginsDB* m_db;
|
||||
|
|
Loading…
Reference in a new issue