shackle
0.6.20
高性能 Erlang 网络客户端框架
- behavior ( shackle_client ).
- export ([
init / 0 ,
setup / 2 ,
handle_request / 2 ,
handle_data / 2 ,
terminate / 1
]).
- record ( state , {
buffer = <<>> :: binary (),
request_counter = 0 :: non_neg_integer ()
}).
- spec init ( Options :: term ()) ->
{ ok , State :: term ()} |
{ error , Reason :: term ()}.
init ( _Options ) ->
{ ok , # state {}}.
- spec setup ( Socket :: inet : socket (), State :: term ()) ->
{ ok , State :: term ()} |
{ error , Reason :: term (), State :: term ()}.
setup ( 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 .
- spec handle_request ( Request :: term (), State :: term ()) ->
{ ok , RequestId :: external_request_id (), Data :: iodata (), State :: term ()}.
handle_request ( noop , State ) ->
Data = arithmetic_protocol : request ( 0 , noop , 0 , 0 ),
{ ok , undefined , Data , State };
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
}}.
- 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
}}.
- spec terminate ( State :: term ()) -> ok .
terminate ( _State ) -> ok .
shackle_pool : start ( shackle_pool : name (), client (), client_options (), pool_options ())
姓名 | 类型 | 默认 | 描述 |
---|---|---|---|
地址 | inet:ip_address() | inet:主机名() | “127.0.0.1” | 服务器地址(以前的ip) |
港口 | inet:端口号() | 不明确的 | 服务器端口 |
协议 | shackle_tcp |卸扣_udp | shackle_ssl | shackle_tcp | 服务器协议 |
重新连接 | 布尔值() | 真的 | 重新连接关闭的连接 |
最大重新连接时间 | pos_integer() | pos_integer() |无穷大 | 120000 | 最大重新连接时间(以毫秒为单位) |
重新连接时间分钟 | pos_integer() | 1000 | 最短重新连接时间(以毫秒为单位) |
套接字选项 | [gen_tcp:connect_option()| gen_udp:选项()] | [] | 传递给套接字的选项 |
姓名 | 类型 | 默认 | 描述 |
---|---|---|---|
待办事项大小 | pos_integer() | pos_integer() |无穷大 | 1024 | 每个连接的最大并发请求数 |
最大重试次数 | 非负整数() | 3 | 尝试查找活动服务器的最大次数 |
池大小 | pos_integer() | 16 | 连接数 |
池策略 | 随机|循环赛 | 随机的 | 连接选择策略 |
1 > shackle : call ( pool_name , { get , << " test " >>}).
{ ok , << " bar " >>}
2 > { ok , ReqId } = shackle : cast ( pool_name , { get , << " foo " >>}, 500 ).
{ ok , { anchor , anchor_client , # Ref < 0.0 . 0.2407 > }}
3 > shackle : receive_response ( ReqId ).
{ ok , << " bar " >>}
Shackle 与后端无关的遥测库集成。有关 shackle 可以发出的遥测事件列表,请参阅shackle_telemetry
。
make dialyzer
make eunit
make xref
要运行性能测试目标,您必须首先启动服务器:
./bin/server.sh
然后您可以运行bench
或配置profile
目标:
make bench
make profile
姓名 | 描述 |
---|---|
锚 | 内存缓存客户端 |
尖峰节点 | Aerospike 客户端 |
浮标 | HTTP 1.1 客户端 |
耀斑 | 卡夫卡制片人 |
码头 | Cassandra CQL 客户端 |
统计数据 | 统计D客户端 |
The MIT License (MIT)
Copyright (c) 2015-2023 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.