-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement components as a gen_statem over ranch
- Loading branch information
1 parent
fd3ebe1
commit 95a96b4
Showing
9 changed files
with
629 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
-module(mongoose_component_ranch). | ||
|
||
-behaviour(mongoose_component_socket). | ||
|
||
-export([socket_new/2, | ||
socket_peername/1, | ||
socket_handle_data/2, | ||
socket_activate/1, | ||
socket_close/1, | ||
socket_send_xml/2 | ||
]). | ||
|
||
-record(state, { | ||
ranch_ref :: ranch:ref(), | ||
socket :: ranch_transport:socket(), | ||
ip :: {inet:ip_address(), inet:port_number()} | ||
}). | ||
|
||
-type state() :: #state{}. | ||
|
||
-spec socket_new(term(), mongoose_listener:options()) -> state(). | ||
socket_new({ranch_tcp, RanchRef}, _Opts) -> | ||
{ok, TcpSocket} = ranch:handshake(RanchRef), | ||
{ok, Ip} = ranch_tcp:peername(TcpSocket), | ||
#state{ | ||
ranch_ref = RanchRef, | ||
socket = TcpSocket, | ||
ip = Ip}. | ||
|
||
-spec socket_peername(state()) -> {inet:ip_address(), inet:port_number()}. | ||
socket_peername(#state{ip = Ip}) -> | ||
Ip. | ||
|
||
-spec socket_handle_data(state(), {tcp, term(), iodata()}) -> | ||
iodata() | {raw, [exml:element()]} | {error, term()}. | ||
socket_handle_data(#state{socket = Socket}, {tcp, Socket, Data}) -> | ||
mongoose_instrument:execute(component_tcp_data_in, #{}, #{byte_size => byte_size(Data)}), | ||
Data. | ||
|
||
-spec socket_activate(state()) -> ok. | ||
socket_activate(#state{socket = Socket}) -> | ||
ranch_tcp:setopts(Socket, [{active, once}]). | ||
|
||
-spec socket_close(state()) -> ok. | ||
socket_close(#state{socket = Socket}) -> | ||
ranch_tcp:close(Socket). | ||
|
||
-spec socket_send_xml(state(), iodata() | exml_stream:element() | [exml_stream:element()]) -> | ||
ok | {error, term()}. | ||
socket_send_xml(#state{socket = Socket}, XML) -> | ||
Text = exml:to_iolist(XML), | ||
case send(Socket, Text) of | ||
ok -> | ||
ok; | ||
Error -> | ||
Error | ||
end. | ||
|
||
-spec send(ranch_transport:socket(), iodata()) -> ok | {error, term()}. | ||
send(Socket, Data) -> | ||
mongoose_instrument:execute(component_tcp_data_out, #{}, #{byte_size => iolist_size(Data)}), | ||
ranch_tcp:send(Socket, Data). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
-module(mongoose_component_socket). | ||
|
||
-export([new/3, handle_data/2, activate/1, close/1, send_xml/2]). | ||
|
||
-callback socket_new(term(), mongoose_c2s:listener_opts()) -> state(). | ||
-callback socket_peername(state()) -> {inet:ip_address(), inet:port_number()}. | ||
-callback socket_handle_data(state(), {tcp, term(), iodata()}) -> | ||
iodata() | {raw, [exml:element()]} | {error, term()}. | ||
-callback socket_activate(state()) -> ok. | ||
-callback socket_close(state()) -> ok. | ||
-callback socket_send_xml(state(), iodata() | exml_stream:element() | [exml_stream:element()]) -> | ||
ok | {error, term()}. | ||
|
||
-record(component_socket, {module :: module(), | ||
state :: state()}). | ||
-type socket() :: #component_socket{}. | ||
-type state() :: term(). | ||
-type conn_type() :: component. | ||
-export_type([socket/0, state/0, conn_type/0]). | ||
|
||
-spec new(module(), term(), mongoose_listener:options()) -> socket(). | ||
new(Module, SocketOpts, LOpts) -> | ||
State = Module:socket_new(SocketOpts, LOpts), | ||
C2SSocket = #component_socket{ | ||
module = Module, | ||
state = State}, | ||
activate(C2SSocket), | ||
C2SSocket. | ||
|
||
-spec handle_data(socket(), {tcp, term(), iodata()}) -> | ||
iodata() | {raw, [term()]} | {error, term()}. | ||
handle_data(#component_socket{module = Module, state = State}, Payload) -> | ||
Module:socket_handle_data(State, Payload); | ||
handle_data(_, _) -> | ||
{error, bad_packet}. | ||
|
||
-spec activate(socket()) -> ok | {error, term()}. | ||
activate(#component_socket{module = Module, state = State}) -> | ||
Module:socket_activate(State). | ||
|
||
-spec close(socket()) -> ok. | ||
close(#component_socket{module = Module, state = State}) -> | ||
Module:socket_close(State). | ||
|
||
-spec send_xml(socket(), exml_stream:element() | [exml_stream:element()]) -> ok | {error, term()}. | ||
send_xml(#component_socket{module = Module, state = State}, XML) -> | ||
Module:socket_send_xml(State, XML). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
%%%---------------------------------------------------------------------- | ||
%%% File : mongoose_transport.erl | ||
%%% Author : Piotr Nosek <[email protected]> | ||
%%% Purpose : transport module for s2s and components connection | ||
%%% Purpose : transport module for s2s connection | ||
%%% Created : 18 Jan 2017 | ||
%%%---------------------------------------------------------------------- | ||
|
||
|
@@ -26,7 +26,7 @@ | |
-type peercert_return() :: no_peer_cert | {ok, #'Certificate'{}}. | ||
|
||
-type stanza_size() :: pos_integer() | infinity. | ||
-type connection_type() :: s2s | component | undefined. | ||
-type connection_type() :: s2s | undefined. | ||
|
||
-type options() :: #{max_stanza_size := stanza_size(), | ||
hibernate_after := non_neg_integer(), | ||
|
@@ -182,10 +182,6 @@ send_text(SocketData, Data) -> | |
send_element(#socket_data{connection_type = s2s} = SocketData, El) -> | ||
mongoose_instrument:execute(s2s_xmpp_element_size_out, #{}, #{byte_size => exml:xml_size(El)}), | ||
BinEl = exml:to_binary(El), | ||
send_text(SocketData, BinEl); | ||
send_element(#socket_data{connection_type = component} = SocketData, El) -> | ||
mongoose_instrument:execute(component_xmpp_element_size_out, #{}, #{byte_size => exml:xml_size(El)}), | ||
BinEl = exml:to_binary(El), | ||
send_text(SocketData, BinEl). | ||
|
||
-spec get_peer_certificate(socket_data()) -> mongoose_tls:cert(). | ||
|
@@ -413,9 +409,6 @@ process_data(Data, #state{parser = Parser, | |
|
||
wrap_xml_elements_and_update_metrics(E, s2s) -> | ||
mongoose_instrument:execute(s2s_xmpp_element_size_in, #{}, #{byte_size => exml:xml_size(E)}), | ||
wrap_xml(E); | ||
wrap_xml_elements_and_update_metrics(E, component) -> | ||
mongoose_instrument:execute(component_xmpp_element_size_in, #{}, #{byte_size => exml:xml_size(E)}), | ||
wrap_xml(E). | ||
|
||
wrap_xml(#xmlel{} = E) -> | ||
|
@@ -434,15 +427,7 @@ update_transport_metrics(Data, #{connection_type := s2s, direction := in, sockmo | |
update_transport_metrics(Data, #{connection_type := s2s, direction := out, sockmod := gen_tcp}) -> | ||
mongoose_instrument:execute(s2s_tcp_data_out, #{}, #{byte_size => byte_size(Data)}); | ||
update_transport_metrics(Data, #{connection_type := s2s, direction := out, sockmod := mongoose_tls}) -> | ||
mongoose_instrument:execute(s2s_tls_data_out, #{}, #{byte_size => byte_size(Data)}); | ||
update_transport_metrics(Data, #{connection_type := component, direction := in, sockmod := gen_tcp}) -> | ||
mongoose_instrument:execute(component_tcp_data_in, #{}, #{byte_size => byte_size(Data)}); | ||
update_transport_metrics(Data, #{connection_type := component, direction := in, sockmod := mongoose_tls}) -> | ||
mongoose_instrument:execute(component_tls_data_in, #{}, #{byte_size => byte_size(Data)}); | ||
update_transport_metrics(Data, #{connection_type := component, direction := out, sockmod := gen_tcp}) -> | ||
mongoose_instrument:execute(component_tcp_data_out, #{}, #{byte_size => byte_size(Data)}); | ||
update_transport_metrics(Data, #{connection_type := component, direction := out, sockmod := mongoose_tls}) -> | ||
mongoose_instrument:execute(component_tls_data_out, #{}, #{byte_size => byte_size(Data)}). | ||
mongoose_instrument:execute(s2s_tls_data_out, #{}, #{byte_size => byte_size(Data)}). | ||
|
||
-spec maybe_pause(Delay :: non_neg_integer(), state()) -> any(). | ||
maybe_pause(_, #state{dest_pid = undefined}) -> | ||
|