Skip to content

Commit

Permalink
Timeout in WASM beekeeper should be checked only for current session
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariusz-Trela committed Nov 6, 2023
1 parent f31718e commit 8b53806
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 32 deletions.
4 changes: 4 additions & 0 deletions programs/beekeeper/core/include/core/time_manager_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class time_manager_base

session_data_index items;

bool run( const types::timepoint_t& now, const session_data& s_data, std::vector<std::string>& modified_items );
void modify_times( const std::vector<std::string>& modified_items );

protected:

virtual void send_auto_lock_error_message( const std::string& message ){ /*not implemented here*/ };
Expand All @@ -58,6 +61,7 @@ class time_manager_base
void change( const std::string& token, const types::timepoint_t& time, bool refresh_only_active );

void run();
void run( const std::string& token );

void close( const std::string& token );
};
Expand Down
2 changes: 1 addition & 1 deletion programs/beekeeper/core/session_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void session_base::set_timeout( const std::chrono::seconds& t )
void session_base::check_timeout()
{
FC_ASSERT( time );
time->run();
time->run( token );

refresh_timeout( true/*refresh_only_active*/ );
}
Expand Down
92 changes: 61 additions & 31 deletions programs/beekeeper/core/time_manager_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,48 +10,40 @@ time_manager_base::~time_manager_base()
{
}

void time_manager_base::run()
bool time_manager_base::run( const types::timepoint_t& now, const session_data& s_data, std::vector<std::string>& modified_items )
{
std::vector<std::string> _modified_items;

if( now >= s_data.time )
{
const auto& _now = std::chrono::system_clock::now();

auto& _idx_time = items.get<by_time>();
auto _it = _idx_time.begin();

while( _it != _idx_time.end() )
auto _microseconds = std::chrono::duration_cast<std::chrono::microseconds>( now - s_data.time );
if( _microseconds.count() / 1000 > 0 )
dlog("lock activated: actual time is longer ${differ}[ms] than timeout time", ("differ", _microseconds.count() / 1000));
else
dlog("lock activated: actual time is equals to timeout time");
auto _result = exception::exception_handler([&]()
{
modified_items.emplace_back( s_data.token );
return "";
}
);
if( !_result.second )
{
if( _now >= _it->time )
{
auto _microseconds = std::chrono::duration_cast<std::chrono::microseconds>( _now - _it->time );
if( _microseconds.count() / 1000 > 0 )
dlog("lock activated: actual time is longer ${differ}[ms] than timeout time", ("differ", _microseconds.count() / 1000));
else
dlog("lock activated: actual time is equals to timeout time");
auto _result = exception::exception_handler([&]()
{
_modified_items.emplace_back( _it->token );
return "";
}
);
if( !_result.second )
{
send_auto_lock_error_message( _result.first );
return; //First error during locking finishes whole procedure
}
}
++_it;
send_auto_lock_error_message( _result.first );
return false; //First error during locking finishes whole procedure
}
}

if( _modified_items.empty() )
return true;
}

void time_manager_base::modify_times( const std::vector<std::string>& modified_items )
{
if( modified_items.empty() )
return;

auto& _idx_token = items.get<by_token>();
auto _result = exception::exception_handler([&]()
{
for( auto& token : _modified_items )
for( auto& token : modified_items )
{
auto _found = _idx_token.find( token );
FC_ASSERT( _found != _idx_token.end() );
Expand All @@ -68,6 +60,42 @@ void time_manager_base::run()
send_auto_lock_error_message( _result.first );
}

void time_manager_base::run()
{
std::vector<std::string> _modified_items;

auto& _idx_time = items.get<by_time>();
auto _it = _idx_time.begin();

const auto& _now = std::chrono::system_clock::now();

while( _it != _idx_time.end() )
{
if( !run( _now, *_it, _modified_items ) )
return;
++_it;
}

modify_times( _modified_items );
}

void time_manager_base::run( const std::string& token )
{
std::vector<std::string> _modified_items;

auto& _idx = items.get<by_token>();
const auto& _found = _idx.find( token );

FC_ASSERT( _found != _idx.end() );

const auto& _now = std::chrono::system_clock::now();

if( !run( _now, *_found, _modified_items ) )
return;

modify_times( _modified_items );
}

void time_manager_base::add( const std::string& token, types::lock_method_type&& lock_method, types::notification_method_type&& notification_method )
{
auto& _idx = items.get<by_token>();
Expand All @@ -79,6 +107,8 @@ void time_manager_base::change( const std::string& token, const types::timepoint
auto& _idx = items.get<by_token>();
const auto& _found = _idx.find( token );

FC_ASSERT( _found != _idx.end() );

_idx.modify( _found, [&time, &refresh_only_active]( session_data &sd )
{
if( !refresh_only_active || ( refresh_only_active && sd.time != types::timepoint_t::max() ) )
Expand Down

0 comments on commit 8b53806

Please sign in to comment.