Merge pull request #834 from kiwix/concurrency_safe_suggestion_endpoint

This commit is contained in:
Matthieu Gautier 2022-10-18 17:00:02 +02:00 committed by GitHub
commit 7feef320d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 4 deletions

View File

@ -633,6 +633,21 @@ std::unique_ptr<Response> InternalServer::build_homepage(const RequestContext& r
* Archive and Zim handlers begin * Archive and Zim handlers begin
**/ **/
class InternalServer::LockableSuggestionSearcher : public zim::SuggestionSearcher
{
public:
explicit LockableSuggestionSearcher(const zim::Archive& archive)
: zim::SuggestionSearcher(archive)
{}
std::unique_lock<std::mutex> getLock() {
return std::unique_lock<std::mutex>(m_mutex);
}
virtual ~LockableSuggestionSearcher() = default;
private:
std::mutex m_mutex;
};
std::unique_ptr<Response> InternalServer::handle_suggest(const RequestContext& request) std::unique_ptr<Response> InternalServer::handle_suggest(const RequestContext& request)
{ {
if (m_verbose.load()) { if (m_verbose.load()) {
@ -676,8 +691,9 @@ std::unique_ptr<Response> InternalServer::handle_suggest(const RequestContext& r
/* Get the suggestions */ /* Get the suggestions */
auto searcher = suggestionSearcherCache.getOrPut(bookId, auto searcher = suggestionSearcherCache.getOrPut(bookId,
[=](){ return make_shared<zim::SuggestionSearcher>(*archive); } [=](){ return make_shared<LockableSuggestionSearcher>(*archive); }
); );
const auto lock(searcher->getLock());
auto search = searcher->suggest(queryString); auto search = searcher->suggest(queryString);
auto srs = search.getResults(start, count); auto srs = search.getResults(start, count);

View File

@ -88,9 +88,6 @@ class SearchInfo {
typedef kainjow::mustache::data MustacheData; typedef kainjow::mustache::data MustacheData;
typedef ConcurrentCache<SearchInfo, std::shared_ptr<zim::Search>> SearchCache;
typedef ConcurrentCache<std::string, std::shared_ptr<zim::SuggestionSearcher>> SuggestionSearcherCache;
class OPDSDumper; class OPDSDumper;
class InternalServer { class InternalServer {
@ -157,6 +154,11 @@ class InternalServer {
bool isLocallyCustomizedResource(const std::string& url) const; bool isLocallyCustomizedResource(const std::string& url) const;
private: // types
class LockableSuggestionSearcher;
typedef ConcurrentCache<SearchInfo, std::shared_ptr<zim::Search>> SearchCache;
typedef ConcurrentCache<std::string, std::shared_ptr<LockableSuggestionSearcher>> SuggestionSearcherCache;
private: // data private: // data
std::string m_addr; std::string m_addr;
int m_port; int m_port;