-
Notifications
You must be signed in to change notification settings - Fork 38
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
Changes from all commits
52b3d69
7dc0257
4a88cb0
6c7bfd9
fe3bd93
04120d1
0438fe4
404f7e0
c641feb
027d49f
250b0d7
8bc2379
484ca6d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
%%=================================================================== | ||
|
@@ -46,8 +47,17 @@ send_events(Tag, Events, SummaryEvent) -> | |
|
||
-spec get_last_server_time(Tag :: atom()) -> integer(). | ||
get_last_server_time(Tag) -> | ||
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 | ||
|
@@ -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}]), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This theoretically should be a read-heavy operation. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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), | ||
|
@@ -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}. | ||
|
||
|
@@ -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}. | ||
|
||
|
@@ -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), | ||
|
@@ -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; | ||
|
@@ -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)). |
There was a problem hiding this comment.
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.