buoy
High-Performance Erlang HTTP 1.1 Client
Disclaimer:
This HTTP client has been designed for HTTP 1.1 with keep-alive. For performance reasons, it only implements a subset of RFC2616.
Unsupported Features:
- Doesn't accept an arbitrary number of new lines in headers
- Doesn't accept random capitalization of content-length header
- Doesn't protect against malicious servers
API
Examples
1> buoy_app:start().
{ok,[telemetry,knot,metal,foil,shackle,buoy]}
2> Url = buoy_utils:parse_url(<<"http://example.com">>).
{buoy_url,<<"example.com">>,<<"example.com">>,<<"/">>,80,
http}
3> ok = buoy_pool:start(Url, [{pool_size, 1}]).
ok
4> {ok, Resp} = buoy:get(Url, #{timeout => 500}).
{ok,{buoy_resp,done,
<<"<!doctype html>\n<html>\n<head>\n <title>Example Domain</title>\n\n <meta charset=\"utf-8\" />\n "...>>,
1270,
[<<"Cache-Control: max-age=604800">>,
<<"Content-Type: text/html">>,
<<"Date: Mon, 20 Mar 2017 14:48:25 GMT">>,
<<"Etag: \"359670651+gzip+ident\"">>,
<<"Expires: Mon, 27 Mar 2017 14:48:25 GMT">>,
<<"Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT">>,
<<"Server: ECS (cpm/F9D5)">>,<<"Vary: Accept-Encoding">>,
<<"X-Cache: HIT">>,<<"Content-Length: 1270">>],
<<"OK">>,200}}
5> {ok, Headers} = buoy_protocol:headers(Resp).
{ok,[{<<"Cache-Control">>,<<"max-age=604800">>},
{<<"Content-Type">>,<<"text/html">>},
{<<"Date">>,<<"Mon, 20 Mar 2017 14:48:25 GMT">>},
{<<"Etag">>,<<"\"359670651+gzip+ident\"">>},
{<<"Expires">>,<<"Mon, 27 Mar 2017 14:48:25 GMT">>},
{<<"Last-Modified">>,<<"Fri, 09 Aug 2013 23:54:35 GMT">>},
{<<"Server">>,<<"ECS (cpm/F9D5)">>},
{<<"Vary">>,<<"Accept-Encoding">>},
{<<"X-Cache">>,<<"HIT">>},
{<<"Content-Length">>,<<"1270">>}]}Pool Options
| 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 |
| reconnect | boolean() | true | reconnect closed connections |
| reconnect_time_max | pos_integer() | infinity | 120000 | reconnect maximum time |
| reconnect_time_min | pos_integer() | 500 | reconnect minimum time |
| socket_options | [gen_tcp:connect_option() | ssl:tls_client_option()] | [binary, {packet, line}, {packet, raw}, {send_timeout, 50}, {send_timeout_close, true}] | options passed to the socket when connecting |
Errors
All buoy:* calls return {ok, term()} | {error, error_reason()} where error_reason/0 (exported, added in 0.2.8) enumerates:
- buoy-level —
pool_not_started,buoy_not_started,pool_already_started,invalid_url. - HTTP parser —
invalid_headers,invalid_chunk_size(surfaced bybuoy_protocolvia shackle'shandle_datacallback when a malformed response arrives). - shackle that propagate through buoy —
no_server,shackle_not_started,timeout.
error/0 itself stays {error, term()} (not the tight sum) — see include/buoy.hrl for the rationale, briefly: buoy_client's responses/5 has a {error, not_enough_data} buffering branch that's semantically required at runtime but unreachable from dialyzer's flow analysis when error/0 is a closed sum. error_reason/0 covers the documentation half without the analyzer cost.
Telemetry
buoy emits two telemetry events at the request boundary. Attach
handlers via telemetry:attach/4:
| Event | Measurements | Metadata |
|---|---|---|
[buoy, request, sent] | count => 1 | method, host, async |
[buoy, request, error] | count => 1 | method, host, reason |
sent fires when the request hits shackle for dispatch (sync and
async paths both); error fires when the pool lookup fails (e.g.
pool_not_started). Per-request lifecycle (queue / send / receive)
remains observable via shackle's own telemetry — buoy's events
surface the buoy-level routing decision without duplicating that
work.
Tests
make testLicense
The MIT License (MIT)
Copyright (c) 2016-2026 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.