Skip to content

Commit

Permalink
Changed the h2_stream behaviour to take additional options on init
Browse files Browse the repository at this point in the history
  • Loading branch information
joedevivo committed Jun 28, 2017
1 parent 185c009 commit 2dd1e28
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/chatterbox_static_stream.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
-behaviour(h2_stream).

-export([
init/2,
init/3,
on_receive_request_headers/2,
on_send_push_promise/2,
on_receive_request_data/2,
Expand All @@ -18,7 +18,7 @@
stream_id :: stream_id()
}).

init(ConnPid, StreamId) ->
init(ConnPid, StreamId, _) ->
%% You need to pull settings here from application:env or something
{ok, #cb_static{connection_pid=ConnPid,
stream_id=StreamId}}.
Expand Down
5 changes: 5 additions & 0 deletions src/h2_connection.erl
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
next_available_stream_id = 2 :: stream_id(),
streams :: h2_stream_set:stream_set(),
stream_callback_mod = application:get_env(chatterbox, stream_callback_mod, chatterbox_static_stream) :: module(),
stream_callback_opts = application:get_env(chatterbox, stream_callback_opts, []) :: list(),
buffer = empty :: empty | {binary, binary()} | {frame, h2_frame:header(), binary()},
continuation = undefined :: undefined | #continuation_state{},
flow_control = auto :: auto | manual,
Expand Down Expand Up @@ -598,6 +599,7 @@ route_frame({#frame_header{type=?HEADERS}=FH, _Payload}=Frame,
StreamId,
self(),
Conn#connection.stream_callback_mod,
Conn#connection.stream_callback_opts,
Conn#connection.socket,
(Conn#connection.peer_settings)#settings.initial_window_size,
(Conn#connection.self_settings)#settings.initial_window_size,
Expand Down Expand Up @@ -715,6 +717,7 @@ route_frame({H=#frame_header{
PSID,
NotifyPid,
Conn#connection.stream_callback_mod,
Conn#connection.stream_callback_opts,
Conn#connection.socket,
(Conn#connection.peer_settings)#settings.initial_window_size,
(Conn#connection.self_settings)#settings.initial_window_size,
Expand Down Expand Up @@ -1103,6 +1106,7 @@ handle_sync_event({new_stream, NotifyPid}, _F, StateName,
NextId,
NotifyPid,
Conn#connection.stream_callback_mod,
Conn#connection.stream_callback_opts,
Conn#connection.socket,
Conn#connection.peer_settings#settings.initial_window_size,
Conn#connection.self_settings#settings.initial_window_size,
Expand Down Expand Up @@ -1659,6 +1663,7 @@ send_request(NextId, NotifyPid, Conn, Streams, Headers, Body) ->
NextId,
NotifyPid,
Conn#connection.stream_callback_mod,
Conn#connection.stream_callback_opts,
Conn#connection.socket,
Conn#connection.peer_settings#settings.initial_window_size,
Conn#connection.self_settings#settings.initial_window_size,
Expand Down
13 changes: 9 additions & 4 deletions src/h2_stream.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

%% Public API
-export([
start_link/4,
start_link/5,
send_pp/2,
send_data/2,
stream_id/0,
Expand Down Expand Up @@ -71,7 +71,9 @@

-callback init(
Conn :: pid(),
StreamId :: stream_id()) ->
StreamId :: stream_id(),
CallbackOptions :: list()
) ->
{ok, callback_state()}.

-callback on_receive_request_headers(
Expand All @@ -98,14 +100,16 @@
StreamId :: stream_id(),
Connection :: pid(),
CallbackModule :: module(),
CallbackOptions :: list(),
Socket :: sock:socket()
) ->
{ok, pid()} | ignore | {error, term()}.
start_link(StreamId, Connection, CallbackModule, Socket) ->
start_link(StreamId, Connection, CallbackModule, CallbackOptions, Socket) ->
gen_fsm:start_link(?MODULE,
[StreamId,
Connection,
CallbackModule,
CallbackOptions,
Socket],
[]).

Expand Down Expand Up @@ -146,10 +150,11 @@ init([
StreamId,
ConnectionPid,
CB,
CBOptions,
Socket
]) ->
%% TODO: Check for CB implementing this behaviour
{ok, CallbackState} = CB:init(ConnectionPid, StreamId),
{ok, CallbackState} = CB:init(ConnectionPid, StreamId, CBOptions),

{ok, idle, #stream_state{
callback_mod=CB,
Expand Down
5 changes: 4 additions & 1 deletion src/h2_stream_set.erl
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
-export(
[
new/1,
new_stream/7,
new_stream/8,
get/2,
upsert/2,
sort/1
Expand Down Expand Up @@ -202,6 +202,7 @@ new(server) ->
StreamId :: stream_id(),
NotifyPid :: pid(),
CBMod :: module(),
CBOpts :: list(),
Socket :: sock:socket(),
InitialSendWindow :: integer(),
InitialRecvWindow :: integer(),
Expand All @@ -212,6 +213,7 @@ new_stream(
StreamId,
NotifyPid,
CBMod,
CBOpts,
Socket,
InitialSendWindow,
InitialRecvWindow,
Expand All @@ -227,6 +229,7 @@ new_stream(
StreamId,
self(),
CBMod,
CBOpts,
Socket
),
NewStream = #active_stream{
Expand Down
9 changes: 5 additions & 4 deletions test/double_body_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
-behaviour(h2_stream).

-export([
init/2,
init/3,
on_receive_request_headers/2,
on_send_push_promise/2,
on_receive_request_data/2,
Expand All @@ -16,9 +16,10 @@
stream_id :: stream_id()
}).

-spec init(pid(), stream_id()) -> {ok, any()}.
init(ConnPid, StreamId) -> {ok, #state{conn_pid=ConnPid,
stream_id=StreamId}}.
-spec init(pid(), stream_id(), list()) -> {ok, any()}.
init(ConnPid, StreamId, _Opts) ->
{ok, #state{conn_pid=ConnPid,
stream_id=StreamId}}.

-spec on_receive_request_headers(
Headers :: hpack:headers(),
Expand Down
9 changes: 5 additions & 4 deletions test/echo_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
-behaviour(h2_stream).

-export([
init/2,
init/3,
on_receive_request_headers/2,
on_send_push_promise/2,
on_receive_request_data/2,
Expand All @@ -17,9 +17,10 @@
buffer = <<>> :: binary()
}).

-spec init(pid(), stream_id()) -> {ok, any()}.
init(ConnPid, StreamId) -> {ok, #state{conn_pid=ConnPid,
stream_id=StreamId}}.
-spec init(pid(), stream_id(), list()) -> {ok, any()}.
init(ConnPid, StreamId, _Opts) ->
{ok, #state{conn_pid=ConnPid,
stream_id=StreamId}}.

-spec on_receive_request_headers(
Headers :: hpack:headers(),
Expand Down
3 changes: 3 additions & 0 deletions test/flow_control_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ init_per_suite(Config) ->
application:ensure_started(crypto),
Config.

end_per_suite(_Config) ->
ok.

init_per_testcase(
exceed_server_connection_receive_window,
Config) ->
Expand Down
6 changes: 3 additions & 3 deletions test/flow_control_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
-behaviour(h2_stream).

-export([
init/2,
init/3,
on_receive_request_headers/2,
on_send_push_promise/2,
on_receive_request_data/2,
Expand All @@ -18,8 +18,8 @@
stream_id :: stream_id()
}).

-spec init(pid(), stream_id()) -> {ok, any()}.
init(ConnPid, StreamId) ->
-spec init(pid(), stream_id(), list()) -> {ok, any()}.
init(ConnPid, StreamId, _Opts) ->
{ok, #state{conn_pid=ConnPid,
stream_id=StreamId}}.

Expand Down
2 changes: 2 additions & 0 deletions test/http2_spec_5_1_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ init_per_suite(Config) ->
application:ensure_started(crypto),
Config.

end_per_suite(_Config) ->
ok.

init_per_testcase(total_streams_above_max_concurrent, Config) ->
chatterbox_test_buddy:start(
Expand Down
9 changes: 5 additions & 4 deletions test/peer_test_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
-behaviour(h2_stream).

-export([
init/2,
init/3,
on_receive_request_headers/2,
on_send_push_promise/2,
on_receive_request_data/2,
Expand All @@ -18,9 +18,10 @@
inet:port_number()}
}).

-spec init(pid(), stream_id()) -> {ok, any()}.
init(ConnPid, StreamId) -> {ok, #state{conn_pid=ConnPid,
stream_id=StreamId}}.
-spec init(pid(), stream_id(), list()) -> {ok, any()}.
init(ConnPid, StreamId, _Opts) ->
{ok, #state{conn_pid=ConnPid,
stream_id=StreamId}}.

-spec on_receive_request_headers(
Headers :: hpack:headers(),
Expand Down
4 changes: 2 additions & 2 deletions test/server_connection_receive_window.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-behaviour(h2_stream).

-export([
init/2,
init/3,
on_receive_request_headers/2,
on_send_push_promise/2,
on_receive_request_data/2,
Expand All @@ -14,7 +14,7 @@
req_headers=[]
}).

init(_ConnPid, _StreamId) ->
init(_ConnPid, _StreamId, _Opts) ->
%% You need to pull settings here from application:env or something
{ok, #cb_static{}}.

Expand Down
4 changes: 2 additions & 2 deletions test/server_stream_receive_window.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-behaviour(h2_stream).

-export([
init/2,
init/3,
on_receive_request_headers/2,
on_send_push_promise/2,
on_receive_request_data/2,
Expand All @@ -14,7 +14,7 @@
req_headers=[]
}).

init(_ConnPid, _StreamId) ->
init(_ConnPid, _StreamId, _Opts) ->
%% You need to pull settings here from application:env or something
{ok, #cb_static{}}.

Expand Down

0 comments on commit 2dd1e28

Please sign in to comment.