Skip to content

Commit 568ac51

Browse files
authored
feat: Use ets for last-known server-time. (#136)
Addresses main bottleneck of #132.
1 parent f487151 commit 568ac51

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

src/ldclient_event_process_server.erl

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@
2727
global_private_attributes := ldclient_config:private_attributes(),
2828
events_uri := string(),
2929
tag := atom(),
30-
dispatcher_state := any(),
31-
last_server_time := integer()
30+
dispatcher_state := any()
3231
}.
3332

33+
-define(TABLE_PREFIX, "event_process_state").
34+
3435
%%===================================================================
3536
%% API
3637
%%===================================================================
@@ -46,8 +47,17 @@ send_events(Tag, Events, SummaryEvent) ->
4647

4748
-spec get_last_server_time(Tag :: atom()) -> integer().
4849
get_last_server_time(Tag) ->
49-
ServerName = get_local_reg_name(Tag),
50-
gen_server:call(ServerName, {get_last_server_time}).
50+
TableName = ets_table_name(Tag),
51+
case ets:info(TableName) of
52+
undefined ->
53+
0;
54+
_ ->
55+
case ets:lookup(TableName, last_known_server_time) of
56+
[] -> 0;
57+
[{last_known_server_time, LastKnownServerTime}] -> LastKnownServerTime
58+
end
59+
end.
60+
5161

5262
%%===================================================================
5363
%% Supervision
@@ -67,6 +77,7 @@ start_link(Tag) ->
6777
{ok, State :: state()} | {ok, State :: state(), timeout() | hibernate} |
6878
{stop, Reason :: term()} | ignore.
6979
init([Tag]) ->
80+
_Tid = ets:new(ets_table_name(Tag), [set, named_table, {read_concurrency, true}]),
7081
SdkKey = ldclient_config:get_value(Tag, sdk_key),
7182
Dispatcher = ldclient_config:get_value(Tag, events_dispatcher),
7283
GlobalPrivateAttributes = ldclient_config:get_value(Tag, private_attributes),
@@ -77,8 +88,7 @@ init([Tag]) ->
7788
global_private_attributes => GlobalPrivateAttributes,
7889
events_uri => EventsUri,
7990
tag => Tag,
80-
dispatcher_state => Dispatcher:init(Tag, SdkKey),
81-
last_server_time => 0
91+
dispatcher_state => Dispatcher:init(Tag, SdkKey)
8292
},
8393
{ok, State}.
8494

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

@@ -101,7 +109,8 @@ handle_cast({send_events, Events, SummaryEvent},
101109
dispatcher := Dispatcher,
102110
global_private_attributes := GlobalPrivateAttributes,
103111
events_uri := Uri,
104-
dispatcher_state := DispatcherState
112+
dispatcher_state := DispatcherState,
113+
tag := Tag
105114
} = State) ->
106115
FormattedSummaryEvent = format_summary_event(SummaryEvent),
107116
FormattedEvents = format_events(Events, GlobalPrivateAttributes),
@@ -111,7 +120,8 @@ handle_cast({send_events, Events, SummaryEvent},
111120
ok ->
112121
State;
113122
{ok, Date} ->
114-
State#{last_server_time => Date};
123+
ets:insert(ets_table_name(Tag), {last_known_server_time, Date}),
124+
State;
115125
{error, temporary, _Reason} ->
116126
erlang:send_after(1000, self(), {send, OutputEvents, PayloadId}),
117127
State;
@@ -321,3 +331,6 @@ send(Dispatcher, DispatcherState, OutputEvents, PayloadId, Uri) ->
321331
-spec get_local_reg_name(Tag :: atom()) -> atom().
322332
get_local_reg_name(Tag) ->
323333
list_to_atom("ldclient_event_process_server_" ++ atom_to_list(Tag)).
334+
335+
-spec ets_table_name(Tag :: atom()) -> atom().
336+
ets_table_name(Tag) -> list_to_atom(?TABLE_PREFIX ++ atom_to_list(Tag)).

0 commit comments

Comments
 (0)