This repository has been archived by the owner on Oct 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Client connection pool example
Pouriya Jahanbakhsh edited this page Jul 10, 2017
·
3 revisions
connection_pool.erl
:
-module(connection_pool).
-behaviour(sockerl).
-export([start_link/2
,binary_timestamp/0
,connector_init/2
,handle_packet/3
,handle_disconnect/2
,terminate/3
,code_change/3]).
start_link(Port, Count) ->
Host = "127.0.0.1",
sockerl:start_link_connector_pool({local, connection_pool}
,?MODULE
,none % Init argument
,[{Host, Port} || _ <- lists:seq(1, Count)]
,[{connector_per_address, 2} % Makes 2 connections for every address
,{connector_debug, [trace]}]).
connector_init(none, _SMD) -> % SMD: Sockerl MetaData
%% Send timestamp to server after connecting
{ok, [{packet, binary_timestamp()}]}.
handle_packet(_Packet, _State, _SMD) ->
ok.
handle_disconnect(_State, _SMD) ->
close.
terminate(_Reason, _State, _SMD) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%% Returns timestamp in binary in micro-seconds
binary_timestamp() ->
{Me, S, Mi} = os:timestamp(),
erlang:integer_to_binary(((Me * 1000000) + S) * 1000000 + Mi).
Run echo_server, then compile this and run in shell:
Erlang/OTP 19 [erts-8.3] [source-d5c06c6] [64-bit] [smp:8:8] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V8.3 (abort with ^G)
%% Makes 10*2 connections
1> connection_pool:start_link(8080, 10).
*DBG* Sockerl connector "<0.133.0>" sent packet <<"1499630803422602">>
*DBG* Sockerl connector "<0.133.0>" got packet "1499630803422602"
*DBG* Sockerl connector "<0.136.0>" sent packet <<"1499630803422943">>
*DBG* Sockerl connector "<0.136.0>" got packet "1499630803422943"
*DBG* Sockerl connector "<0.137.0>" sent packet <<"1499630803423228">>
*DBG* Sockerl connector "<0.137.0>" got packet "1499630803423228"
*DBG* Sockerl connector "<0.138.0>" sent packet <<"1499630803423488">>
*DBG* Sockerl connector "<0.138.0>" got packet "1499630803423488"
*DBG* Sockerl connector "<0.139.0>" sent packet <<"1499630803423769">>
*DBG* Sockerl connector "<0.139.0>" got packet "1499630803423769"
*DBG* Sockerl connector "<0.140.0>" sent packet <<"1499630803424030">>
*DBG* Sockerl connector "<0.140.0>" got packet "1499630803424030"
*DBG* Sockerl connector "<0.141.0>" sent packet <<"1499630803424244">>
*DBG* Sockerl connector "<0.141.0>" got packet "1499630803424244"
*DBG* Sockerl connector "<0.142.0>" sent packet <<"1499630803424507">>
*DBG* Sockerl connector "<0.142.0>" got packet "1499630803424507"
*DBG* Sockerl connector "<0.143.0>" sent packet <<"1499630803424721">>
*DBG* Sockerl connector "<0.143.0>" got packet "1499630803424721"
*DBG* Sockerl connector "<0.144.0>" sent packet <<"1499630803424940">>
*DBG* Sockerl connector "<0.144.0>" got packet "1499630803424940"
*DBG* Sockerl connector "<0.145.0>" sent packet <<"1499630803425160">>
*DBG* Sockerl connector "<0.145.0>" got packet "1499630803425160"
*DBG* Sockerl connector "<0.146.0>" sent packet <<"1499630803425377">>
*DBG* Sockerl connector "<0.146.0>" got packet "1499630803425377"
*DBG* Sockerl connector "<0.147.0>" sent packet <<"1499630803425628">>
*DBG* Sockerl connector "<0.147.0>" got packet "1499630803425628"
*DBG* Sockerl connector "<0.148.0>" sent packet <<"1499630803425885">>
*DBG* Sockerl connector "<0.148.0>" got packet "1499630803425885"
*DBG* Sockerl connector "<0.149.0>" sent packet <<"1499630803426144">>
*DBG* Sockerl connector "<0.149.0>" got packet "1499630803426144"
*DBG* Sockerl connector "<0.150.0>" sent packet <<"1499630803426399">>
*DBG* Sockerl connector "<0.150.0>" got packet "1499630803426399"
*DBG* Sockerl connector "<0.151.0>" sent packet <<"1499630803426643">>
*DBG* Sockerl connector "<0.151.0>" got packet "1499630803426643"
*DBG* Sockerl connector "<0.152.0>" sent packet <<"1499630803426855">>
*DBG* Sockerl connector "<0.152.0>" got packet "1499630803426855"
*DBG* Sockerl connector "<0.153.0>" sent packet <<"1499630803427099">>
*DBG* Sockerl connector "<0.153.0>" got packet "1499630803427099"
*DBG* Sockerl connector "<0.154.0>" sent packet <<"1499630803427346">>
*DBG* Sockerl connector "<0.154.0>" got packet "1499630803427346"
{ok,<0.132.0>}
%% Send timestamp trough all pool connections synchronously:
2> [sockerl:send_sync(Pid, connection_pool:binary_timestamp())
|| {_Id, Pid} <- sockerl:get_pool_connections(connection_pool)],ok.
*DBG* Sockerl connector "<0.143.0>" got synchronous request for sending packet <<"1499630870532491">> from "<0.129.0>" with tag "#Ref<0.0.1.234>"
*DBG* Sockerl connector "<0.143.0>" sent packet <<"1499630870532491">>
*DBG* Sockerl connector "<0.143.0>" sent "ok" to "<0.129.0>" with tag "#Ref<0.0.1.234>"
*DBG* Sockerl connector "<0.151.0>" got synchronous request for sending packet <<"1499630870532817">> from "<0.129.0>" with tag "#Ref<0.0.1.238>"
*DBG* Sockerl connector "<0.143.0>" got packet "1499630870532491"
*DBG* Sockerl connector "<0.151.0>" sent packet <<"1499630870532817">>
*DBG* Sockerl connector "<0.151.0>" sent "ok" to "<0.129.0>" with tag "#Ref<0.0.1.238>"
*DBG* Sockerl connector "<0.145.0>" got synchronous request for sending packet <<"1499630870533202">> from "<0.129.0>" with tag "#Ref<0.0.1.243>"
*DBG* Sockerl connector "<0.151.0>" got packet "1499630870532817"
*DBG* Sockerl connector "<0.145.0>" sent packet <<"1499630870533202">>
*DBG* Sockerl connector "<0.145.0>" sent "ok" to "<0.129.0>" with tag "#Ref<0.0.1.243>"
*DBG* Sockerl connector "<0.133.0>" got synchronous request for sending packet <<"1499630870533545">> from "<0.129.0>" with tag "#Ref<0.0.1.248>"
*DBG* Sockerl connector "<0.133.0>" sent packet <<"1499630870533545">>
*DBG* Sockerl connector "<0.145.0>" got packet "1499630870533202"
*DBG* Sockerl connector "<0.133.0>" sent "ok" to "<0.129.0>" with tag "#Ref<0.0.1.248>"
*DBG* Sockerl connector "<0.138.0>" got synchronous request for sending packet <<"1499630870533879">> from "<0.129.0>" with tag "#Ref<0.0.1.253>"
*DBG* Sockerl connector "<0.133.0>" got packet "1499630870533545"
*DBG* Sockerl connector "<0.138.0>" sent packet <<"1499630870533879">>
*DBG* Sockerl connector "<0.138.0>" sent "ok" to "<0.129.0>" with tag "#Ref<0.0.1.253>"
*DBG* Sockerl connector "<0.152.0>" got synchronous request for sending packet <<"1499630870534259">> from "<0.129.0>" with tag "#Ref<0.0.1.258>"
*DBG* Sockerl connector "<0.152.0>" sent packet <<"1499630870534259">>
*DBG* Sockerl connector "<0.138.0>" got packet "1499630870533879"
*DBG* Sockerl connector "<0.152.0>" sent "ok" to "<0.129.0>" with tag "#Ref<0.0.1.258>"
*DBG* Sockerl connector "<0.144.0>" got synchronous request for sending packet <<"1499630870534714">> from "<0.129.0>" with tag "#Ref<0.0.1.263>"
*DBG* Sockerl connector "<0.144.0>" sent packet <<"1499630870534714">>
*DBG* Sockerl connector "<0.152.0>" got packet "1499630870534259"
*DBG* Sockerl connector "<0.144.0>" sent "ok" to "<0.129.0>" with tag "#Ref<0.0.1.263>"
*DBG* Sockerl connector "<0.142.0>" got synchronous request for sending packet <<"1499630870535135">> from "<0.129.0>" with tag "#Ref<0.0.5.1>"
*DBG* Sockerl connector "<0.144.0>" got packet "1499630870534714"
*DBG* Sockerl connector "<0.142.0>" sent packet <<"1499630870535135">>
*DBG* Sockerl connector "<0.142.0>" sent "ok" to "<0.129.0>" with tag "#Ref<0.0.5.1>"
*DBG* Sockerl connector "<0.153.0>" got synchronous request for sending packet <<"1499630870535543">> from "<0.129.0>" with tag "#Ref<0.0.5.5>"
*DBG* Sockerl connector "<0.142.0>" got packet "1499630870535135"
*DBG* Sockerl connector "<0.153.0>" sent packet <<"1499630870535543">>
*DBG* Sockerl connector "<0.153.0>" sent "ok" to "<0.129.0>" with tag "#Ref<0.0.5.5>"
*DBG* Sockerl connector "<0.148.0>" got synchronous request for sending packet <<"1499630870535926">> from "<0.129.0>" with tag "#Ref<0.0.1.269>"
*DBG* Sockerl connector "<0.153.0>" got packet "1499630870535543"
*DBG* Sockerl connector "<0.148.0>" sent packet <<"1499630870535926">>
*DBG* Sockerl connector "<0.148.0>" sent "ok" to "<0.129.0>" with tag "#Ref<0.0.1.269>"
*DBG* Sockerl connector "<0.137.0>" got synchronous request for sending packet <<"1499630870536357">> from "<0.129.0>" with tag "#Ref<0.0.1.271>"
*DBG* Sockerl connector "<0.148.0>" got packet "1499630870535926"
*DBG* Sockerl connector "<0.137.0>" sent packet <<"1499630870536357">>
*DBG* Sockerl connector "<0.137.0>" sent "ok" to "<0.129.0>" with tag "#Ref<0.0.1.271>"
*DBG* Sockerl connector "<0.139.0>" got synchronous request for sending packet <<"1499630870536886">> from "<0.129.0>" with tag "#Ref<0.0.1.274>"
*DBG* Sockerl connector "<0.137.0>" got packet "1499630870536357"
*DBG* Sockerl connector "<0.139.0>" sent packet <<"1499630870536886">>
*DBG* Sockerl connector "<0.139.0>" sent "ok" to "<0.129.0>" with tag "#Ref<0.0.1.274>"
*DBG* Sockerl connector "<0.146.0>" got synchronous request for sending packet <<"1499630870537252">> from "<0.129.0>" with tag "#Ref<0.0.1.279>"
*DBG* Sockerl connector "<0.139.0>" got packet "1499630870536886"
*DBG* Sockerl connector "<0.146.0>" sent packet <<"1499630870537252">>
*DBG* Sockerl connector "<0.146.0>" sent "ok" to "<0.129.0>" with tag "#Ref<0.0.1.279>"
*DBG* Sockerl connector "<0.150.0>" got synchronous request for sending packet <<"1499630870537636">> from "<0.129.0>" with tag "#Ref<0.0.1.282>"
*DBG* Sockerl connector "<0.146.0>" got packet "1499630870537252"
*DBG* Sockerl connector "<0.150.0>" sent packet <<"1499630870537636">>
*DBG* Sockerl connector "<0.150.0>" sent "ok" to "<0.129.0>" with tag "#Ref<0.0.1.282>"
*DBG* Sockerl connector "<0.140.0>" got synchronous request for sending packet <<"1499630870538048">> from "<0.129.0>" with tag "#Ref<0.0.1.285>"
*DBG* Sockerl connector "<0.150.0>" got packet "1499630870537636"
*DBG* Sockerl connector "<0.140.0>" sent packet <<"1499630870538048">>
*DBG* Sockerl connector "<0.140.0>" sent "ok" to "<0.129.0>" with tag "#Ref<0.0.1.285>"
*DBG* Sockerl connector "<0.149.0>" got synchronous request for sending packet <<"1499630870538459">> from "<0.129.0>" with tag "#Ref<0.0.1.288>"
*DBG* Sockerl connector "<0.140.0>" got packet "1499630870538048"
*DBG* Sockerl connector "<0.149.0>" sent packet <<"1499630870538459">>
*DBG* Sockerl connector "<0.149.0>" sent "ok" to "<0.129.0>" with tag "#Ref<0.0.1.288>"
*DBG* Sockerl connector "<0.136.0>" got synchronous request for sending packet <<"1499630870538941">> from "<0.129.0>" with tag "#Ref<0.0.1.291>"
*DBG* Sockerl connector "<0.149.0>" got packet "1499630870538459"
*DBG* Sockerl connector "<0.136.0>" sent packet <<"1499630870538941">>
*DBG* Sockerl connector "<0.136.0>" sent "ok" to "<0.129.0>" with tag "#Ref<0.0.1.291>"
*DBG* Sockerl connector "<0.141.0>" got synchronous request for sending packet <<"1499630870539352">> from "<0.129.0>" with tag "#Ref<0.0.1.295>"
*DBG* Sockerl connector "<0.136.0>" got packet "1499630870538941"
*DBG* Sockerl connector "<0.141.0>" sent packet <<"1499630870539352">>
*DBG* Sockerl connector "<0.141.0>" sent "ok" to "<0.129.0>" with tag "#Ref<0.0.1.295>"
*DBG* Sockerl connector "<0.154.0>" got synchronous request for sending packet <<"1499630870539713">> from "<0.129.0>" with tag "#Ref<0.0.1.299>"
*DBG* Sockerl connector "<0.141.0>" got packet "1499630870539352"
*DBG* Sockerl connector "<0.154.0>" sent packet <<"1499630870539713">>
*DBG* Sockerl connector "<0.154.0>" sent "ok" to "<0.129.0>" with tag "#Ref<0.0.1.299>"
*DBG* Sockerl connector "<0.147.0>" got synchronous request for sending packet <<"1499630870540105">> from "<0.129.0>" with tag "#Ref<0.0.1.302>"
*DBG* Sockerl connector "<0.154.0>" got packet "1499630870539713"
*DBG* Sockerl connector "<0.147.0>" sent packet <<"1499630870540105">>
*DBG* Sockerl connector "<0.147.0>" sent "ok" to "<0.129.0>" with tag "#Ref<0.0.1.302>"
*DBG* Sockerl connector "<0.147.0>" got packet "1499630870540105"
ok
%% See count of connections:
3> length(sockerl:get_pool_connections(connection_pool)).
20
%% Add new connection to pool:
4> sockerl:add_connector(connection_pool, "127.0.0.1", 8080).
*DBG* Sockerl connector "<0.157.0>" sent packet <<"1499630892516829">>
*DBG* Sockerl connector "<0.157.0>" got packet "1499630892516829"
{ok,<0.157.0>}
%% See cout of connections again:
3> length(sockerl:get_pool_connections(connection_pool)).
21