From 62f9409ba3e114b40b6923808290c02bf5af3d2c Mon Sep 17 00:00:00 2001 From: Weiyi Wang Date: Mon, 17 Sep 2018 14:28:58 -0400 Subject: web_backend: protect jwt cache with a mutex --- src/web_service/web_backend.cpp | 2 ++ src/web_service/web_backend.h | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) (limited to 'src/web_service') diff --git a/src/web_service/web_backend.cpp b/src/web_service/web_backend.cpp index 3a3f44dc2..5df4df5eb 100644 --- a/src/web_service/web_backend.cpp +++ b/src/web_service/web_backend.cpp @@ -24,6 +24,7 @@ Client::JWTCache Client::jwt_cache{}; Client::Client(const std::string& host, const std::string& username, const std::string& token) : host(host), username(username), token(token) { + std::lock_guard lock(jwt_cache.mutex); if (username == jwt_cache.username && token == jwt_cache.token) { jwt = jwt_cache.jwt; } @@ -116,6 +117,7 @@ void Client::UpdateJWT() { if (result.result_code != Common::WebResult::Code::Success) { LOG_ERROR(WebService, "UpdateJWT failed"); } else { + std::lock_guard lock(jwt_cache.mutex); jwt_cache.username = username; jwt_cache.token = token; jwt_cache.jwt = jwt = result.returned_data; diff --git a/src/web_service/web_backend.h b/src/web_service/web_backend.h index 549bcce29..d75fbcc15 100644 --- a/src/web_service/web_backend.h +++ b/src/web_service/web_backend.h @@ -5,7 +5,7 @@ #pragma once #include -#include +#include #include #include #include @@ -81,6 +81,7 @@ private: std::unique_ptr cli; struct JWTCache { + std::mutex mutex; std::string username; std::string token; std::string jwt; -- cgit v1.2.3