From 611f9a9b78cab4005892e13dffb7a2c8e44580ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Fri, 2 Aug 2019 14:30:08 +0200 Subject: 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. --- src/gun_content_handler.erl | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'src/gun_content_handler.erl') 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. -- cgit v1.2.3