Skip to content

Commit

Permalink
device/lldp: use GSource for tracking ratelimit in NMLldpListener
Browse files Browse the repository at this point in the history
The reason is my dislike of these guint source ids. What is their
advantage anyway? Just use the GSource pointers.
  • Loading branch information
thom311 committed Apr 16, 2021
1 parent 655dd13 commit a5f3644
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/core/devices/nm-lldp-listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ struct _NMLldpListener {
gpointer notify_user_data;

/* the timestamp in nsec until which we delay updates. */
gint64 ratelimit_next_nsec;
guint ratelimit_id;
GSource *ratelimit_source;
gint64 ratelimit_next_nsec;

int ifindex;
};
Expand Down Expand Up @@ -812,32 +812,34 @@ data_changed_timeout(gpointer user_data)
{
NMLldpListener *self = user_data;

self->ratelimit_id = 0;
nm_clear_g_source_inst(&self->ratelimit_source);
self->ratelimit_next_nsec = nm_utils_get_monotonic_timestamp_nsec() + MIN_UPDATE_INTERVAL_NSEC;
data_changed_notify(self);
return G_SOURCE_REMOVE;
return G_SOURCE_CONTINUE;
}

static void
data_changed_schedule(NMLldpListener *self)
{
gint64 now_nsec;

if (self->ratelimit_id != 0)
if (self->ratelimit_source)
return;

now_nsec = nm_utils_get_monotonic_timestamp_nsec();
if (now_nsec < self->ratelimit_next_nsec) {
self->ratelimit_id =
g_timeout_add_full(G_PRIORITY_LOW,
NM_UTILS_NSEC_TO_MSEC_CEIL(self->ratelimit_next_nsec - now_nsec),
data_changed_timeout,
self,
NULL);
return;
self->ratelimit_source = nm_g_timeout_source_new(
NM_UTILS_NSEC_TO_MSEC_CEIL(self->ratelimit_next_nsec - now_nsec),
G_PRIORITY_LOW,
data_changed_timeout,
self,
NULL);
} else {
self->ratelimit_source =
nm_g_idle_source_new(G_PRIORITY_LOW, data_changed_timeout, self, NULL);
}

self->ratelimit_id = g_idle_add_full(G_PRIORITY_LOW, data_changed_timeout, self, NULL);
g_source_attach(self->ratelimit_source, NULL);
}

static void
Expand Down Expand Up @@ -1012,7 +1014,7 @@ nm_lldp_listener_destroy(NMLldpListener *self)
sd_lldp_detach_event(self->lldp_handle);
sd_lldp_unref(self->lldp_handle);

nm_clear_g_source(&self->ratelimit_id);
nm_clear_g_source_inst(&self->ratelimit_source);

g_hash_table_destroy(self->lldp_neighbors);

Expand Down

0 comments on commit a5f3644

Please sign in to comment.