diff options
author | Loïc Hoguin <[email protected]> | 2019-04-26 15:59:58 +0200 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2019-04-26 15:59:58 +0200 |
commit | 352206dee80905b10fa9af97f065ce7bee928d87 (patch) | |
tree | 5eb48b3c5689c61cb26329317475ee3b67370752 /src/gun.erl | |
parent | f422624ce4527ce5b07bad63c5fda50e13ff4994 (diff) | |
download | gun-352206dee80905b10fa9af97f065ce7bee928d87.tar.gz gun-352206dee80905b10fa9af97f065ce7bee928d87.tar.bz2 gun-352206dee80905b10fa9af97f065ce7bee928d87.zip |
Add specs to await functions, document error type better
Diffstat (limited to 'src/gun.erl')
-rw-r--r-- | src/gun.erl | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/src/gun.erl b/src/gun.erl index d5a39af..b1f493b 100644 --- a/src/gun.erl +++ b/src/gun.erl @@ -488,14 +488,22 @@ connect(ServerPid, Destination, Headers, ReqOpts) -> %% Awaiting gun messages. -%% @todo spec await await_body - +-type resp_headers() :: [{binary(), binary()}]. +-type await_result() :: {inform, 100..199, resp_headers()} + | {response, fin | nofin, non_neg_integer(), resp_headers()} + | {data, fin | nofin, binary()} + | {trailers, resp_headers()} + | {push, reference(), binary(), binary(), resp_headers()} + | {error, {stream_error | connection_error | down, any()} | timeout}. + +-spec await(pid(), reference()) -> await_result(). await(ServerPid, StreamRef) -> MRef = monitor(process, ServerPid), Res = await(ServerPid, StreamRef, 5000, MRef), demonitor(MRef, [flush]), Res. +-spec await(pid(), reference(), timeout() | reference()) -> await_result(). await(ServerPid, StreamRef, MRef) when is_reference(MRef) -> await(ServerPid, StreamRef, 5000, MRef); await(ServerPid, StreamRef, Timeout) -> @@ -505,6 +513,7 @@ await(ServerPid, StreamRef, Timeout) -> Res. %% @todo Add gun_upgrade and gun_ws? +-spec await(pid(), reference(), timeout(), reference()) -> await_result(). await(ServerPid, StreamRef, Timeout, MRef) -> receive {gun_inform, ServerPid, StreamRef, Status, Headers} -> @@ -527,12 +536,18 @@ await(ServerPid, StreamRef, Timeout, MRef) -> {error, timeout} end. +-type await_body_result() :: {ok, binary()} + | {ok, binary(), resp_headers()} + | {error, {stream_error | connection_error | down, any()} | timeout}. + +-spec await_body(pid(), reference()) -> await_body_result(). await_body(ServerPid, StreamRef) -> MRef = monitor(process, ServerPid), Res = await_body(ServerPid, StreamRef, 5000, MRef, <<>>), demonitor(MRef, [flush]), Res. +-spec await_body(pid(), reference(), timeout() | reference()) -> await_body_result(). await_body(ServerPid, StreamRef, MRef) when is_reference(MRef) -> await_body(ServerPid, StreamRef, 5000, MRef, <<>>); await_body(ServerPid, StreamRef, Timeout) -> @@ -541,6 +556,7 @@ await_body(ServerPid, StreamRef, Timeout) -> demonitor(MRef, [flush]), Res. +-spec await_body(pid(), reference(), timeout(), reference()) -> await_body_result(). await_body(ServerPid, StreamRef, Timeout, MRef) -> await_body(ServerPid, StreamRef, Timeout, MRef, <<>>). @@ -565,14 +581,14 @@ await_body(ServerPid, StreamRef, Timeout, MRef, Acc) -> {error, timeout} end. --spec await_up(pid()) -> {ok, http | http2} | {error, atom()}. +-spec await_up(pid()) -> {ok, http | http2} | {error, {down, any()} | timeout}. await_up(ServerPid) -> MRef = monitor(process, ServerPid), Res = await_up(ServerPid, 5000, MRef), demonitor(MRef, [flush]), Res. --spec await_up(pid(), reference() | timeout()) -> {ok, http | http2} | {error, atom()}. +-spec await_up(pid(), reference() | timeout()) -> {ok, http | http2} | {error, {down, any()} | timeout}. await_up(ServerPid, MRef) when is_reference(MRef) -> await_up(ServerPid, 5000, MRef); await_up(ServerPid, Timeout) -> @@ -581,7 +597,7 @@ await_up(ServerPid, Timeout) -> demonitor(MRef, [flush]), Res. --spec await_up(pid(), timeout(), reference()) -> {ok, http | http2} | {error, atom()}. +-spec await_up(pid(), timeout(), reference()) -> {ok, http | http2} | {error, {down, any()} | timeout}. await_up(ServerPid, Timeout, MRef) -> receive {gun_up, ServerPid, Protocol} -> |