nozzlegear / ShopifySharp

ShopifySharp is a .NET library that helps developers easily authenticate with and manage Shopify stores.
https://nozzlegear.com/shopify-development-handbook
MIT License
742 stars 308 forks source link

Using Leaky bucket and still throttled ! #987

Closed BasitBulbulia closed 8 months ago

BasitBulbulia commented 8 months ago

I am using leakybucket and am still being throttled ; is my code logically correct or am I doing something wrong?


    Public Async Sub Initialise()

        Dim err As Short = 0

        Try

trygain:

        Dim productService = New ProductService(urlFromUser, shopAccessToken)
        'set the RetryExecutionPolicy to handle the API limits
        productService.SetExecutionPolicy(New LeakyBucketExecutionPolicy())

            Dim isValidDomain = Await ShopifySharp.AuthorizationService.IsValidShopDomainAsync(urlFromUser)

            If Not isValidDomain Then

                ' MsgBox("not valid -> shopify url:" & urlFromUser)

                '   Call ProductDetail.NewShopifyID()

                err = err + 1
                Thread.Sleep(3000)

                If err > 3 Then
                    ProductDetail.LstFiles.Items.Add("Unfortuntely your shopify usage has been throttled ! ! ")
                    Exit Sub
                End If
                GoTo trygain

            End If

        Catch ex As Exception
            GoTo trygain
        End Try
    End Sub
nozzlegear commented 8 months ago

Hey @BasitBulbulia, I think there might be a logic problem in this code. It looks like you're trying to check if the Shopify domain is valid before making a call to the Shopify API? But I don't see where you're making a call to the API except only to check if the domain is valid. The domain check itself is not a real Shopify API call and shouldn't trip any leaky bucket policies.

Try changing your code to this:

Dim isValidDomain = Await ShopifySharp.AuthorizationService.IsValidShopDomainAsync(urlFromUser)

If isValidDomain Then
    Dim products = Await productService.ListAsync();
Else
    ' MsgBox("not valid -> shopify url:" & urlFromUser)

    '   Call ProductDetail.NewShopifyID()

    err = err + 1
    Thread.Sleep(3000)

    If err > 3 Then
        ProductDetail.LstFiles.Items.Add("Unfortuntely your shopify usage has been throttled ! ! ")
        Exit Sub
    End If
    GoTo trygain

End If

I think you should also move the lines where you create the product service and set the execution policy outside of the tryagain label. I'm not positive, but I think resetting the policy on each try may cause the policy to forget its "state" and then hit the limit.