Skip to content

Commit

Permalink
lru: fix wrong object insert in push function
Browse files Browse the repository at this point in the history
Also, make sure a expected object is indeed present when trying
to remove it. This catches inconsistencies between queue and index.
  • Loading branch information
RoadRunnr authored and vk committed Oct 29, 2021
1 parent 7b89e31 commit 0bc62a1
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions apps/ergw_core/src/lru.erl
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ to_list(#lru{queue = Queue}) ->
pop(#lru{queue = Queue, index = Index}) ->
case ets:first(Queue) of
{_, Key} = Entry ->
ets:delete(Queue, Entry),
ets:delete(Index, Key),
[_] = ets:take(Queue, Entry),
[_] = ets:take(Index, Key),
{ok, Key};
'$end_of_table' ->
{error, empty}
end.

push(Key, #lru{queue = Queue, index = Index}) ->
Now = erlang:monotonic_time(),
case ets:insert_new(Index, {Key, Now}) of
Entry = {erlang:monotonic_time(), Key},
case ets:insert_new(Index, {Key, Entry}) of
true ->
ets:insert(Queue, {{Now, Key}}),
ets:insert(Queue, {Entry}),
ok;
false ->
erlang:error(badarg, [Key])
Expand All @@ -53,7 +53,7 @@ push(Key, #lru{queue = Queue, index = Index}) ->
take(Key, #lru{queue = Queue, index = Index}) ->
case ets:take(Index, Key) of
[{_, QEntry}] ->
ets:delete(Queue, QEntry),
[_] = ets:take(Queue, QEntry),
ok;
_ ->
{error, not_found}
Expand Down

0 comments on commit 0bc62a1

Please sign in to comment.