Repository: deno
Issue: Deno should make simultaneous outgoing requests to IPv4 and IPv6 addresses when both exist
Labels: feat,ext/websocket,ext/net

Version: 
> deno 1.42.4 (release, aarch64-apple-darwin)
> v8 12.3.219.9
> typescript 5.4.3

Update: The Problem
----------------------

Copy/paste from my comment below:

My host, nfnitloop.com had an old/invalid IPv6 address in DNS.

Chrome's implementation simultaneously tries to open an IPv6 and and IPv4 connection, and continues with whichever responds first.

Deno's implementation seems to always try IPv6 first. Only after that times out (75 seconds?) does it fall back to IPv4.

So, yes, as far as my server was concerned, Deno was doing nothing for 70+ seconds before connecting.

Oddly, Deno's fetch() implementation does the same thing that Chrome does, and tries both IPv6 and 4 simultaneously. It might be good to do the same thing for WebSocket!



Original Report
---------------

(Details here about how this surfaced, and how I tried to debug it. But I didn't discover the above problem until later.)

I can [run this code in my browser][1] and the web socket connects in a reasonable amount of time (given that I'm on a high-latency link, and there's TCP/TLS/HTTP round-trip overhead):


[1]: https://www.typescriptlang.org/play/?#code/IYZwngdgxgBAZgV2gFwJYHsIypiBTKZACgEoYBvAKBm0xGRjQFs8AnGAXhnwHcYAVVC1alqtCPRg8Qnbnj4B1PACMAyuigBrPMQBE0kAC4A9MYhwIqZABt06AA4A6HEzPp6rY7pJicE9NZ4jrYA5kS6CsBWqBAh8OjsPCowIBraDMjo4viE3r50AUGh4ax4wAAmYKrIwMh4ugA0UiCOpRVVNXU+YtKOFeUAogBueBDIADKo9KNs4Q6jjTBEeCNjZBwAfBRiNMxsjuWYeKI0NH6pgcHoYbptldW19U29dx2PPqfNzrYgxx8wAF8fADKJQoNZQDJBMJtjR6MBWHVyrJofsIOgeCcUp1ULB0ZiyOQYKVkAhWFgACKPRz40iOIbAawIPAAeTgpEBYhCOhgeAh9l+5QAsiAOUSSWSsKjWDSMRyALSMAAWU0c8MReGRIJoh3wYp24guRWu4WlMF1eBgwDgdVYi2QKpafOAAs1IqauiYIG8AG4xCCQWDcARiCQgA

```javascript
async function connect() {
  const timer = new Timer()
  const ws = new WebSocket("wss://nfnitloop.com/nostr/")
  console.log("Waiting for web socket to connect")
  console.log("readyState", ws.readyState)

  ws.addEventListener("open", (event) => {
    timer.done()
    console.log("readyState", ws.readyState)
    ws.close()
  })
}

class Timer {
  started = Timer.now()
  static now() { return Date.now().valueOf() }
  get elapsedMs() { return Timer.now() - this.started }
  done() {
    console.log("Timer done after", this.elapsedMs, "ms");
  }
}

connect()
```

output:
```
Waiting for web socket to connect
runtime.ts:177 readyState 0
runtime.ts:177 Timer done after 717 ms
runtime.ts:177 readyState 1
```

But when I run this in Deno, it takes for-ev-er:

```
> deno run -A example.js
Waiting for web socket to connect
readyState 0
Timer done after 75760 ms
readyState 1
```

⚠️ Now, this doesn't happen on every web socket. For example, if I replace the URL with `wss://nos.lol` I get reasonable performance there too.

```
> deno run -A example.js
Waiting for web socket to connect
readyState 0
Timer done after 969 ms
readyState 1
```

So it seems Deno's WebSocket implementation doesn't work well with something on *my* web socket, in particular. I haven't noticed this in browsers or in iOS apps that connect to that socket. It's likely that I'm just doing something that's an edge case that triggers this poor behavior in Deno's implementation.

I'm just using Axum's WebSocket implementation to handle incoming web sockets. That works instantly when I run locally, without TLS. (127.0.0.1).  When I deploy it to my remote server, behind an Nginx proxy, which handles my TLS encryption, then I see the issue. (Again, *only* in Deno.)

I'm not sure if it's relevant but just in case, my proxy settings in nginx are:

```
map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

server {
    server_name www.nfnitloop.com nfnitloop.com;
    # <normal HTTPS server config>

    location /nostr/ {
        proxy_pass http://127.0.0.1:8095/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        # See: https://nginx.org/en/docs/http/websocket.html
        proxy_set_header Connection $connection_upgrade;
        proxy_buffering off; # have tried this on/off. 
    }
```

When I enable logging on the proxied service, it seems like it doesn't get the request until many seconds later, at which point things operate "normally" AFAICT. I haven't yet done a packet sniff to see if this delay is in Deno sending the first packet(s) or Nginx holding onto them for a long time before forwarding.