]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Add function to reschedule events at specific timer
authorVsevolod Stakhov <vsevolod@rspamd.com>
Tue, 23 Apr 2024 14:03:25 +0000 (15:03 +0100)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Tue, 23 Apr 2024 14:03:25 +0000 (15:03 +0100)
src/libutil/libev_helper.c
src/libutil/libev_helper.h

index 3b880aaa269c9e32e3c024d62b2abf028a40f939..203e1ed73fb89e3c0e0d6be6f544e2d98c489a90 100644 (file)
@@ -108,4 +108,34 @@ void rspamd_ev_watcher_reschedule(struct ev_loop *loop,
                        ev_timer_start(EV_A, &ev->tm);
                }
        }
+}
+
+void rspamd_ev_watcher_reschedule_at(struct ev_loop *loop,
+                                                                        struct rspamd_io_ev *ev,
+                                                                        short what,
+                                                                        ev_tstamp at)
+{
+       g_assert(ev->cb != NULL);
+
+       if (ev_can_stop(&ev->io)) {
+               ev_io_stop(EV_A, &ev->io);
+               ev_io_set(&ev->io, ev->io.fd, what);
+               ev_io_start(EV_A, &ev->io);
+       }
+       else {
+               ev->io.data = ev;
+               ev_io_init(&ev->io, rspamd_ev_watcher_io_cb, ev->io.fd, what);
+               ev_io_start(EV_A, &ev->io);
+       }
+
+       if (at > 0) {
+               if (!(ev_can_stop(&ev->tm))) {
+                       /* Update timestamp to avoid timers running early */
+                       ev_now_update_if_cheap(loop);
+
+                       ev->tm.data = ev;
+                       ev_timer_init(&ev->tm, rspamd_ev_watcher_timer_cb, at, 0.0);
+                       ev_timer_start(EV_A, &ev->tm);
+               }
+       }
 }
\ No newline at end of file
index 44d1604b05db4e42cdedad50eaa74b5175ee3c17..d68f179516d2cf0385aebf92e93cfcda8606b9ea 100644 (file)
@@ -1,11 +1,11 @@
-/*-
- * Copyright 2019 Vsevolod Stakhov
+/*
+ * Copyright 2024 Vsevolod Stakhov
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *   http://www.apache.org/licenses/LICENSE-2.0
+ *    http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
@@ -79,6 +79,17 @@ void rspamd_ev_watcher_reschedule(struct ev_loop *loop,
                                                                  struct rspamd_io_ev *ev,
                                                                  short what);
 
+/**
+ * Convenience function to reschedule watcher with different events and different timeout
+ * @param loop
+ * @param ev
+ * @param what
+ */
+void rspamd_ev_watcher_reschedule_at(struct ev_loop *loop,
+                                                                        struct rspamd_io_ev *ev,
+                                                                        short what,
+                                                                        ev_tstamp at);
+
 #ifdef __cplusplus
 }
 #endif