Search Unity

Connection: keep-alive in Unity Web Request?

Discussion in 'Multiplayer' started by umityayla, Dec 17, 2019.

  1. umityayla

    umityayla

    Joined:
    May 21, 2019
    Posts:
    26
    Hello,

    We make dozens of API calls in a single game session so doing a new TCP handshake on each request is reducing our performance terribly. We wanted to see if there's any way that we could do to set Connection header to keep-alive on Unity. I'm aware of this though;

    "These headers cannot be set with custom values on any platform: accept-charset, access-control-request-headers, access-control-request-method, connection, date, dnt, expect, host, keep-alive, origin, referer, te, trailer, transfer-encoding, upgrade, via."

    But we need it terribly because each TCP(+SSL) handshake takes from 300 ms - 12,000 ms
    And a collegaue of mine told something about Unity Pro may have an advantage of this as access to lower level code.

    And I'd like to ask if there's anyone having issues with TCP Handshake failures (SSL) on Unitywebrequest with iOS/Android.

    Thanks, have a nice day.
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    There were recently Unity updates regarding this issue and keep-alive and connection reuse should be on by default.
    What version of Unity are you one, I could check if most recent release of it has it.
     
  3. umityayla

    umityayla

    Joined:
    May 21, 2019
    Posts:
    26
    Our Unity version is 2019.1.4f1
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    Yeah, this version is no longer supported. You have to upgrade to 2019.2 to get a fix.
     
  5. umityayla

    umityayla

    Joined:
    May 21, 2019
    Posts:
    26
    Upgraded to 2019.2 but still getting "InvalidOperationException: Cannot override system-specified headers"
     
  6. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    You don't need to override the Connection header, keep-alive is default behavior.
     
  7. KissTheBlade

    KissTheBlade

    Joined:
    Nov 25, 2017
    Posts:
    19
    Is there anything special I need to do to get keep alive connections to work? Does it matter if I make this request from a singleton (which doesn't get destroyed between scene changes).

    UnityWebRequest www = UnityWebRequest.Post(url, "");

    Or do I have to keep an instance to the web request somehow alive, in an instance variable for example?

    Now I don't see any keep alive requests at the server.
     
  8. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    On HTTP/1.1 keep-alive is default according latest specs, unless you seem "Connection: close", it should be used.
    If you see servers not adhering to that, let me know, we could force that header.
     
  9. menelaus

    menelaus

    Joined:
    Apr 24, 2014
    Posts:
    3
    Hi,

    When I try to send request with UnityWebRequest, on 2019.1 it doesn't reuse the connection. But for 2019.2 or later it obey the keep-alive. I'm debugging with Wireshark for my iPhone. This is a huge issue because of ssl handshake it'll cost too much data out on AWS on our backend.

    How can I enable keep alive for all unity versions (2018 and later) or is there specific unity version keep alive started to be supported ?

    Thanks
     
  10. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    Correct, the support for it is fairly recent.
     
  11. menelaus

    menelaus

    Joined:
    Apr 24, 2014
    Posts:
    3
    Thanks, is there any alternative way we can use for older Unity versions ? Or do we need to implement native plugins for each platform to utilize keep alive ?