private static bool TryLookupUrl(UrlType urlType, out string url) { url = null; // Once the internet has been found missing, don't bother trying it again for the duration of the program. if (!_internetAvailable) { return(false); } try { using (var s3Client = new BloomS3Client(null)) { s3Client.Timeout = TimeSpan.FromMilliseconds(2500.0); s3Client.ReadWriteTimeout = TimeSpan.FromMilliseconds(3000.0); s3Client.MaxErrorRetry = 1; var jsonContent = s3Client.DownloadFile(BloomS3Client.BloomDesktopFiles, kUrlLookupFileName); Urls urls = JsonConvert.DeserializeObject <Urls>(jsonContent); url = urls.GetUrlById(urlType.ToJsonPropertyString()); if (!string.IsNullOrWhiteSpace(url)) { return(true); } Logger.WriteEvent("Unable to look up URL type " + urlType); } } catch (Exception e) { _internetAvailable = false; Logger.WriteEvent("Exception while attempting look up of URL type " + urlType + ": " + e); } return(false); }
private static bool TryGetUrlDataFromServer() { // Once the internet has been found missing, don't bother trying it again for the duration of the program. // And if we got the data once, it's very unlikely we'll get something new by trying again. if (!_internetAvailable || _gotJsonFromServer) { return(false); } // It's pathologically possible that two threads at about the same time come here and both send // the query. If so, no great harm done...they'll both put the same values into the dictionary. // And in practice, it won't happen...one call to this, and only one, happens very early in // Bloom's startup code, and after that _gotJsonFromServer will be true. // I don't think it's worth the effort to set up locks and guarantee that only on thread // sends the request. try { using (var s3Client = new BloomS3Client(null)) { s3Client.Timeout = TimeSpan.FromMilliseconds(2500.0); s3Client.ReadWriteTimeout = TimeSpan.FromMilliseconds(3000.0); s3Client.MaxErrorRetry = 1; var jsonContent = s3Client.DownloadFile(BloomS3Client.BloomDesktopFiles, kUrlLookupFileName); Urls urls = JsonConvert.DeserializeObject <Urls>(jsonContent); // cache them all, so we don't have to repeat the server request. foreach (UrlType urlType in Enum.GetValues(typeof(UrlType))) { var url = urls.GetUrlById(urlType.ToJsonPropertyString()); if (!string.IsNullOrWhiteSpace(url)) { s_liveUrlCache.AddOrUpdate(urlType, url, (type, s) => s); } } // Do this only after we populated the dictionary; we definitely don't want // another thread to return false because it thinks things are already loaded // when the value it wanted isn't in the dictionary. _gotJsonFromServer = true; return(true); // we did the retrieval, it's worth checking the dictionary again. } } catch (Exception e) { _internetAvailable = false; var msg = $"Exception while attempting get URL data from server"; Logger.WriteEvent($"{msg}: {e.Message}"); NonFatalProblem.ReportSentryOnly(e, msg); } return(false); }