Skip to content

Commit 4ca0455

Browse files
authored
Change port selection to random (#90)
* Change port selection to random * Change get_valid_pair to use enums
1 parent b9ad154 commit 4ca0455

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

lib/ex_ice/priv/checklist.ex

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,17 @@ defmodule ExICE.Priv.Checklist do
1919
checklist
2020
# pair might have been marked as failed if the associated
2121
# local candidate has been closed
22-
|> Stream.filter(fn {_id, pair} -> pair.state == :succeeded end)
23-
|> Stream.filter(fn {_id, pair} -> pair.valid? end)
22+
|> Enum.filter(fn {_id, pair} -> pair.state == :succeeded and pair.valid? end)
2423
|> Enum.max_by(fn {_id, pair} -> pair.priority end, fn -> {nil, nil} end)
2524
|> elem(1)
2625
end
2726

2827
@spec get_valid_pair(t()) :: CandidatePair.t() | nil
2928
def get_valid_pair(checklist) do
3029
checklist
31-
|> Stream.map(fn {_id, pair} -> pair end)
32-
|> Stream.filter(fn pair -> pair.valid? end)
33-
|> Enum.sort_by(fn pair -> pair.priority end, :desc)
34-
|> Enum.at(0)
30+
|> Enum.filter(fn {_id, pair} -> pair.valid? end)
31+
|> Enum.max_by(fn {_id, pair} -> pair.priority end, fn -> {nil, nil} end)
32+
|> elem(1)
3533
end
3634

3735
@spec find_pair(t(), CandidatePair.t()) :: CandidatePair.t() | nil

lib/ex_ice/priv/gatherer.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ defmodule ExICE.Priv.Gatherer do
6363
inet
6464
]
6565

66-
Enum.reduce_while(gatherer.ports, nil, fn port, _ ->
66+
gatherer.ports
67+
|> Enum.shuffle()
68+
|> Enum.reduce_while(nil, fn port, _ ->
6769
case gatherer.transport_module.open(port, socket_opts) do
6870
{:ok, socket} ->
6971
{:ok, {^ip, sock_port}} = gatherer.transport_module.sockname(socket)

0 commit comments

Comments
 (0)