Skip to content
Open
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## v0.5.0
- Request parameter encoding with new `:params` option

## v0.4.4
- Fixed ArithmeticError when calculating how many streams to request
on infinite stream sets
Expand Down
27 changes: 27 additions & 0 deletions lib/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ defmodule Kadabra.Application do
use Application
import Supervisor.Spec

alias Kadabra.ConnectionPool

def start(_type, _args) do
children = [
supervisor(Registry, [:unique, Registry.Kadabra]),
Expand All @@ -12,4 +14,29 @@ defmodule Kadabra.Application do

Supervisor.start_link(children, strategy: :one_for_one, name: :kadabra)
end

def route_request(pid, request) when is_pid(pid) do
ConnectionPool.request(pid, request)
end

def route_request(uri, request) do
pid =
case open(uri, name: via_tuple(uri)) do
{:ok, pid} -> pid
{:error, {{:already_started, pid}, _}} -> pid
end

ConnectionPool.request(pid, request)
end

defp via_tuple(%{scheme: scheme, host: host, port: port}) do
{:via, Registry, {Registry.Kadabra, {scheme, host, port}}}
end

def open(uri, opts) do
spec_opts = [id: :erlang.make_ref(), restart: :transient]
spec = worker(Kadabra.ConnectionPool, [uri, self(), opts], spec_opts)

Supervisor.start_child(:kadabra, spec)
end
end
3 changes: 2 additions & 1 deletion lib/connection/egress.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
defmodule Kadabra.Connection.Egress do
@moduledoc false

alias Kadabra.{Encodable, Error, Frame, Socket}
alias Kadabra.{Encodable, Frame, Socket}
alias Kadabra.Connection.Error

alias Kadabra.Frame.{
Goaway,
Expand Down
20 changes: 10 additions & 10 deletions lib/error.ex → lib/connection/error.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Kadabra.Error do
defmodule Kadabra.Connection.Error do
@moduledoc false

@type error ::
Expand Down Expand Up @@ -26,7 +26,7 @@ defmodule Kadabra.Error do

## Examples

iex> Kadabra.Error.no_error
iex> Kadabra.Connection.Error.no_error
<<0, 0, 0, 0>>
"""
@spec no_error :: <<_::32>>
Expand All @@ -40,7 +40,7 @@ defmodule Kadabra.Error do

## Examples

iex> Kadabra.Error.protocol_error
iex> Kadabra.Connection.Error.protocol_error
<<0, 0, 0, 1>>
"""
@spec protocol_error :: <<_::32>>
Expand All @@ -53,7 +53,7 @@ defmodule Kadabra.Error do

## Examples

iex> Kadabra.Error.flow_control_error
iex> Kadabra.Connection.Error.flow_control_error
<<0, 0, 0, 3>>
"""
@spec flow_control_error :: <<_::32>>
Expand All @@ -64,7 +64,7 @@ defmodule Kadabra.Error do

## Examples

iex> Kadabra.Error.frame_size_error
iex> Kadabra.Connection.Error.frame_size_error
<<0, 0, 0, 6>>
"""
@spec frame_size_error :: <<_::32>>
Expand All @@ -75,7 +75,7 @@ defmodule Kadabra.Error do

## Examples

iex> Kadabra.Error.compression_error
iex> Kadabra.Connection.Error.compression_error
<<0, 0, 0, 9>>
"""
@spec compression_error :: <<_::32>>
Expand All @@ -86,9 +86,9 @@ defmodule Kadabra.Error do

## Examples

iex> Kadabra.Error.parse(0x1)
iex> Kadabra.Connection.Error.parse(0x1)
:PROTOCOL_ERROR
iex> Kadabra.Error.parse(0xfff)
iex> Kadabra.Connection.Error.parse(0xfff)
0xfff
"""
@spec parse(integer) :: error | integer
Expand All @@ -113,9 +113,9 @@ defmodule Kadabra.Error do

## Examples

iex> Kadabra.Error.code(:PROTOCOL_ERROR)
iex> Kadabra.Connection.Error.code(:PROTOCOL_ERROR)
0x1
iex> Kadabra.Error.code(:NOT_AN_ERROR)
iex> Kadabra.Connection.Error.code(:NOT_AN_ERROR)
:NOT_AN_ERROR
"""
@spec code(error) :: integer
Expand Down
2 changes: 1 addition & 1 deletion lib/connection/flow_control.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ defmodule Kadabra.Connection.FlowControl do
max_frame_size: max_frame
} = flow

stream = Stream.new(config, stream_id, window, max_frame)
stream = Stream.new(config, stream_id, window, max_frame, request)

case Stream.start_link(stream) do
{:ok, pid} ->
Expand Down
11 changes: 6 additions & 5 deletions lib/connection/processor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ defmodule Kadabra.Connection.Processor do

alias Kadabra.{
Connection,
Error,
Frame,
Hpack,
Stream,
StreamSet
}

alias Kadabra.Connection.{Egress, FlowControl}
alias Kadabra.Connection.{Egress, Error, FlowControl}

alias Kadabra.Frame.{
Continuation,
Expand Down Expand Up @@ -81,8 +80,10 @@ defmodule Kadabra.Connection.Processor do
end

def process(%RstStream{stream_id: stream_id} = frame, state) do
pid = StreamSet.pid_for(state.flow_control.stream_set, stream_id)
Stream.call_recv(pid, frame)
case StreamSet.pid_for(state.flow_control.stream_set, stream_id) do
nil -> nil
pid -> Stream.call_recv(pid, frame)
end

{:ok, state}
end
Expand Down Expand Up @@ -150,7 +151,7 @@ defmodule Kadabra.Connection.Processor do
max_frame_size: max_frame
} = flow_control

stream = Stream.new(config, stream_id, window, max_frame)
stream = Stream.new(config, stream_id, window, max_frame, nil)

case Stream.start_link(stream) do
{:ok, pid} ->
Expand Down
10 changes: 5 additions & 5 deletions lib/connection/settings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule Kadabra.Connection.Settings do
max_frame_size: @default_max_frame_size,
max_header_list_size: nil

alias Kadabra.Error
alias Kadabra.Connection.Error

@type t :: %__MODULE__{
header_table_size: non_neg_integer,
Expand Down Expand Up @@ -54,23 +54,23 @@ defmodule Kadabra.Connection.Settings do
iex> Kadabra.Connection.Settings.put(s, enable_push, 1)
{:ok, %Kadabra.Connection.Settings{enable_push: true}}
iex> Kadabra.Connection.Settings.put(s, enable_push, :bad)
{:error, Kadabra.Error.protocol_error, s}
{:error, Kadabra.Connection.Error.protocol_error, s}

iex> s = %Kadabra.Connection.Settings{}
iex> init_window_size = 0x4
iex> Kadabra.Connection.Settings.put(s, init_window_size, 70_000)
{:ok, %Kadabra.Connection.Settings{initial_window_size: 70_000}}
iex> Kadabra.Connection.Settings.put(s, init_window_size, 5_000_000_000)
{:error, Kadabra.Error.flow_control_error, s}
{:error, Kadabra.Connection.Error.flow_control_error, s}

iex> s = %Kadabra.Connection.Settings{}
iex> max_frame_size = 0x5
iex> Kadabra.Connection.Settings.put(s, max_frame_size, 20_000)
{:ok, %Kadabra.Connection.Settings{max_frame_size: 20_000}}
iex> Kadabra.Connection.Settings.put(s, max_frame_size, 20_000_000)
{:error, Kadabra.Error.protocol_error, s}
{:error, Kadabra.Connection.Error.protocol_error, s}
iex> Kadabra.Connection.Settings.put(s, max_frame_size, 2_000)
{:error, Kadabra.Error.protocol_error, s}
{:error, Kadabra.Connection.Error.protocol_error, s}
"""
@spec put(t, non_neg_integer, term) :: {:ok, t} | {:error, binary, t}
def put(settings, @table_header_size, value) do
Expand Down
5 changes: 4 additions & 1 deletion lib/connection_pool.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ defmodule Kadabra.ConnectionPool do
opts: opts
}

GenServer.start_link(__MODULE__, config)
case opts[:name] do
nil -> GenServer.start_link(__MODULE__, config)
name -> GenServer.start_link(__MODULE__, config, name: name)
end
end

def request(pid, requests) when is_list(requests) do
Expand Down
3 changes: 2 additions & 1 deletion lib/frame/goaway.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ defmodule Kadabra.Frame.Goaway do

defstruct last_stream_id: nil, error_code: nil, debug_data: <<>>

alias Kadabra.{Error, Frame}
alias Kadabra.Connection.Error
alias Kadabra.Frame

@type t :: %__MODULE__{
debug_data: bitstring,
Expand Down
3 changes: 2 additions & 1 deletion lib/frame/rst_stream.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ defmodule Kadabra.Frame.RstStream do

defstruct [:stream_id, :error_code]

alias Kadabra.{Error, Frame}
alias Kadabra.Connection.Error
alias Kadabra.Frame

@type t :: %__MODULE__{
error_code: <<_::32>>,
Expand Down
Loading