Skip to content

Commit

Permalink
Implemented suggested improvements regarding ETS state
Browse files Browse the repository at this point in the history
  • Loading branch information
mtvch committed Dec 13, 2024
1 parent d249e5f commit c51ee13
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/brod_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,8 @@ is_cooled_down(Ts, #state{config = Config}) ->
maybe_cache_unknown_topic_partition(Ets, Topic, TopicMetadataArray, UnknownTopicCacheTtl) ->
case find_partition_count_in_topic_metadata_array(TopicMetadataArray, Topic) of
{error, unknown_topic_or_partition} = Err ->
_ = ets:insert(Ets, {?TOPIC_METADATA_KEY(Topic), Err, expire_ts(UnknownTopicCacheTtl)}),
_ = ets:insert(Ets, {?TOPIC_METADATA_KEY(Topic), Err,
{expire, expire_ts(UnknownTopicCacheTtl)}}),
ok;
_ ->
%% do nothing when ok or any other error
Expand All @@ -838,9 +839,9 @@ update_partitions_count_cache(Ets, [TopicMetadata | Rest], UnknownTopicCacheTtl)
Topic = kf(name, TopicMetadata),
case get_partitions_count_in_metadata(TopicMetadata) of
{ok, Cnt} ->
ets:insert(Ets, {?TOPIC_METADATA_KEY(Topic), Cnt, nil});
ets:insert(Ets, {?TOPIC_METADATA_KEY(Topic), Cnt, {added, now_ts()}});
{error, ?unknown_topic_or_partition} = Err ->
ets:insert(Ets, {?TOPIC_METADATA_KEY(Topic), Err, expire_ts(UnknownTopicCacheTtl)});
ets:insert(Ets, {?TOPIC_METADATA_KEY(Topic), Err, {expire, expire_ts(UnknownTopicCacheTtl)}});
{error, _Reason} ->
ok
end,
Expand Down Expand Up @@ -990,10 +991,13 @@ safe_gen_call(Server, Call, Timeout) ->
kf(FieldName, Struct) -> kpro:find(FieldName, Struct).

-spec expire_ts(integer()) -> integer().
expire_ts(Ttl) -> erlang:monotonic_time(millisecond) + Ttl.
expire_ts(Ttl) -> now_ts() + Ttl.

-spec is_expired(integer()) -> boolean().
is_expired(ExpireTs) -> erlang:monotonic_time(millisecond) - ExpireTs < 0.
is_expired(ExpireTs) -> now_ts() - ExpireTs < 0.

-spec now_ts() -> integer().
now_ts() -> erlang:monotonic_time(millisecond).

%%%_* Emacs ====================================================================
%%% Local Variables:
Expand Down

0 comments on commit c51ee13

Please sign in to comment.