Implemented Timeout for HTTP LIB

Fixes https://gitlab.com/kicad/code/kicad/-/issues/15756
This commit is contained in:
Andre Iwers 2024-01-08 23:20:23 +00:00 committed by Jon Evans
parent 92c58f2564
commit 047cb41a1d
4 changed files with 15 additions and 3 deletions

View File

@ -20,6 +20,7 @@
#include <wx/log.h>
#include <fmt/core.h>
#include <wx/translation.h>
#include <ctime>
#include <boost/algorithm/string.hpp>
#include <nlohmann/json.hpp>
@ -38,6 +39,7 @@ HTTP_LIB_CONNECTION::HTTP_LIB_CONNECTION( const HTTP_LIB_SOURCE& aSource, bool a
m_rootURL = aSource.root_url;
m_token = aSource.token;
m_sourceType = aSource.type;
m_timeout = aSource.timeout;
if( aTestConnectionNow )
{
@ -176,9 +178,9 @@ bool HTTP_LIB_CONNECTION::SelectOne( const std::string& aPartID, HTTP_LIB_PART&
return false;
}
// if the same part is selected again, use cached part
// if the same part is selected again and cache has not expired use cached part
// instead to minimise http requests.
if( m_cached_part.id == aPartID )
if( m_cached_part.id == aPartID && ( std::difftime( std::time( nullptr ), m_cached_part.lastCached ) < m_timeout ) )
{
aFetchedPart = m_cached_part;
return true;
@ -205,9 +207,12 @@ bool HTTP_LIB_CONNECTION::SelectOne( const std::string& aPartID, HTTP_LIB_PART&
std::string value = "";
// the id used to identify the part, the name is needed to show a human-readable
// part descirption to the user inside the symbol chooser dialog
// part description to the user inside the symbol chooser dialog
aFetchedPart.id = response.at( "id" );
// get a timestamp for caching
aFetchedPart.lastCached = std::time( nullptr );
// API might not want to return an optional name.
if( response.contains( "name" ) )
{

View File

@ -42,6 +42,8 @@ HTTP_LIB_SETTINGS::HTTP_LIB_SETTINGS( const std::string& aFilename ) :
m_params.emplace_back( new PARAM<std::string>( "source.root_url", &m_Source.root_url, "" ) );
m_params.emplace_back( new PARAM<std::string>( "source.token", &m_Source.token, "" ) );
m_params.emplace_back( new PARAM<int>( "source.timeout_seconds", &m_Source.timeout, 2 ) );
}

View File

@ -90,6 +90,7 @@ private:
std::string m_token;
std::string m_rootURL;
int m_timeout;
HTTP_LIB_SOURCE_TYPE m_sourceType;

View File

@ -21,6 +21,7 @@
#define KICAD_HTTP_LIB_SETTINGS_H
#include <settings/json_settings.h>
#include <ctime>
enum class HTTP_LIB_SOURCE_TYPE
@ -36,6 +37,7 @@ struct HTTP_LIB_SOURCE
std::string root_url;
std::string api_version;
std::string token;
int timeout;
};
@ -55,6 +57,8 @@ struct HTTP_LIB_PART
bool exclude_from_board = false;
bool exclude_from_sim = false;
std::time_t lastCached = 0;
std::map<std::string, std::tuple<std::string, bool>> fields; ///< additional generic fields
};