refit icon indicating copy to clipboard operation
refit copied to clipboard

[BUG] Dynamic API endpoint creation with dynamic url is slow

Open LuukVerhagen opened this issue 3 years ago • 3 comments

Describe the bug I use Refit with different host URLs, which come dynamically from the database. To accomplish this, I have created a factory service, which creates the Refit Endpoints dynamically based on the URL of the database. The problem which I encountered, is that the first API call with this newly created endpoint object is really slow (more than 40 seconds). If I debug the same code with a literal string URL instead of an URL from the database, I don't encounter this slowness.

//Slow
Project project = await projectService.GetSelectedProject();
var endpointWithDynamicUrl = Refit.RestService.For<TApiEndpoint>(project.Url, new Refit.RefitSettings()
{
    AuthorizationHeaderValueGetter = this.apiAuthService.GetToken
});

//Fast
var endpointWithLiteralUrl = Refit.RestService.For<TApiEndpoint>("https://example.com/", new Refit.RefitSettings()
{
    AuthorizationHeaderValueGetter = this.apiAuthService.GetToken
});

Steps To Reproduce

Expected behavior

That dynamically created API endpoint objects with a dynamic URL source doesn't cause a slow endpoint call.

Screenshots

Environment

  • OS: Windows
  • Device: Lenovo thinkpad p1
  • Version:
  • Working Version:

Additional context

If the URL in the database is a local URL, the first call is much faster.

LuukVerhagen avatar Feb 07 '22 10:02 LuukVerhagen

Any suggestions for improvement? It doesn't look like a bug!

codeex avatar Feb 28 '22 08:02 codeex

I finally found the problem. The underlying HttpClient wasn't able to resolve my IPv6 address (which was wrongly configured in my DNS). It takes more than half a minute for the HttpClient to figure out IPv6 won't work and to switch to IPv4. To conclude the problem wasn't in Refit.

LuukVerhagen avatar Apr 20 '22 13:04 LuukVerhagen

I just hit this same issue, also thinking that Refit might be the culprit. For me it did not take half a minute, just a few seconds (but very noticeable at application startup).

What I ended up doing is disabling IPv6 by adding the following runtime configuration option in my application csproj:

<ItemGroup>
  <RuntimeHostConfigurationOption Include="System.Net.DisableIPv6" Value="true" />
</ItemGroup>

After disabling IPv6, startup time went from ~2.5 seconds to ~0.2 seconds.

See also Forcing HttpClient to use IPv4 or IPv6 addresses by @meziantou and .NET 6 Networking Improvements (An option to globally disable IPv6).

Addendum: Turns out the server was listening on http://127.0.0.1:5000 and the client was connecting to http://localhost:5000 so by changing the server configuration to listen on localhost the slowness issue disappeared without having to disable IPv6.

Same conclusion as Luuk: no problem in Refit!

0xced avatar Jun 24 '22 10:06 0xced

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Sep 14 '23 00:09 github-actions[bot]