You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

symcache_periodic.hxx 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*-
  2. * Copyright 2022 Vsevolod Stakhov
  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. #ifndef RSPAMD_SYMCACHE_PERIODIC_HXX
  17. #define RSPAMD_SYMCACHE_PERIODIC_HXX
  18. #pragma once
  19. #include "config.h"
  20. #include "contrib/libev/ev.h"
  21. #include "symcache_internal.hxx"
  22. #include "worker_util.h"
  23. namespace rspamd::symcache {
  24. struct cache_refresh_cbdata {
  25. private:
  26. symcache *cache;
  27. struct ev_loop *event_loop;
  28. struct rspamd_worker *w;
  29. double reload_time;
  30. double last_resort;
  31. ev_timer resort_ev;
  32. public:
  33. explicit cache_refresh_cbdata(symcache *_cache,
  34. struct ev_loop *_ev_base,
  35. struct rspamd_worker *_w)
  36. : cache(_cache), event_loop(_ev_base), w(_w)
  37. {
  38. auto log_tag = [&]() { return cache->log_tag(); };
  39. last_resort = rspamd_get_ticks(TRUE);
  40. reload_time = cache->get_reload_time();
  41. auto tm = rspamd_time_jitter(reload_time, 0);
  42. msg_debug_cache("next reload in %.2f seconds", tm);
  43. ev_timer_init(&resort_ev, cache_refresh_cbdata::resort_cb,
  44. tm, tm);
  45. resort_ev.data = (void *) this;
  46. ev_timer_start(event_loop, &resort_ev);
  47. rspamd_mempool_add_destructor(cache->get_pool(),
  48. cache_refresh_cbdata::refresh_dtor, (void *) this);
  49. }
  50. static void refresh_dtor(void *d)
  51. {
  52. auto *cbdata = (struct cache_refresh_cbdata *) d;
  53. delete cbdata;
  54. }
  55. static void resort_cb(EV_P_ ev_timer *w, int _revents)
  56. {
  57. auto *cbdata = (struct cache_refresh_cbdata *) w->data;
  58. auto log_tag = [&]() { return cbdata->cache->log_tag(); };
  59. if (rspamd_worker_is_primary_controller(cbdata->w)) {
  60. /* Plan new event */
  61. auto tm = rspamd_time_jitter(cbdata->reload_time, 0);
  62. msg_debug_cache("resort symbols cache, next reload in %.2f seconds", tm);
  63. cbdata->resort_ev.repeat = tm;
  64. ev_timer_again(EV_A_ w);
  65. auto cur_time = rspamd_get_ticks(FALSE);
  66. cbdata->cache->periodic_resort(cbdata->event_loop, cur_time, cbdata->last_resort);
  67. cbdata->last_resort = cur_time;
  68. }
  69. }
  70. private:
  71. ~cache_refresh_cbdata()
  72. {
  73. ev_timer_stop(event_loop, &resort_ev);
  74. }
  75. };
  76. }// namespace rspamd::symcache
  77. #endif//RSPAMD_SYMCACHE_PERIODIC_HXX