ericnewton76 / gmaps-api-net

C# google maps api interface for interacting with the backend web services for Google Maps

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

OVER_DAILY_LIMIT StatusCode not handled, results in ServiceResponseStatus.Unknown

gbanister opened this issue · comments

Thats an oversight. Thanks for reporting.

Were you running in a tight loop? We've always had plans to try to build an injectable or option for automatically backing off and retry but had some difference of opinions on how to implement.

That is funny you ask that. I was just looking into building retry logic into my app that uses your library and was looking into the response codes trying to figure out when I should retry and when I should not. I just now started thinking about it, but at first glance it seems UnKnown and OverQueryLimit would be the status to retry on, and optionally retry after a delay, especially for OverQueryLimit.

A useful interface to a retry API might look something like this:
var response = await distanceMatrixService
.ImediateRetryOnStatus(ServiceResponseStatus.Unknown, retrycount: 2)
.DelayedRetryOnStatus(ServiceResponseStatus.OverQueryLimit, msecdelay: 100, retrycount: 2)
.GetResponseAsync(request);

In the mean time, I'll settle on an extension method:

public static class GoogleMapsExtensions
{
    public static async Task<DistanceMatrixResponse> GetResponseWithRetryAsync(this DistanceMatrixService service, DistanceMatrixRequest request, int retryCount, int milSecDelay = 0)
    {
        var response = await service.GetResponseAsync(request);
    
        for (var i = 0; i < retryCount; i++)
        {
            if (response.Status == ServiceResponseStatus.OverQueryLimit || 
                response.Status == ServiceResponseStatus.Unknown)
            {
                Thread.Sleep(milSecDelay);
                response = await service.GetResponseAsync(request);
            }
        }
       return response;
    }

There's more coming on that note. See the https://github.com/ericnewton76/gmaps-api-net/tree/fix/tests-using-snapshots branch (which I need to update with all the changes from base master branch)