From 516933f9dd2722329b3886c495d5242308958fe1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Mon, 22 Jul 2019 16:17:10 +0200 Subject: Split domain lookup/connect/TLS handshake and add events This changes the way we connect to servers entirely. We now have three states when connecting (domain_lookup, connect and tls_handshake when applicable) and as a result three corresponding timeout options. Each state has a start/end event associated and the event data was tweaked to best match each event. Since the TLS handshake is separate, the transport_opts option was also split into two: tcp_opts and tls_opts. --- src/gun_event.erl | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) (limited to 'src/gun_event.erl') diff --git a/src/gun_event.erl b/src/gun_event.erl index 5332568..4716f47 100644 --- a/src/gun_event.erl +++ b/src/gun_event.erl @@ -27,22 +27,46 @@ -callback init(init_event(), State) -> State. -%% connect_start/connect_end. +%% domain_lookup_start/domain_lookup_end. --type connect_event() :: #{ +-type domain_lookup_event() :: #{ host := inet:hostname() | inet:ip_address(), port := inet:port_number(), - transport := tcp | tls, - transport_opts := [gen_tcp:connect_option()] | [ssl:connect_option()], + tcp_opts := [gen_tcp:connect_option()], timeout := timeout(), - socket => inet:socket() | ssl:sslsocket() | pid(), - protocol => http | http2, + lookup_info => gun_tcp:lookup_info(), + error => any() +}. + +-callback domain_lookup_start(domain_lookup_event(), State) -> State. +-callback domain_lookup_end(domain_lookup_event(), State) -> State. + +%% connect_start/connect_end. + +-type connect_event() :: #{ + lookup_info := gun_tcp:lookup_info(), + timeout := timeout(), + socket => inet:socket(), + protocol => http | http2, %% Only when transport is tcp. error => any() }. -callback connect_start(connect_event(), State) -> State. -callback connect_end(connect_event(), State) -> State. +%% tls_handshake_start/tls_handshake_end. + +-type tls_handshake_event() :: #{ + socket := inet:socket() | ssl:sslsocket(), %% The socket before/after will be different. + tls_opts := [ssl:connect_option()], + timeout := timeout(), + protocol => http | http2, + error => any() +}. + +-callback tls_handshake_start(tls_handshake_event(), State) -> State. +-callback tls_handshake_end(tls_handshake_event(), State) -> State. + %% request_start/request_headers. -type request_start_event() :: #{ @@ -201,16 +225,12 @@ %% terminate. -type terminate_event() :: #{ - state := not_connected | connected, + state := not_connected | domain_lookup | connecting | tls_handshake | connected, reason := normal | shutdown | {shutdown, any()} | any() }. -callback terminate(terminate_event(), State) -> State. -%% @todo domain_lookup_start -%% @todo domain_lookup_end -%% @todo tls_handshake_start -%% @todo tls_handshake_end %% @todo origin_changed %% @todo transport_changed %% @todo push_promise_start -- cgit v1.2.3