Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use ets for last-known server-time. #136

Merged
merged 13 commits into from
Oct 22, 2024
33 changes: 23 additions & 10 deletions src/ldclient_event_process_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
global_private_attributes := ldclient_config:private_attributes(),
events_uri := string(),
tag := atom(),
dispatcher_state := any(),
last_server_time := integer()
dispatcher_state := any()
}.

-define(TABLE_PREFIX, "event_process_state").

%%===================================================================
%% API
%%===================================================================
Expand All @@ -46,8 +47,17 @@ send_events(Tag, Events, SummaryEvent) ->

-spec get_last_server_time(Tag :: atom()) -> integer().
get_last_server_time(Tag) ->
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of calling into the process this now looks up a value from ets.

ServerName = get_local_reg_name(Tag),
gen_server:call(ServerName, {get_last_server_time}).
TableName = ets_table_name(Tag),
case ets:info(TableName) of
undefined ->
0;
_ ->
case ets:lookup(TableName, last_known_server_time) of
[] -> 0;
[{last_known_server_time, LastKnownServerTime}] -> LastKnownServerTime
end
end.


%%===================================================================
%% Supervision
Expand All @@ -67,6 +77,7 @@ start_link(Tag) ->
{ok, State :: state()} | {ok, State :: state(), timeout() | hibernate} |
{stop, Reason :: term()} | ignore.
init([Tag]) ->
_Tid = ets:new(ets_table_name(Tag), [set, named_table, {read_concurrency, true}]),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This theoretically should be a read-heavy operation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this data were large, then write_concurrency would probably be reasonable, but it is very small.

SdkKey = ldclient_config:get_value(Tag, sdk_key),
Dispatcher = ldclient_config:get_value(Tag, events_dispatcher),
GlobalPrivateAttributes = ldclient_config:get_value(Tag, private_attributes),
Expand All @@ -77,8 +88,7 @@ init([Tag]) ->
global_private_attributes => GlobalPrivateAttributes,
events_uri => EventsUri,
tag => Tag,
dispatcher_state => Dispatcher:init(Tag, SdkKey),
last_server_time => 0
dispatcher_state => Dispatcher:init(Tag, SdkKey)
},
{ok, State}.

Expand All @@ -90,8 +100,6 @@ init([Tag]) ->
-spec handle_call(Request :: term(), From :: from(), State :: state()) ->
{reply, Reply :: term(), NewState :: state()} |
{stop, normal, {error, atom(), term()}, state()}.
handle_call({get_last_server_time}, _From, #{last_server_time := LastServerTime} = State) ->
{reply, LastServerTime, State};
handle_call(_Request, _From, State) ->
{reply, ok, State}.

Expand All @@ -101,7 +109,8 @@ handle_cast({send_events, Events, SummaryEvent},
dispatcher := Dispatcher,
global_private_attributes := GlobalPrivateAttributes,
events_uri := Uri,
dispatcher_state := DispatcherState
dispatcher_state := DispatcherState,
tag := Tag
} = State) ->
FormattedSummaryEvent = format_summary_event(SummaryEvent),
FormattedEvents = format_events(Events, GlobalPrivateAttributes),
Expand All @@ -111,7 +120,8 @@ handle_cast({send_events, Events, SummaryEvent},
ok ->
State;
{ok, Date} ->
State#{last_server_time => Date};
ets:insert(ets_table_name(Tag), {last_known_server_time, Date}),
State;
{error, temporary, _Reason} ->
erlang:send_after(1000, self(), {send, OutputEvents, PayloadId}),
State;
Expand Down Expand Up @@ -321,3 +331,6 @@ send(Dispatcher, DispatcherState, OutputEvents, PayloadId, Uri) ->
-spec get_local_reg_name(Tag :: atom()) -> atom().
get_local_reg_name(Tag) ->
list_to_atom("ldclient_event_process_server_" ++ atom_to_list(Tag)).

-spec ets_table_name(Tag :: atom()) -> atom().
ets_table_name(Tag) -> list_to_atom(?TABLE_PREFIX ++ atom_to_list(Tag)).
Loading