Jump to content
Karbot

Requests to local api very slow with TRESTClient

Recommended Posts

Hello folks,

 

We're having an issue with local requests. They take much longer than external API calls.

It's just a simple GET to the API and it's not reproducible with any other RestClient written with C# or Python.

PASCAL/Delphi is not my native language, so I hope to find help here.

Please see the timing behavior and code used below.

 

Kind Regards

karbot

https://<external_hostname> (26ms)
https://<external_ip> (168ms)
https://mypc (6049ms)
https://192.168.178.102 (24ms)
https://localhost (2029ms)
https://127.0.0.1 (26ms)
function TAPIConnector.SendApiRequest(const aURL, aOperation: string; aSetApiParams: TProc<TRESTRequest>; aRaiseException: Boolean): TRESTResponse;
begin
  var restClient := TRESTClient.Create(aURL);
  try
    restClient.RaiseExceptionOn500 := aRaiseException;
    var restRequest := TRESTRequest.Create(restClient);
    restRequest.Client := restClient;
    restRequest.Method := TRESTRequestMethod.rmPOST;
    restRequest.Resource := aOperation;
    if (Assigned(aSetApiParams)) then begin
      aSetApiParams(restRequest);
    end;

    Result := TRESTResponse.Create(nil);
    try
      restRequest.Response := Result;

      restRequest.Execute;

      if aRaiseException and not Result.Status.Success then begin
        const cInvalidSession = 'No valid session GUID';
        if Result.Status.ClientErrorUnauthorized_401 and Result.StatusText.Contains(cInvalidSession) then begin
          raise Exception.Create(aOperation);
        end;

        var msg := Result.ErrorMessage;
        if msg.IsEmpty then begin
          msg := Result.StatusText;
        end;
        raise Exception.Create(aOperation + msg);
      end;
    except
      Result.Free();
      raise;
    end;
  finally
    restClient.Free;
  end;
end;

 

Share this post


Link to post
Guest

There must be loads of net/http OS libs.

Try another one in the same project (the beauty of Delphi).

My rec would be to try out rtc.teepi.net (trial - it is ridiculously stable) but that is only one. ICS also. And so on...

Share this post


Link to post

I remember also having this! localhost first ran into a 2000 ms timeout, but 127.0.0.1 was normal.

 

After we made our local server listen for ipv6 connections as well, that timeout was gone.

Edited by Der schöne Günther

Share this post


Link to post
Quote

After we made our local server listen for ipv6 connections as well, that timeout was gone.

Yes, I've seen that.  If the client is allowed to use IPv6 and IPv4, it will try IPv6 first, timeout and try IPv4.  All client software should have a setting to make it use IPv4 only, in ICS it's SocketFamily.  

 

Angus

 

Share this post


Link to post

To clarify, this is down to DNS.  If a DNS lookup returns both IPv4 and IPv6 addresses, the client will usually try IPv6 first.  The Windows HOSTS file has both IPv4 and IPv6 lookups for localhost so servers need to listen on both addresses.  Can be fixed by removing the IPv6 address from HOSTS. 

 

Angus

 

Share this post


Link to post

Many thanks to your answers. I will investigate them. The IPv6 approach seems pretty logical to me. I will have a look at that first and let you know.

Maybe there is a switch in the rest libary to set the SocketFamily.

Regards 

Karbot

 

Edited by Karbot

Share this post


Link to post
2 hours ago, yonojoy said:

How fast is ping -4 mypc ?

It's <1ms but it is using ipv6. Another pointer in that direction. Thanks

Share this post


Link to post
7 hours ago, yonojoy said:

How fast is ping -4 mypc ?

5 hours ago, Karbot said:

It's <1ms but it is using ipv6. Another pointer in that direction. Thanks

 

No. If you apply the -4 switch usage of ipv4 should be forced (in Windows).

Edited by yonojoy

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×