Browse files

Don't use set_resp_body unless strictly required

Now the body is starting to be sent on the same packet as the
response instead of after waiting the TCP round-trip, which
means the client receives a little faster.

This will probably not have an impact on benchmarks except on
small to medium files.
  • Loading branch information...
1 parent 56402fe commit f7a8f1adc31e135880bc05b85de1713b75268568 @essen essen committed Apr 3, 2013
Showing with 13 additions and 19 deletions.
  1. +3 −0 include/leo_http.hrl
  2. +6 −11 src/leo_gateway_http_commons.erl
  3. +4 −8 src/leo_gateway_s3_api.erl
View
3 include/leo_http.hrl
@@ -111,6 +111,9 @@
-define(reply_internal_error(_H, _R), cowboy_req:reply(?HTTP_ST_INTERNAL_ERROR, _H, _R)). %% 500
-define(reply_timeout(_H, _R), cowboy_req:reply(?HTTP_ST_GATEWAY_TIMEOUT, _H, _R)). %% 504
+-define(reply_ok(_H, _B, _R), cowboy_req:reply(?HTTP_ST_OK, _H, _B, _R)). %% 200 with body
+-define(reply_partial_content(_H, _B, _R), cowboy_req:reply(?HTTP_ST_PARTIAL_CONTENT, _H, _B, _R)). %% 206 with body
+
-define(http_header(_R, _K), case cowboy_req:header(_K, _R) of
{undefined, _} -> ?BIN_EMPTY;
{Bin, _} -> Bin
View
17 src/leo_gateway_http_commons.erl
@@ -175,9 +175,8 @@ onrequest_2(Req, Expire, Key, {ok, CachedObj}) ->
{ok, Req2} = ?reply_not_modified(Header, Req),
Req2;
_ ->
- Req2 = cowboy_req:set_resp_body(Body, Req),
- {ok, Req3} = ?reply_ok([?SERVER_HEADER], Req2),
- Req3
+ {ok, Req2} = ?reply_ok([?SERVER_HEADER], Body, Req),
+ Req2
end
end.
@@ -250,12 +249,11 @@ get_object(Req, Key, #req_params{has_inner_cache = HasInnerCache}) ->
void
end,
- Req2 = cowboy_req:set_resp_body(RespObject, Req),
Header = [?SERVER_HEADER,
{?HTTP_HEAD_CONTENT_TYPE, Mime},
{?HTTP_HEAD_ETAG4AWS, ?http_etag(Meta#metadata.checksum)},
{?HTTP_HEAD_LAST_MODIFIED, ?http_date(Meta#metadata.timestamp)}],
- ?reply_ok(Header, Req2);
+ ?reply_ok(Header, RespObject, Req);
%% For a chunked object.
{ok, #metadata{cnumber = TotalChunkedObjs}, _RespObject} ->
@@ -287,13 +285,12 @@ get_object(Req, Key, #req_params{has_inner_cache = HasInnerCache}) ->
get_object_with_cache(Req, Key, CacheObj,_Params) ->
case leo_gateway_rpc_handler:get(Key, CacheObj#cache.etag) of
{ok, match} ->
- Req2 = cowboy_req:set_resp_body(CacheObj#cache.body, Req),
Header = [?SERVER_HEADER,
{?HTTP_HEAD_CONTENT_TYPE, CacheObj#cache.content_type},
{?HTTP_HEAD_ETAG4AWS, ?http_etag(CacheObj#cache.etag)},
{?HTTP_HEAD_LAST_MODIFIED, leo_http:rfc1123_date(CacheObj#cache.mtime)},
{?HTTP_HEAD_X_FROM_CACHE, <<"True">>}],
- ?reply_ok(Header, Req2);
+ ?reply_ok(Header, CacheObj#cache.body, Req);
{ok, Meta, Body} ->
Mime = leo_mime:guess_mime(Key),
Val = term_to_binary(#cache{etag = Meta#metadata.checksum,
@@ -303,12 +300,11 @@ get_object_with_cache(Req, Key, CacheObj,_Params) ->
_ = ecache_api:put(Key, Val),
- Req2 = cowboy_req:set_resp_body(Body, Req),
Header = [?SERVER_HEADER,
{?HTTP_HEAD_CONTENT_TYPE, Mime},
{?HTTP_HEAD_ETAG4AWS, ?http_etag(Meta#metadata.checksum)},
{?HTTP_HEAD_LAST_MODIFIED, ?http_date(Meta#metadata.timestamp)}],
- ?reply_ok(Header, Req2);
+ ?reply_ok(Header, Body, Req);
{error, not_found} ->
?reply_not_found([?SERVER_HEADER], Req);
{error, ?ERR_TYPE_INTERNAL_ERROR} ->
@@ -507,10 +503,9 @@ range_object(Req, Key, #req_params{range_header = RangeHeader}) ->
case leo_gateway_rpc_handler:get(Key, Start, End) of
{ok, _Meta, RespObject} ->
Mime = leo_mime:guess_mime(Key),
- Req2 = cowboy_req:set_resp_body(RespObject, Req),
Header = [?SERVER_HEADER,
{?HTTP_HEAD_CONTENT_TYPE, Mime}],
- ?reply_partial_content(Header, Req2);
+ ?reply_partial_content(Header, RespObject, Req);
{error, not_found} ->
?reply_not_found([?SERVER_HEADER], Req);
{error, ?ERR_TYPE_INTERNAL_ERROR} ->
View
12 src/leo_gateway_s3_api.erl
@@ -110,10 +110,9 @@ get_bucket(Req0, Key, #req_params{access_key_id = AccessKeyId,
qs_prefix = Prefix}) ->
case get_bucket_1(AccessKeyId, Key, none, none, 1000, Prefix) of
{ok, Meta, XML} when is_list(Meta) == true ->
- Req1 = cowboy_req:set_resp_body(XML, Req0),
Header = [?SERVER_HEADER,
{?HTTP_HEAD_CONTENT_TYPE, ?HTTP_CTYPE_XML}],
- ?reply_ok(Header, Req1);
+ ?reply_ok(Header, XML, Req0);
{error, not_found} ->
?reply_not_found([?SERVER_HEADER], Req0);
{error, ?ERR_TYPE_INTERNAL_ERROR} ->
@@ -386,8 +385,7 @@ handle_2({ok,_AccessKeyId}, Req0, ?HTTP_POST, _, #req_params{path = Path0,
[Bucket|Path1] = leo_misc:binary_tokens(Path0, ?BIN_SLASH),
XML = gen_upload_initiate_xml(Bucket, Path1, UploadId),
- Req1 = cowboy_req:set_resp_body(XML, Req0),
- ?reply_ok([?SERVER_HEADER], Req1);
+ ?reply_ok([?SERVER_HEADER], XML, Req0);
{error, timeout} ->
?reply_timeout([?SERVER_HEADER], Req0);
{error, Cause} ->
@@ -497,8 +495,7 @@ handle_multi_upload_2({ok, Bin, Req0}, _Req, Path0) ->
[Bucket|Path1] = leo_misc:binary_tokens(Path0, ?BIN_SLASH),
ETag1 = leo_hex:integer_to_hex(ETag0, 32),
XML = gen_upload_completion_xml(Bucket, Path1, ETag1),
- Req1 = cowboy_req:set_resp_body(XML, Req0),
- ?reply_ok([?SERVER_HEADER], Req1);
+ ?reply_ok([?SERVER_HEADER], XML, Req0);
{error, Cause} ->
?error("handle_multi_upload_2/3", "path:~s, cause:~p", [binary_to_list(Path0), Cause]),
?reply_internal_error([?SERVER_HEADER], Req0)
@@ -571,10 +568,9 @@ resp_copy_obj_xml(Req, Meta) ->
XML = io_lib:format(?XML_COPY_OBJ_RESULT,
[leo_http:web_date(Meta#metadata.timestamp),
leo_hex:integer_to_hex(Meta#metadata.checksum, 32)]),
- Req1 = cowboy_req:set_resp_body(XML, Req),
?reply_ok([?SERVER_HEADER,
{?HTTP_HEAD_CONTENT_TYPE, ?HTTP_CTYPE_XML}
- ], Req1).
+ ], XML, Req).
%% @doc Retrieve header values from a request

0 comments on commit f7a8f1a

Please sign in to comment.