-
Notifications
You must be signed in to change notification settings - Fork 7
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
Add TURN support #30
Conversation
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
lib/signal_tower/room.ex
Outdated
|
||
{turn_response, next_turn_timestamp} = | ||
if System.get_env("SIGNALTOWER_TURN_SECRET") && last_turn_timestamp < now do | ||
next_timestamp = now + 3 * 60 * 60 |
There was a problem hiding this comment.
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 ^^
There was a problem hiding this comment.
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
lib/signal_tower/room.ex
Outdated
|
||
{turn_response, next_turn_timestamp} = | ||
if System.get_env("SIGNALTOWER_TURN_SECRET") && last_turn_timestamp < now do | ||
next_timestamp = now + 3 * 60 * 60 |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
lib/signal_tower/session.ex
Outdated
@@ -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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
test/session_test.exs
Outdated
@@ -15,7 +15,7 @@ defmodule SessionTest do | |||
"room_id" => "s-room1", | |||
"status" => %{user: "0"} | |||
}, | |||
nil | |||
{nil, 0} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
test/room_test.exs
Outdated
{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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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}) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
Ok, I have:
I have not (yet)
|
@@ -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 |
There was a problem hiding this comment.
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
lib/signal_tower/room.ex
Outdated
|
||
{turn_response, next_turn_timeout} = | ||
if System.get_env("SIGNALTOWER_TURN_SECRET") && turn_timeout < now do | ||
next_timeout = now + 3 * 60 * 60 |
There was a problem hiding this comment.
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))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
lib/signal_tower/room.ex
Outdated
defp send_joined_room(pid, own_id, members, turn_timeout) do | ||
now = System.os_time(:second) | ||
|
||
{turn_response, next_turn_timeout} = |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 :-)
There was a problem hiding this comment.
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}) |
There was a problem hiding this comment.
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
There was a problem hiding this 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
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.