PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/client-common/cache/MonitorCache.cpp

https://gitlab.com/admin-github-cloud/cynara
C++ | 64 lines | 29 code | 11 blank | 24 comment | 7 complexity | 93d83288ca0182fa4df8f03536f47e9d MD5 | raw file
  1. /*
  2. * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License
  15. */
  16. /**
  17. * @file src/client-common/cache/CapacityCache.h
  18. * @author Aleksander Zdyb <a.zdyb@samsung.com>
  19. * @version 1.0
  20. * @brief This file contains monitor cache implementation
  21. */
  22. #include <ctime>
  23. #include <cynara-error.h>
  24. #include <log/log.h>
  25. #include "MonitorCache.h"
  26. namespace Cynara {
  27. MonitorCache::MonitorCache(ClockFunction clockFunction)
  28. : m_clockFunction(clockFunction) {}
  29. void MonitorCache::update(const PolicyKey &policyKey, int result) {
  30. struct timespec ts;
  31. // https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=da15cfdae03351c689736f8d142618592e3cebc3
  32. auto ret = m_clockFunction(CLOCK_REALTIME_COARSE, &ts);
  33. if (ret != 0) {
  34. LOGE("Could not update monitor entries. clock_gettime() failed with [%d]", ret);
  35. // TODO: Decide what to do. Something very bad must have happened, if clock_gettime() failed
  36. return;
  37. }
  38. // We trust the client to deny access on errors
  39. if (result != CYNARA_API_ACCESS_ALLOWED)
  40. result = CYNARA_API_ACCESS_DENIED;
  41. m_monitorEntries.push_back({ policyKey, result, ts });
  42. m_cacheAgeSec = ts.tv_sec - m_monitorEntries.at(0).timestamp().tv_sec;
  43. }
  44. void MonitorCache::clear() {
  45. m_monitorEntries.clear();
  46. m_cacheAgeSec = 0;
  47. }
  48. bool MonitorCache::shouldFlush() {
  49. if (m_monitorEntries.size() == 0)
  50. return false;
  51. return m_monitorEntries.size() >= CACHE_CAPACITY || m_cacheAgeSec >= MAX_LOG_AGE_SEC;
  52. }
  53. } // namespace Cynara