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

Add TURN support #30

Merged
merged 4 commits into from
Jul 23, 2020
Merged

Add TURN support #30

merged 4 commits into from
Jul 23, 2020

Conversation

farao
Copy link
Member

@farao farao commented Jun 20, 2020

If SIGNALTOWER_TURN_SECRET is set, the signaltower add to each
joined_room message a turn_user and turn_password field that allows the
client to use the TURN server for 3 hours. In the following 3h, the
client will always get the same login/token with the same end time back
when it joins another room.

@farao
Copy link
Member Author

farao commented Jun 20, 2020

Needs testing together with palavatv/palava-web#59 and palavatv/palava-client#30.

If SIGNALTOWER_TURN_SECRET is set, the signaltower add to each
joined_room message a turn_user and turn_password field that allows the
client to use the TURN server for 3 hours. In the following 3h, the
client will always get the same login/token with the same end time back
when it joins another room.
@@ -47,6 +60,8 @@ By default, the websocket is bound to all interfaces (0.0.0.0), you can also bin
export SIGNALTOWER_LOCALHOST
```

## References

[palava protocol]: https://github.com/palavatv/palava-client/wiki/Protocol
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change in the protocol should be documented.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about making an official release after this and writing it in the CHANGELOG?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue to track: palavatv/palava-client#32


{turn_response, next_turn_timestamp} =
if System.get_env("SIGNALTOWER_TURN_SECRET") && last_turn_timestamp < now do
next_timestamp = now + 3 * 60 * 60
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the next_timestamp something like a timeout when the turn session times out. If so I would call it like timeout. If not I need more explanation ^^

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it's a timeout, I will rename it


{turn_response, next_turn_timestamp} =
if System.get_env("SIGNALTOWER_TURN_SECRET") && last_turn_timestamp < now do
next_timestamp = now + 3 * 60 * 60
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use a function from the time module to add three hours to the current time? e.g.: add(now, 3, :hour)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used that before, but since it's only possible to get the system time as unix time, so I would need to always turn that into elixir time and also carry the turn_timestamp around as elixir time only to change it again to unix time for export - so since both ends are unix time, I'm not sure if it makes sense to make it into elixir time in between only to use the add function here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could just imagine that this could be a source of failure since the timestamp has no type based unit. Also the variable name does not provide any information about its unit.

@@ -11,48 +11,48 @@ defmodule SignalTower.Session do
|> (&Process.register(self(), &1)).()
end

def handle_message(msg, room) do
def handle_message(msg, {room, ltt}) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not use ltt but the full version instead. You might know what it means now but some programmer which is new to the code base might have problems to understand what it stands for.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true, I will change that

@@ -15,7 +15,7 @@ defmodule SessionTest do
"room_id" => "s-room1",
"status" => %{user: "0"}
},
nil
{nil, 0}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to add a test case were the second parameter is not equal to 0?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it does, I will add that as well

{timestamp, ""} = Integer.parse(timestamp_str)
assert own_id == id
assert System.os_time(:second) < timestamp
assert timestamp < System.os_time(:second) + 3 * 60 * 60 + 10
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope this line will not get flaky in CI

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope that too :D

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For my understanding. The clock is faked in this test right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean System.os_time(:second)? No, that's not faked

@@ -23,7 +23,7 @@ defmodule RoomTest do

_user2 =
spawn_user_no_join(fn ->
GenServer.call(room_pid, {:join, self(), %{user: "1"}})
GenServer.call(room_pid, {:join, self(), %{user: "1"}, 0})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmh after reading through the code I don't understand this new parameter. In the test it is always 0. Does it vary with and without turn?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well we have a per user turn timeout which is part of the state of the user session, so usually it's 0 when the session starts but then it will have the value of the last timout generated

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a way to make that clearer?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe GenServer's handle_call can have 0 as default value

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is really only for the test unclear and not for the actual code, I would leave it. The parameter is clearly named now so that if you look into the handler, it should be clear what it means.

Make the session state a map and document in the websocket init the
members.
@farao
Copy link
Member Author

farao commented Jun 25, 2020

Ok, I have:

  • improved the naming
  • made the parts of the session state more explicit by putting them into a map and documenting them

I have not (yet)

  • changed something about the time keeping (unix timestamps vs elixir time structure) - I tend to leave it like it is?
  • wrote a session test for turn where the parameter is not 0 (it's only one test, would you probably like to add it to see if the parameters are more clear now?)

@@ -47,6 +60,8 @@ By default, the websocket is bound to all interfaces (0.0.0.0), you can also bin
export SIGNALTOWER_LOCALHOST
```

## References

[palava protocol]: https://github.com/palavatv/palava-client/wiki/Protocol
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue to track: palavatv/palava-client#32


{turn_response, next_turn_timeout} =
if System.get_env("SIGNALTOWER_TURN_SECRET") && turn_timeout < now do
next_timeout = now + 3 * 60 * 60
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this value (3h) be extracted to its own variable/constant ("no magic numbers": https://en.wikipedia.org/wiki/Magic_number_(programming))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

defp send_joined_room(pid, own_id, members, turn_timeout) do
now = System.os_time(:second)

{turn_response, next_turn_timeout} =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*turn_timeout seems to be a kind of expire_date - do you agree this would make the code more readable?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right, I renamed it again, now to turn_token_expiry :-)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Naming is everything 😄

@@ -23,7 +23,7 @@ defmodule RoomTest do

_user2 =
spawn_user_no_join(fn ->
GenServer.call(room_pid, {:join, self(), %{user: "1"}})
GenServer.call(room_pid, {:join, self(), %{user: "1"}, 0})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe GenServer's handle_call can have 0 as default value

Copy link
Member

@erikzenker erikzenker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx for adding the turn support

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants