aboutsummaryrefslogtreecommitdiffstats
path: root/src/gun_content_handler.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2019-08-02 14:30:08 +0200
committerLoïc Hoguin <[email protected]>2019-08-05 19:57:13 +0200
commit611f9a9b78cab4005892e13dffb7a2c8e44580ee (patch)
treed8d3fc407110ea12333ba122cf711326e82a7070 /src/gun_content_handler.erl
parent145b9af4bdbb85e2f83959ee8abaa4d9207a4529 (diff)
downloadgun-611f9a9b78cab4005892e13dffb7a2c8e44580ee.tar.gz
gun-611f9a9b78cab4005892e13dffb7a2c8e44580ee.tar.bz2
gun-611f9a9b78cab4005892e13dffb7a2c8e44580ee.zip
Add flow control
Flow control is disabled by default. The initial flow value must be set to enable it (either for the entire connection or on a per-request basis). Flow applies to all HTTP streams as well as Websocket. HTTP/2 pushed streams receive the same value as their originating stream.
Diffstat (limited to 'src/gun_content_handler.erl')
-rw-r--r--src/gun_content_handler.erl21
1 files changed, 14 insertions, 7 deletions
diff --git a/src/gun_content_handler.erl b/src/gun_content_handler.erl
index 78e5e9d..fa180d8 100644
--- a/src/gun_content_handler.erl
+++ b/src/gun_content_handler.erl
@@ -28,7 +28,9 @@
cow_http:headers(), map()) -> {ok, any()} | disable.
%% @todo Make fin | nofin its own type.
-callback handle(fin | nofin, any(), State)
- -> {ok, any(), State} | {done, State} when State::any().
+ -> {ok, any(), non_neg_integer(), State}
+ | {done, non_neg_integer(), State}
+ when State::any().
-spec init(pid(), any(), cow_http:status(),
cow_http:headers(), State) -> State when State::state().
@@ -44,13 +46,18 @@ init(ReplyTo, StreamRef, Status, Headers, [Handler|Tail]) ->
disable -> init(ReplyTo, StreamRef, Status, Headers, Tail)
end.
--spec handle(fin | nofin, any(), State) -> State when State::state().
-handle(_, _, []) ->
- [];
-handle(IsFin, Data0, [{Mod, State0}|Tail]) ->
+-spec handle(fin | nofin, any(), State) -> {ok, non_neg_integer(), State} when State::state().
+handle(IsFin, Data, State) ->
+ handle(IsFin, Data, State, 0, []).
+
+handle(_, _, [], Flow, Acc) ->
+ {ok, Flow, lists:reverse(Acc)};
+handle(IsFin, Data0, [{Mod, State0}|Tail], Flow, Acc) ->
case Mod:handle(IsFin, Data0, State0) of
- {ok, Data, State} -> [{Mod, State}|handle(IsFin, Data, Tail)];
- {done, State} -> [{Mod, State}|Tail]
+ {ok, Data, Inc, State} ->
+ handle(IsFin, Data, Tail, Flow + Inc, [{Mod, State}|Acc]);
+ {done, Inc, State} ->
+ {ok, Flow + Inc, lists:reverse([{Mod, State}|Acc], Tail)}
end.
-spec check_option(list()) -> ok | error.