Skip to content

Commit

Permalink
Tune memory usage on 0.x (#3211)
Browse files Browse the repository at this point in the history
* mod_cookie_consent: small css changes

* Tune memory usage with forced gc
  • Loading branch information
mworrell committed Dec 14, 2022
1 parent 21df4c6 commit 39047f2
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 32 deletions.
11 changes: 6 additions & 5 deletions modules/mod_cookie_consent/lib/css/cookie_consent.css
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
overflow: auto;
z-index: 999999;
color: white; }
.cookie-consent .cookie-consent__explanation {
max-width: 70%; }
.cookie-consent .cookie-consent__upper,
.cookie-consent .cookie-consent__lower {
padding-top: 50px;
Expand Down Expand Up @@ -114,10 +112,11 @@
display: flex;
flex-direction: column;
list-style: none;
margin-bottom: 24px; }
margin-bottom: 16px; }
.cookie-consent .cookie-option {
width: auto;
margin-left: 0; }
margin-left: 0;
margin-bottom: 8px; }
.cookie-consent .cookie-option__label {
width: 100%;
margin: 0;
Expand All @@ -137,7 +136,7 @@
display: inline-block;
flex-shrink: 0;
position: relative;
top: 2px;
top: 0;
border: 2px solid white;
transition: all 0.3s; }
.cookie-consent .cookie-option__label:after {
Expand Down Expand Up @@ -166,6 +165,8 @@
.cookie-consent .anchor-down {
margin-left: 8px; }
@media only screen and (min-width: 600px) {
.cookie-consent .cookie-consent__explanation {
max-width: 70%; }
.cookie-consent .cookie-consent__lower__accept {
margin: 1em 0 0; }
.cookie-consent .cookie-consent__upper__controls {
Expand Down
13 changes: 7 additions & 6 deletions modules/mod_cookie_consent/lib/css/cookie_consent.scss
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@
z-index: 999999;
color: white;

.cookie-consent__explanation {
max-width: 70%;
}

.cookie-consent__upper,
.cookie-consent__lower {
padding-top: 50px;
Expand Down Expand Up @@ -165,12 +161,13 @@
display: flex;
flex-direction: column;
list-style: none;
margin-bottom: 24px;
margin-bottom: 16px;
}

.cookie-option {
width: auto;
margin-left: 0;
margin-bottom: 8px;
}

.cookie-option__label {
Expand Down Expand Up @@ -198,7 +195,7 @@
display: inline-block;
flex-shrink: 0;
position: relative;
top: 2px;
top: 0;
border: 2px solid white;
transition: all 0.3s;
}
Expand Down Expand Up @@ -243,6 +240,10 @@
}

@media only screen and (min-width: 600px) {
.cookie-consent__explanation {
max-width: 70%;
}

.cookie-consent__lower__accept {
margin: 1em 0 0;
}
Expand Down
26 changes: 15 additions & 11 deletions src/support/z_module_indexer.erl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
-include("zotonic.hrl").

-define(TIMEOUT, infinity).
-define(GC_TIMEOUT, 1000).

%%====================================================================
%% API
Expand Down Expand Up @@ -169,20 +170,20 @@ init(SiteProps) ->
%% {stop, Reason, Reply, State} |
%% {stop, Reason, State}
handle_call({find_all, scomp, Name, _Class}, _From, State) ->
{reply, lookup_all(Name, State#state.scomps), State};
{reply, lookup_all(Name, State#state.scomps), State, ?GC_TIMEOUT};
handle_call({find_all, action, Name, _Class}, _From, State) ->
{reply, lookup_all(Name, State#state.actions), State};
{reply, lookup_all(Name, State#state.actions), State, ?GC_TIMEOUT};
handle_call({find_all, validator, Name, _Class}, _From, State) ->
{reply, lookup_all(Name, State#state.validators), State};
{reply, lookup_all(Name, State#state.validators), State, ?GC_TIMEOUT};
handle_call({find_all, model, Name, _Class}, _From, State) ->
{reply, lookup_all(Name, State#state.models), State};
{reply, lookup_all(Name, State#state.models), State, ?GC_TIMEOUT};
handle_call({find_all, service, Name, _Class}, _From, State) ->
{reply, lookup_all(Name, State#state.services), State};
{reply, lookup_all(Name, State#state.services), State, ?GC_TIMEOUT};

handle_call({find_all, lib, File, _Class}, _From, State) ->
{reply, lookup_class_all(generic, File, State#state.lib), State};
{reply, lookup_class_all(generic, File, State#state.lib), State, ?GC_TIMEOUT};
handle_call({find_all, template, File, Class}, _From, State) ->
{reply, lookup_class_all(Class, File, State#state.templates), State};
{reply, lookup_class_all(Class, File, State#state.templates), State, ?GC_TIMEOUT};

%% @doc Trap unknown calls
handle_call(Message, _From, State) ->
Expand All @@ -205,7 +206,7 @@ handle_cast({module_ready, _NotifyContext}, #state{scanner_pid=undefined}=State)
gen_server:cast(Self, {scanned_items, Scanned})
end),

{noreply, State#state{scanner_pid=Pid}};
{noreply, State#state{scanner_pid=Pid}, ?GC_TIMEOUT};

handle_cast({module_ready, _NotifyContext}, #state{scanner_pid=Pid}=State) when is_pid(Pid) ->
%% The scanner is still busy, just let it continue.
Expand Down Expand Up @@ -233,9 +234,9 @@ handle_cast({scanned_items, Scanned}, State) ->
z_depcache:set(module_index_ref, erlang:make_ref(), NewState#state.context),
z_notifier:notify(module_reindexed, NewState#state.context),
z_depcache:flush(module_index, NewState#state.context),
{noreply, NewState};
{noreply, NewState, ?GC_TIMEOUT};
false ->
{noreply, State1}
{noreply, State1, ?GC_TIMEOUT}
end;

%% @doc Trap unknown casts
Expand All @@ -247,8 +248,11 @@ handle_cast(Message, State) ->
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% @doc Handling all non call/cast messages
handle_info(timeout, State) ->
erlang:garbage_collect(),
{noreply, State};
handle_info(_Info, State) ->
{noreply, State}.
{noreply, State, ?GC_TIMEOUT}.


%% @spec terminate(Reason, State) -> void()
Expand Down
25 changes: 16 additions & 9 deletions src/support/z_pivot_rsc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@
%% Max number of task retries
-define(TASK_MAX_RETRY, 10).

%% Forced garbages collect after 5 secs of inactivity
-define(GC_TIMEOUT, 5000).


-record(state, {
site,
Expand Down Expand Up @@ -305,11 +308,11 @@ handle_cast(poll, #state{is_initial_delay=true} = State) ->
handle_cast(poll, State) ->
try
do_poll(z_context:new(State#state.site)),
{noreply, State}
{noreply, State, ?GC_TIMEOUT}
catch
?WITH_STACKTRACE(Type, Err, Stack)
lager:error("Poll error ~p:~p, backing off pivoting. Stack: ~p", [ Type, Err, Stack ]),
{noreply, State#state{ backoff_counter = ?BACKOFF_POLL_ERROR }}
{noreply, State#state{ backoff_counter = ?BACKOFF_POLL_ERROR }, ?GC_TIMEOUT}
end;

%% @doc Insert an id into the queue.
Expand All @@ -321,17 +324,17 @@ handle_cast({insert_queue, Ids}, State) when is_list(Ids) ->
%% @doc Requests for immediate pivot of a resource
handle_cast({pivot, Id}, #state{ is_initial_delay = true } = State) ->
do_insert_queue([Id], z_context:new(State#state.site)),
{noreply, State};
{noreply, State, ?GC_TIMEOUT};
handle_cast({pivot, Id}, #state{ backoff_counter = Ct } = State) when Ct > 0 ->
do_insert_queue([Id], z_context:new(State#state.site)),
{noreply, State};
{noreply, State, ?GC_TIMEOUT};
handle_cast({pivot, Id}, State) ->
do_pivot(Id, z_context:new(State#state.site)),
{noreply, State};
{noreply, State, ?GC_TIMEOUT};

%% @doc Delay the next pivot, useful when performing big updates
handle_cast(pivot_delay, State) ->
{noreply, State#state{is_pivot_delay=true}};
{noreply, State#state{is_pivot_delay=true}, ?GC_TIMEOUT};

%% @doc Trap unknown casts
handle_cast(Message, State) ->
Expand All @@ -353,16 +356,20 @@ handle_info(poll, State) ->
true -> timer:send_after(?PIVOT_POLL_INTERVAL_FAST*1000, poll);
false -> timer:send_after(?PIVOT_POLL_INTERVAL_SLOW*1000, poll)
end,
{noreply, State#state{ is_initial_delay = false }}
{noreply, State#state{ is_initial_delay = false }, ?GC_TIMEOUT}
catch
?WITH_STACKTRACE(Type, Err, Stack)
lager:error("Pivot error ~p:~p, backing off pivoting. Stack: ~p", [ Type, Err, Stack ]),
timer:send_after(?PIVOT_POLL_INTERVAL_SLOW*1000, poll),
{noreply, State#state{ backoff_counter = ?BACKOFF_POLL_ERROR }}
{noreply, State#state{ backoff_counter = ?BACKOFF_POLL_ERROR }, ?GC_TIMEOUT}
end;

handle_info(timeout, State) ->
erlang:garbage_collect(),
{noreply, State};

handle_info(_Info, State) ->
{noreply, State}.
{noreply, State, ?GC_TIMEOUT}.

%% @spec terminate(Reason, State) -> void()
%% @doc This function is called by a gen_server when it is about to
Expand Down
2 changes: 2 additions & 0 deletions src/support/z_sites_dispatcher.erl
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ handle_cast(update_dispatchinfo, State) ->
do_update_hosts(FallbackSite),
NewRules = collect_dispatchrules(),
do_compile_modified(State#state.rules, NewRules),
erlang:garbage_collect(),
{noreply, State#state{
rules=NewRules,
fallback_site=FallbackSite
Expand All @@ -277,6 +278,7 @@ handle_cast(update_dispatchinfo, State) ->
handle_cast(update_hosts, State) ->
FallbackSite = z_sites_manager:get_fallback_site(),
do_update_hosts(FallbackSite),
erlang:garbage_collect(),
{noreply, State#state{fallback_site=FallbackSite}};

%% @doc Trap unknown casts
Expand Down
4 changes: 3 additions & 1 deletion src/support/z_sites_manager.erl
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,9 @@ handle_call(Message, _From, State) ->
%% {stop, Reason, State}
%% @doc Sync known sites with loaded sites
handle_cast(upgrade, State) ->
{noreply, handle_upgrade(State)};
State1 = handle_upgrade(State),
erlang:garbage_collect(),
{noreply, State1};

%% @doc Stop a site, assume it is a known site.
handle_cast({stop, Site}, State) ->
Expand Down

0 comments on commit 39047f2

Please sign in to comment.