ninenines / gun

HTTP/1.1, HTTP/2, Websocket client (and more) for Erlang/OTP.
ISC License
898 stars 231 forks source link

when gun:cancel/2 get "gun_error {badstate,"The stream cannot be found."}" #163

Closed boboxiaodd closed 6 years ago

boboxiaodd commented 6 years ago

i use gun (HTTP2) to push notification to APNs

handle_call({push, DeviceToken, Content}, _From, #{conn := ConnPid, topic := Topic} = State) ->
    Token = token_server:get_token(),
    _StreamRef = gun:post(ConnPid, "/3/device/" ++ DeviceToken, [
        {<<"authorization">>, <<"bearer ", Token/binary>>},
        {<<"apns-expiration">>, "0"},
        {<<"apns-priority">>, "10"},
        {<<"apns-topic">>, Topic}
    ], Content, #{reply_to => self()}),
    {reply, ok, State};

%% ...

handle_info({gun_response, ConnPid, StreamRef, fin, Status, _Headers}, State) ->
    error_logger:info_msg("apns response:~p",[Status]),
    gun:cancel(ConnPid, StreamRef),
    {noreply, State};

output:

=INFO REPORT==== 23-Aug-2018::11:13:51.381696 ===
apns response:200
=INFO REPORT==== 23-Aug-2018::11:13:51.381929 ===
info:{gun_error,<0.102.0>,#Ref<0.1522685329.2782134274.8508>,
                {badstate,"The stream cannot be found."}}

question is :
1 , why catch gun_error 2 , Do we need to call the gun:cancel function, or it will clean up automatically

essen commented 6 years ago

You only need to cancel requests that are not completed, if you sent the request fully and got the response with the fin flag set, then the request has completed and cannot be canceled anymore.