Author: Louis-Philippe Gauthier.
Non-blocking Erlang client framework
- Erlang 16.0 +
- Backpressure via backlog (OOM protection)
- Fast pool implementation (random, round_robin)
- Performance optimized
- Request pipelining
-behavior(shackle_client).
-export([
after_connect/2,
handle_data/2,
handle_request/2,
handle_timing/2,
options/0,
terminate/1
]).
-record(state, {
buffer = <<>>,
request_counter = 0
}).
%% called after the connection is established
-callback after_connect(Socket :: inet:socket(), State :: term()) ->
{ok, State :: term()} |
{error, Reason :: term(), State :: term()}.
after_connect(Socket, State) ->
case gen_tcp:send(Socket, <<"INIT">>) of
ok ->
case gen_tcp:recv(Socket, 0) of
{ok, <<"OK">>} ->
{ok, State};
{error, Reason} ->
{error, Reason, State}
end;
{error, Reason} ->
{error, Reason, State}
end.
%% handles data received on the connection
-spec handle_data(Data :: binary(), State :: term()) ->
{ok, [{RequestId :: external_request_id(), Reply :: term()}], State :: term()}.
handle_data(Data, #state {
buffer = Buffer
} = State) ->
Data2 = <<Buffer/binary, Data/binary>>,
{Replies, Buffer2} = parse_replies(Data2, []),
{ok, Replies, State#state {
buffer = Buffer2
}}.
%% handles request serialization
-spec handle_request(Request :: term(), State :: term()) ->
{ok, RequestId :: external_request_id(), Data :: iodata(), State :: term()}.
handle_request({Operation, A, B}, #state {
request_counter = RequestCounter
} = State) ->
RequestId = request_id(RequestCounter),
Data = request(RequestId, Operation, A, B),
{ok, RequestId, Data, State#state {
request_counter = RequestCounter + 1
}}.
%% handles timing information (in microseconds)
-spec handle_timing(Request :: term(), Timing :: [non_neg_integer()]) -> ok.
handle_timing(_Request, [_Pool, _Request, _Response]) ->
ok.
%% client config
-spec options() -> {ok, Options :: client_options()}.
options() ->
{ok, [
{port, 123},
{reconnect, true},
{state, #state {}}
]}.
%% called when the client is terminating
-spec terminate(State :: term()) -> ok.
terminate(_State) -> ok.
Name | Type | Default | Description |
---|---|---|---|
connect_options | [gen_tcp:connect_option()] | [{send_timeout, 50}, {send_timeout_close, true}] | options passed to gen_tcp:connect/2 |
ip | inet:ip_address() | inet:hostname() | "127.0.0.1" | server ip |
port | inet:port_number() | undefined | server port |
reconnect | boolean() | true | reconnect closed connections |
reconnect_time_max | pos_integer() | timer:minutes(2) | maximum reconnect time |
reconnect_time_min | pos_integer() | timer:seconds(1) | minimum reconnect time |
state | term() | undefined | client state |
shackle_pool:start(pool_name, client, [{pool_size, 32}]).
Name | Type | Default | Description |
---|---|---|---|
backlog_size | pos_integer() | 1024 | maximum number of concurrent requests per connection |
pool_size | pos_integer() | 16 | number of connections |
pool_strategy | random | round_robin | random | connection selection strategy |
1> shackle:call(pool_name, {get, <<"test">>}).
{ok, <<"bar">>}
2> {ok, ReqId} = shackle:cast(pool_name, {get, <<"foo">>}).
{ok, {anchor, anchor_client, #Ref<0.0.0.2407>}}
3> shackle:receive_response(ReqId).
{ok, <<"bar">>}
make dialyzer
make elvis
make eunit
make xref
* Elvis requires Erlang 17.0 +
Name | Description |
---|---|
anchor | Memcached client |
marina | Cassandra CQL3 client |
The MIT License (MIT)
Copyright (c) 2015 Louis-Philippe Gauthier
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.