Skip to content

Commit

Permalink
Poll for the internet more frequently on connection change
Browse files Browse the repository at this point in the history
This significantly improves the time to detect Internet availability by
polling more frequently at the start. It then backs off exponentially to
the maximum interval time.
  • Loading branch information
fhunleth committed Jun 4, 2019
1 parent 0ae172e commit 4cf8371
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions lib/vintage_net/interface/connectivity_checker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ defmodule VintageNet.Interface.ConnectivityChecker do

require Record

@delay_to_first_check 100
@interval 30_000
@min_interval 500
@max_interval 30_000

@ping_port 80
@ping_timeout 5_000

Expand All @@ -23,7 +24,7 @@ defmodule VintageNet.Interface.ConnectivityChecker do

@impl true
def init(ifname) do
state = %{ifname: ifname, interval: @interval}
state = %{ifname: ifname, interval: @min_interval}
{:ok, state, {:continue, :continue}}
end

Expand All @@ -35,7 +36,7 @@ defmodule VintageNet.Interface.ConnectivityChecker do

case VintageNet.get(lower_up_property(ifname)) do
true ->
{:noreply, state, @delay_to_first_check}
{:noreply, state, @min_interval}

_not_true ->
# If the physical layer isn't up, don't start polling until
Expand Down Expand Up @@ -63,7 +64,7 @@ defmodule VintageNet.Interface.ConnectivityChecker do

set_connectivity(ifname, connectivity)

{:noreply, state, interval}
{:noreply, state, next_interval(connectivity, interval)}
end

def handle_info(
Expand All @@ -83,7 +84,9 @@ defmodule VintageNet.Interface.ConnectivityChecker do
# Physical layer is up. Optimistically assume that the LAN is accessible and
# start polling again after a short delay
set_connectivity(ifname, :lan)
{:noreply, state, @delay_to_first_check}

new_state = %{state | interval: @min_interval}
{:noreply, new_state, @min_interval}
end

defp ping(ifname) do
Expand Down Expand Up @@ -138,4 +141,11 @@ defmodule VintageNet.Interface.ConnectivityChecker do
defp lower_up_property(ifname) do
["interface", ifname, "lower_up"]
end

# Back off of checks if they're not working
defp next_interval(:internet, _interval), do: @max_interval

defp next_interval(_not_internet, interval) do
min(interval * 2, @max_interval)
end
end

0 comments on commit 4cf8371

Please sign in to comment.