[99s-extend] cowboy how to ruduce the memory usage per long-hold connection

yongboy yongboy at gmail.com
Fri Apr 26 08:11:56 CEST 2013


I have tested one long-hold webapp, when 512000 user connected, the app
used
6801M memory, 6801M*1024K / 512000 = 13.6K/Connection.

Does anyone give me some advice on how to reduce the memory usage per one
connection, thanks very much !

Here is the code snippet:

start(_Type, _Args) ->
        Dispatch = cowboy_router:compile([
            {'_', [{'_', htmlfile_handler, []}]}
        ]),
        cowboy:start_http(my_http_listener, 100,
            [{port, 8000}, {max_connections, infinity}],
            [{env, [{dispatch, Dispatch}]}]
        ),
        count_server:start(),
        htmlfilesimple_sup:start_link().

......

-module(htmlfile_handler).
-behaviour(cowboy_loop_handler).
-export([init/3, info/3, terminate/3]).
-define(HEARBEAT_TIMEOUT, 20*1000).
-record(status, {count=0}).

init(_Any, Req, State) ->
        NowCount = count_server:welcome(),
        io:format("online user ~p :))~n", [NowCount]),

        output_first(Req),
        Req2 = cowboy_req:compact(Req),
        {loop, Req2, State, hibernate}.

%% POST/Short Request
info(_Any, Req, State) ->
        {loop, Req, State, hibernate}.

output_first(Req) ->
        {ok, Reply} = cowboy_req:chunked_reply(200, [{<<"Content-Type">>,
<<"text/html; charset=utf-8">>},

{<<"Connection">>, <<"keep-alive">>}], Req),
        cowboy_req:chunk(<<"<html><body><script>var _ = function (msg) {
parent.s._(msg, document);
};</script>
">>,
                                                                Reply),
        cowboy_req:chunk(gen_output("1::"), Reply).

gen_output(String) ->
        DescList = io_lib:format("<script>_('~s');</script>", [String]),
        list_to_binary(DescList).

terminate(Reason, _Req, _State) ->
        NowCount = count_server:bye(),
        io:format("offline user ~p :(( ~n", [NowCount]).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ninenines.eu/archives/extend/attachments/20130426/9d234e27/attachment.html>


More information about the Extend mailing list