<p>Andre Barbosa has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/16364">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">media_cache: Don't lock when curl the remote file<br><br>When playing a remote sound file, which is not in cache, first we need<br>to download it with ast_bucket_file_retrieve.<br><br>This can take a while if the remote host is slow. The current CURL<br>timeout is 180secs, so in extreme situations, it can take 3 minutes to<br>return.<br><br>Because ast_media_cache_retrieve has a lock on all function, while we<br>are waiting for the delayed download, Asterisk is not able to play any<br>more files, even the files already cached locally.<br><br>ASTERISK-29544 #close<br><br>Change-Id: I8d4142b463ae4a1d4c41bff2bf63324821567408<br>---<br>M main/media_cache.c<br>1 file changed, 25 insertions(+), 2 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/64/16364/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/main/media_cache.c b/main/media_cache.c</span><br><span>index 1899fb4..7e1cbe3 100644</span><br><span>--- a/main/media_cache.c</span><br><span>+++ b/main/media_cache.c</span><br><span>@@ -157,13 +157,15 @@</span><br><span>     char *file_path, size_t len)</span><br><span> {</span><br><span>    struct ast_bucket_file *bucket_file;</span><br><span style="color: hsl(120, 100%, 40%);">+  struct ast_bucket_file *tmp_bucket_file;</span><br><span>     char *ext;</span><br><span style="color: hsl(0, 100%, 40%);">-      SCOPED_AO2LOCK(media_lock, media_cache);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span>     if (ast_strlen_zero(uri)) {</span><br><span>          return -1;</span><br><span>   }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_lock(media_cache);</span><br><span style="color: hsl(120, 100%, 40%);">+        ast_debug(5, "Looking for media at local cache, file: %s\n", uri);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>       /* First, retrieve from the ao2 cache here. If we find a bucket_file</span><br><span>          * matching the requested URI, ask the appropriate backend if it is</span><br><span>   * stale. If not; return it.</span><br><span>@@ -179,6 +181,7 @@</span><br><span>                   ao2_ref(bucket_file, -1);</span><br><span> </span><br><span>                        ast_debug(5, "Returning media at local file: %s\n", file_path);</span><br><span style="color: hsl(120, 100%, 40%);">+                     ao2_unlock(media_cache);</span><br><span>                     return 0;</span><br><span>            }</span><br><span> </span><br><span>@@ -187,6 +190,10 @@</span><br><span>                 ast_bucket_file_delete(bucket_file);</span><br><span>                 ao2_ref(bucket_file, -1);</span><br><span>    }</span><br><span style="color: hsl(120, 100%, 40%);">+     /* We unlock to retrieve the file, because it can take a long time;</span><br><span style="color: hsl(120, 100%, 40%);">+    * and we don't want to lock access to cached files while waiting</span><br><span style="color: hsl(120, 100%, 40%);">+  */</span><br><span style="color: hsl(120, 100%, 40%);">+   ao2_unlock(media_cache);</span><br><span> </span><br><span>         /* Either this is new or the resource is stale; do a full retrieve</span><br><span>    * from the appropriate bucket_file backend</span><br><span>@@ -197,6 +204,21 @@</span><br><span>           return -1;</span><br><span>   }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+ /* we lock again, before updating cache */</span><br><span style="color: hsl(120, 100%, 40%);">+    ao2_lock(media_cache);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      /* We can have duplicated buckets here, we check if already exists</span><br><span style="color: hsl(120, 100%, 40%);">+     * before saving</span><br><span style="color: hsl(120, 100%, 40%);">+       */</span><br><span style="color: hsl(120, 100%, 40%);">+   tmp_bucket_file = ao2_find(media_cache, uri, OBJ_SEARCH_KEY | OBJ_NOLOCK);</span><br><span style="color: hsl(120, 100%, 40%);">+    if (tmp_bucket_file) {</span><br><span style="color: hsl(120, 100%, 40%);">+                ao2_ref(tmp_bucket_file, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+         ast_bucket_file_delete(bucket_file);</span><br><span style="color: hsl(120, 100%, 40%);">+          ao2_ref(bucket_file, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+             ao2_unlock(media_cache);</span><br><span style="color: hsl(120, 100%, 40%);">+              return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>  /* We can manipulate the 'immutable' bucket_file here, as we haven't</span><br><span>      * let anyone know of its existence yet</span><br><span>       */</span><br><span>@@ -210,6 +232,7 @@</span><br><span>    ao2_ref(bucket_file, -1);</span><br><span> </span><br><span>        ast_debug(5, "Returning media at local file: %s\n", file_path);</span><br><span style="color: hsl(120, 100%, 40%);">+     ao2_unlock(media_cache);</span><br><span> </span><br><span>         return 0;</span><br><span> }</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/16364">change 16364</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/16364"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 18 </div>
<div style="display:none"> Gerrit-Change-Id: I8d4142b463ae4a1d4c41bff2bf63324821567408 </div>
<div style="display:none"> Gerrit-Change-Number: 16364 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Andre Barbosa <andre.emanuel.barbosa@gmail.com> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>