private static async Task QueryLuisWithRetry()

in blog-samples/CSharp/TriviaBotSpeechSample/TriviaBot/Luis/QueryLuis.cs [50:88]


        private static async Task<LuisResult> QueryLuisWithRetry(
            string appid,
            string key,
            string query,
            int retries)
        {
            LuisResult result = null;
            Exception exception = null;
            int tooManyRequestsDelay = 125;

            for (var attempt = 0; attempt < retries; ++attempt)
            {
                exception = null;

                try
                {
                    result = await CallLuis(appid, key, query);
                    break;
                }
                catch (Exception e)
                {
                    // Check for 429 "Too many requests" error.
                    exception = e;
                    if (e.IfIs<System.Net.WebException, bool>(
                            we => we.Response.IfIs<System.Net.HttpWebResponse, bool>(
                                wr => (int)wr.StatusCode == 429, false), false))
                    {
                        await Task.Delay(tooManyRequestsDelay);
                        tooManyRequestsDelay *= 3;
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            return result;
        }