[banshee/stable-2.4] PlaylistParser: Retry HTTP request after timeout (bgo#662909)



commit ec65d60c3e503f9232b9fab1be56d9f408d04d1d
Author: Olivier Dufour <olivier duff gmail com>
Date:   Thu Mar 22 22:26:56 2012 +0100

    PlaylistParser: Retry HTTP request after timeout (bgo#662909)
    
    It seems a lot of radio stations have trouble responding on the first
    query, but will work fine if you retry right after. So if the HTTP
    request for a playlist times out, we'll now wait for a second
    and then retry. We'll retry 3 times maximum.
    
    Signed-off-by: Bertrand Lorentz <bertrand lorentz gmail com>

 .../Banshee.Playlists.Formats/PlaylistParser.cs    |   69 +++++++++++++-------
 1 files changed, 46 insertions(+), 23 deletions(-)
---
diff --git a/src/Core/Banshee.Services/Banshee.Playlists.Formats/PlaylistParser.cs b/src/Core/Banshee.Services/Banshee.Playlists.Formats/PlaylistParser.cs
index 6394563..6dcad3a 100644
--- a/src/Core/Banshee.Services/Banshee.Playlists.Formats/PlaylistParser.cs
+++ b/src/Core/Banshee.Services/Banshee.Playlists.Formats/PlaylistParser.cs
@@ -52,6 +52,7 @@ namespace Banshee.Playlists.Formats
         private List<Dictionary<string, object>> elements;
         private Uri base_uri = null;
         private string title = null;
+        private readonly int HTTP_REQUEST_RETRIES = 3;
 
         public PlaylistParser ()
         {
@@ -77,30 +78,8 @@ namespace Banshee.Playlists.Formats
                 if (uri.Scheme == "file") {
                     stream = Banshee.IO.File.OpenRead (uri);
                 } else if (uri.Scheme == "http") {
-                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create (uri.AbsoluteUri);
-                    request.UserAgent = Banshee.Web.Browser.UserAgent;
-                    request.KeepAlive = false;
-                    request.Timeout = 5 * 1000;
-                    request.AllowAutoRedirect = true;
-
-                    // Parse out and set credentials, if any
-                    string user_info = new Uri (uri.AbsoluteUri).UserInfo;
-                    if (!String.IsNullOrEmpty (user_info)) {
-                        string username = String.Empty;
-                        string password = String.Empty;
-                        int cIndex = user_info.IndexOf (":");
-                        if (cIndex != -1) {
-                            username = user_info.Substring (0, cIndex);
-                            password = user_info.Substring (cIndex + 1);
-                        } else {
-                            username = user_info;
-                        }
-                        request.Credentials = new NetworkCredential (username, password);
-                    }
-
-                    response = (HttpWebResponse)request.GetResponse ();
+                    response = Download (uri, HTTP_REQUEST_RETRIES);
                     web_stream = response.GetResponseStream ();
-
                     try {
                         stream = new MemoryStream ();
 
@@ -169,6 +148,50 @@ namespace Banshee.Playlists.Formats
             }
         }
 
+        private HttpWebResponse Download (SafeUri uri, int nb_retries)
+        {
+            Hyena.ThreadAssist.AssertNotInMainThread ();
+
+            while (true) {
+                try {
+                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create (uri.AbsoluteUri);
+                    request.UserAgent = Banshee.Web.Browser.UserAgent;
+                    request.KeepAlive = false;
+                    request.Timeout = 5 * 1000;
+                    request.AllowAutoRedirect = true;
+
+                    // Parse out and set credentials, if any
+                    string user_info = new Uri (uri.AbsoluteUri).UserInfo;
+                    if (!String.IsNullOrEmpty (user_info)) {
+                        string username = String.Empty;
+                        string password = String.Empty;
+                        int cIndex = user_info.IndexOf (":");
+                        if (cIndex != -1) {
+                            username = user_info.Substring (0, cIndex);
+                            password = user_info.Substring (cIndex + 1);
+                        } else {
+                            username = user_info;
+                        }
+                        request.Credentials = new NetworkCredential (username, password);
+                    }
+
+                    var response = (HttpWebResponse)request.GetResponse ();
+                    if (response.StatusCode == HttpStatusCode.GatewayTimeout) {
+                        throw new WebException ("", WebExceptionStatus.Timeout);
+                    }
+                    return response;
+                } catch (WebException e) {
+                    if (e.Status == WebExceptionStatus.Timeout && nb_retries > 0) {
+                        nb_retries--;
+                        Log.InformationFormat ("Playlist download from {0} timed out, retrying in 1 second...", uri.AbsoluteUri);
+                        System.Threading.Thread.Sleep (1000);
+                    } else {
+                        throw;
+                    }
+                }
+            }
+        }
+
         public List<Dictionary<string, object>> Elements {
             get { return elements; }
         }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]