networked-aframe / naf-janus-adapter

networked-aframe Janus network adapter
Mozilla Public License 2.0
12 stars 9 forks source link

Document the adapter.onReconnectionError callback #48

Open vincentfretin opened 1 year ago

vincentfretin commented 1 year ago

Copy and paste a conversation I had on slack to not lose it. This needs to be included in the documentation and added in an example.

If a user has a bad connection, it will try to reconnect infinitely if you didn't set a adapter.onReconnectionError callback, and will use your server CPU for nothing...

I have some logs like this:

docker logs docker-janus-1|grep "Error sending signalling message"
[Mon Nov 14 15:57:35 2022] [ERR] Error sending signalling message to 0x7ff960023b60: Unknown error (code: -2)
[Wed Nov 16 13:42:21 2022] [ERR] Error sending signalling message to 0x7ff960001b10: Unknown error (code: -2)
[Wed Nov 16 13:42:21 2022] [ERR] Error sending signalling message to 0x7ff9281a65a0: Unknown error (code: -2)
[Wed Nov 16 13:42:21 2022] [ERR] Error sending signalling message to 0x7ff960001b10: Unknown error (code: -2)
[Wed Nov 16 13:42:21 2022] [ERR] Error sending signalling message to 0x7ff9281a65a0: Unknown error (code: -2)
[Sat Jan 28 08:18:07 2023] [ERR] Error sending signalling message to 0x7ff9b42a98c0: Unknown error (code: -2)
[Sun Jan 29 10:22:56 2023] [ERR] Error sending signalling message to 0x7f5c58008660: Unknown error (code: -2)
[Sun Jan 29 11:03:18 2023] [ERR] Error sending signalling message to 0x7f5c58007c80: Unknown error (code: -2)
[Sun Jan 29 22:45:36 2023] [ERR] Error sending signalling message to 0x7f5c58003390: Unknown error (code: -2)
[Mon Jan 30 00:06:02 2023] [ERR] Error sending signalling message to 0x7f5c1c0277e0: Unknown error (code: -2)
[Mon Jan 30 00:15:40 2023] [ERR] Error sending signalling message to 0x7f5c58008ea0: Unknown error (code: -2)
[Mon Jan 30 04:23:22 2023] [ERR] Error sending signalling message to 0x7f5c58009f80: Unknown error (code: -2)

generally with destroy session afterwards:

[Sun Jan 29 10:22:56 2023] [ERR] Error sending signalling message to 0x7f5c58008660: Unknown error (code: -2)
[Sun Jan 29 10:22:56 2023] [6124030342223887] Handle and related resources freed; 0x7f5c5800abf0 0x7f5c58007a10
[Sun Jan 29 10:22:56 2023] [8396142484582223] Handle and related resources freed; 0x7f5c58009b20 0x7f5c1c07a3c0
[Sun Jan 29 10:22:56 2023] [7674298688103066] Handle and related resources freed; 0x7f5c5800c060 0x7f5c5800c000
[Sun Jan 29 10:22:56 2023] [4407323319927970] Handle and related resources freed; 0x7f5c58008c30 0x7f5c1c07a3c0
[Sun Jan 29 10:22:56 2023] [4168275316899545] Handle and related resources freed; 0x7f5c58006060 0x7f5c58007a10
[Sun Jan 29 10:23:53 2023] ESC[31m[ERR]ESC[0m [janus.c:janus_process_incoming_request:1147] Couldn't find any session 4625370836905971...
[Sun Jan 29 10:23:53 2023] ESC[31m[ERR]ESC[0m [janus.c:janus_process_incoming_request:1147] Couldn't find any session 5223480265397123...
[Sun Jan 29 10:23:54 2023] ESC[31m[ERR]ESC[0m [janus.c:janus_process_incoming_request:1147] Couldn't find any session 4807772804356266...
[Sun Jan 29 10:24:24 2023] ESC[31m[ERR]ESC[0m [janus.c:janus_process_incoming_request:1147] Couldn't find any session 5223480265397123...
[Sun Jan 29 10:24:24 2023] ESC[31m[ERR]ESC[0m [janus.c:janus_process_incoming_request:1147] Couldn't find any session 4625370836905971...
[Sun Jan 29 10:24:25 2023] ESC[31m[ERR]ESC[0m [janus.c:janus_process_incoming_request:1147] Couldn't find any session 4807772804356266...

Those logs show probably a user having bad connection, and then naf-janus-adapter will reconnect and recreate all the sessions so increase of CPU. You need to define a adapter.onReconnectionError callback to be sure it doesn't try to reconnect indefinitely. It's called after 10 failures if I remember.

Something like that:

    adapter.onReconnectionError = () => exitScene("connect_error");
    // adapter.onReconnecting = (delay) => notifications.create({ type: "log", code: ??, level: "warning" }) // Connection issue, reconnecting...
    let msgShown = false;
    adapter.onReconnecting = (delay) => {
      let wsState = "NONE";
      if (NAF.connection.adapter && NAF.connection.adapter.ws) {
        wsState =
          { 0: "CONNECTING", 1: "OPEN", 2: "CLOSING", 3: "CLOSED" }[NAF.connection.adapter.ws.readyState] || "UNKNOWN";
      }
      let webrtcState = "NONE";
      if (NAF.connection.adapter && NAF.connection.adapter.publisher) {
        webrtcState = NAF.connection.adapter.publisher.conn.connectionState;
      }
      const msg = `connection issue, reconnecting... [WS ${wsState}, WebRTC ${webrtcState}]. Try to stop your VPN?`;
      console.log(msg);
      if (!msgShown) {
        notifications.create({ type: "log", code: 12, level: "info" }); // Connection issue, reconnecting... Try to stop your VPN?
        msgShown = true;
        setTimeout(() => {
          msgShown = false;
        }, 10000);
      }
    };
    adapter.onReconnected = () => {
      msgShown = false;
      console.log("reconnected");
      notifications.create({ type: "log", code: 11, level: "info" }); // There was a network issue, you have been reconnected.
    };
  });

my exitScene function (I use solidjs signals here)

const exitScene = (reason = "left", details: string | undefined = undefined) => {
  const existingTrack = audioTrack();
  if (existingTrack) {
    existingTrack.onmute = null;
    existingTrack.onunmute = null;
    existingTrack.stop();
    setAudioTrack(null);
  }

  if (NAF.connection.adapter) {
    if (NAF.connection.adapter.localMediaStream) {
      NAF.connection.adapter.localMediaStream.getTracks().forEach((t) => t.stop());
    }
    // removing networked-scene will call NAF.connection.adapter.disconnect()
    const sceneEl = document.querySelector("a-scene");
    sceneEl.removeAttribute("networked-scene");
  }
  setExitReason(reason);
  setExitDetails(details);
  setStep(Step.LEFT_ROOM);
};

Relevant code is here https://github.com/networked-aframe/naf-janus-adapter/blob/fed7925cd1f4f771980f63a845edcfd606ae3544/src/index.js#L296-L300 If you don't define onReconnectionError it will try reconnecting forever.