27
27
global_private_attributes := ldclient_config :private_attributes (),
28
28
events_uri := string (),
29
29
tag := atom (),
30
- dispatcher_state := any (),
31
- last_server_time := integer ()
30
+ dispatcher_state := any ()
32
31
}.
33
32
33
+ -define (TABLE_PREFIX , " event_process_state" ).
34
+
34
35
% %===================================================================
35
36
% % API
36
37
% %===================================================================
@@ -46,8 +47,17 @@ send_events(Tag, Events, SummaryEvent) ->
46
47
47
48
-spec get_last_server_time (Tag :: atom ()) -> integer ().
48
49
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
+
51
61
52
62
% %===================================================================
53
63
% % Supervision
@@ -67,6 +77,7 @@ start_link(Tag) ->
67
77
{ok , State :: state ()} | {ok , State :: state (), timeout () | hibernate } |
68
78
{stop , Reason :: term ()} | ignore .
69
79
init ([Tag ]) ->
80
+ _Tid = ets :new (ets_table_name (Tag ), [set , named_table , {read_concurrency , true }]),
70
81
SdkKey = ldclient_config :get_value (Tag , sdk_key ),
71
82
Dispatcher = ldclient_config :get_value (Tag , events_dispatcher ),
72
83
GlobalPrivateAttributes = ldclient_config :get_value (Tag , private_attributes ),
@@ -77,8 +88,7 @@ init([Tag]) ->
77
88
global_private_attributes => GlobalPrivateAttributes ,
78
89
events_uri => EventsUri ,
79
90
tag => Tag ,
80
- dispatcher_state => Dispatcher :init (Tag , SdkKey ),
81
- last_server_time => 0
91
+ dispatcher_state => Dispatcher :init (Tag , SdkKey )
82
92
},
83
93
{ok , State }.
84
94
@@ -90,8 +100,6 @@ init([Tag]) ->
90
100
-spec handle_call (Request :: term (), From :: from (), State :: state ()) ->
91
101
{reply , Reply :: term (), NewState :: state ()} |
92
102
{stop , normal , {error , atom (), term ()}, state ()}.
93
- handle_call ({get_last_server_time }, _From , #{last_server_time := LastServerTime } = State ) ->
94
- {reply , LastServerTime , State };
95
103
handle_call (_Request , _From , State ) ->
96
104
{reply , ok , State }.
97
105
@@ -101,7 +109,8 @@ handle_cast({send_events, Events, SummaryEvent},
101
109
dispatcher := Dispatcher ,
102
110
global_private_attributes := GlobalPrivateAttributes ,
103
111
events_uri := Uri ,
104
- dispatcher_state := DispatcherState
112
+ dispatcher_state := DispatcherState ,
113
+ tag := Tag
105
114
} = State ) ->
106
115
FormattedSummaryEvent = format_summary_event (SummaryEvent ),
107
116
FormattedEvents = format_events (Events , GlobalPrivateAttributes ),
@@ -111,7 +120,8 @@ handle_cast({send_events, Events, SummaryEvent},
111
120
ok ->
112
121
State ;
113
122
{ok , Date } ->
114
- State #{last_server_time => Date };
123
+ ets :insert (ets_table_name (Tag ), {last_known_server_time , Date }),
124
+ State ;
115
125
{error , temporary , _Reason } ->
116
126
erlang :send_after (1000 , self (), {send , OutputEvents , PayloadId }),
117
127
State ;
@@ -321,3 +331,6 @@ send(Dispatcher, DispatcherState, OutputEvents, PayloadId, Uri) ->
321
331
-spec get_local_reg_name (Tag :: atom ()) -> atom ().
322
332
get_local_reg_name (Tag ) ->
323
333
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