Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions lib/nectar.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ defmodule Nectar do
require Logger

def start(_type, args) do
port = Keyword.get(args, :port, 8080)
requested_port = Keyword.get(args, :port, 0)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is good for tests, but it would be nice to default a port for when you mix nectar.server similar to how phoenix defaults to 4000

concurrency = Keyword.get(args, :concurrency, 20)

{:ok, socket} =
:gen_tcp.listen(port, [
:gen_tcp.listen(requested_port, [
:binary,
packet: :raw,
active: false,
Expand All @@ -21,8 +21,12 @@ defmodule Nectar do
nodelay: true
])

Logger.info(fn -> "Listening on Port #{port}" end)
{:ok, assigned_port} = :inet.port(socket)
Logger.info(fn -> "listening on port #{assigned_port}" end)

Nectar.Supervisor.start_link(socket, concurrency)
case Nectar.Supervisor.start_link(socket, concurrency) do
{:ok, pid} -> {:ok, pid, %{port: assigned_port}}
{:error, _reason} = other -> other
end
end
end
15 changes: 5 additions & 10 deletions test/nectar_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@ defmodule NectarTest do
use ExUnit.Case
doctest Nectar

# https://en.wikipedia.org/wiki/Ephemeral_port#Range
def start_server, do: start_server(49152)

def start_server(65536), do: {:error, :no_port}

def start_server(port) do
case Nectar.start(nil, port: port, concurrency: 1) do
{:ok, pid} when is_pid(pid) ->
def start_server do
case Nectar.start(nil, concurrency: 1) do
{:ok, pid, %{port: port}} when is_pid(pid) ->
{:ok, port}

_ ->
start_server(port + 1)
{:error, reason} ->
raise "could not start nectar server: #{inspect(reason)}"
end
end

Expand Down