From 755c506d21af5ae14092d9bcc8b673d0982ae25d Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Thu, 4 Feb 2021 10:32:46 +0100
Subject: [PATCH] MOD: Don't be greedy on the db and harddrive while importing
 favorites

When importing favorites, the db lock will be always taken by the repo
module, this is bad for lektord because the main thread will no longer
be able to lock and use the db. It will also use constantly the hard
drive.

An easy and dirty fix is to sleep a bit...
---
 src/module/module_repo.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/src/module/module_repo.c b/src/module/module_repo.c
index 3969e2ac..0fef2151 100644
--- a/src/module/module_repo.c
+++ b/src/module/module_repo.c
@@ -102,6 +102,17 @@ struct __file {
  * Private functions *
  *********************/
 
+/* Sleep a bit, to not overuse ressources */
+static inline void
+__sleep(void)
+{
+    struct timespec time_sleep = {
+        .tv_sec  = 0,
+        .tv_nsec = 100000000L,
+    }; /* Sleep for 0.1s */
+    nanosleep(&time_sleep, NULL); /* Sleep a bit, better for Hard drive */
+}
+
 /* Recursive mkdir, where the last word of the string is a file, not a folder. */
 static inline void
 __mkdir(const char *dir, unsigned int umask)
@@ -393,11 +404,7 @@ __handle_got_json_internal_callback(const char *key, const char *val, int comp,
 
     /* The `void *user` is complete */
     else if (comp) {
-        struct timespec time_sleep = {
-            .tv_sec  = 0,
-            .tv_nsec = 100000000L,
-        }; /* Sleep for 0.1s */
-        nanosleep(&time_sleep, NULL); /* Sleep a bit, better for Hard drive */
+        __sleep();
 
         long filestamp  = 0;
         int current_id  = 0;
@@ -559,6 +566,8 @@ __handle_fav_list_internal(const char UNUSED *key, const char *val, int UNUSED c
         LOG_ERROR("REPO", "Failed to add kara %ld to playlist %s", uri->uri.id, uri->fav);
         return;
     }
+    /* Dirty fix for db lock and hard drive usage */
+    __sleep();
 
 }
 
-- 
GitLab