trash-monitor: Rate limit updates

Trash monitor queries info from gvfsd-trash after each file monitor
change which can be problematic when too many changes happen in
a short time. Let's rate limit the number of queries...

Fixes: #1010
This commit is contained in:
Ondrej Holy 2020-05-21 21:14:47 -04:00 committed by Matthias Clasen
parent e9872d52d8
commit e8a120e5af

View File

@ -24,6 +24,8 @@
#include "gtkmarshalers.h"
#include "gtktrashmonitor.h"
#define UPDATE_RATE_SECONDS 1
struct _GtkTrashMonitor
{
GObject parent;
@ -31,6 +33,9 @@ struct _GtkTrashMonitor
GFileMonitor *file_monitor;
gulong file_monitor_changed_id;
gboolean pending;
gint timeout_id;
guint has_trash : 1;
};
@ -70,6 +75,10 @@ gtk_trash_monitor_dispose (GObject *object)
g_clear_object (&monitor->file_monitor);
}
if (monitor->timeout_id > 0)
g_source_remove (monitor->timeout_id);
monitor->timeout_id = 0;
G_OBJECT_CLASS (_gtk_trash_monitor_parent_class)->dispose (object);
}
@ -136,12 +145,38 @@ trash_query_info_cb (GObject *source,
g_object_unref (monitor); /* was reffed in recompute_trash_state() */
}
static void recompute_trash_state (GtkTrashMonitor *monitor);
static gboolean
recompute_trash_state_cb (gpointer data)
{
GtkTrashMonitor *monitor = data;
monitor->timeout_id = 0;
if (monitor->pending)
{
monitor->pending = FALSE;
recompute_trash_state (monitor);
}
return G_SOURCE_REMOVE;
}
/* Asynchronously recomputes whether there is trash or not */
static void
recompute_trash_state (GtkTrashMonitor *monitor)
{
GFile *file;
/* Rate limit the updates to not flood the gvfsd-trash when too many changes
* happended in a short time.
*/
if (monitor->timeout_id > 0)
{
monitor->pending = TRUE;
return;
}
file = g_file_new_for_uri ("trash:///");
g_file_query_info_async (file,
G_FILE_ATTRIBUTE_TRASH_ITEM_COUNT,
@ -149,6 +184,10 @@ recompute_trash_state (GtkTrashMonitor *monitor)
G_PRIORITY_DEFAULT, NULL,
trash_query_info_cb, g_object_ref (monitor));
monitor->timeout_id = g_timeout_add_seconds (UPDATE_RATE_SECONDS,
recompute_trash_state_cb,
monitor);
g_object_unref (file);
}
@ -173,6 +212,8 @@ _gtk_trash_monitor_init (GtkTrashMonitor *monitor)
file = g_file_new_for_uri ("trash:///");
monitor->file_monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, NULL);
monitor->pending = FALSE;
monitor->timeout_id = 0;
g_object_unref (file);