Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document the adapter.onReconnectionError callback #48

Open
vincentfretin opened this issue Apr 13, 2023 · 0 comments
Open

Document the adapter.onReconnectionError callback #48

vincentfretin opened this issue Apr 13, 2023 · 0 comments
Labels
documentation Improvements or additions to documentation

Comments

@vincentfretin
Copy link
Member

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

if (this.reconnectionAttempts > this.maxReconnectionAttempts && this.onReconnectionError) {
return this.onReconnectionError(
new Error("Connection could not be reestablished, exceeded maximum number of reconnection attempts.")
);
}

If you don't define onReconnectionError it will try reconnecting forever.

@vincentfretin vincentfretin added the documentation Improvements or additions to documentation label Apr 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant