Fix segmented cache expiration timeouts.

This commit is contained in:
Daniil Fedotov 2017-09-20 15:56:52 +01:00
parent 3455c72447
commit 0c6ac7e491
2 changed files with 10 additions and 10 deletions

View File

@ -65,8 +65,7 @@ init([SegmentSize]) ->
handle_call({get_write_segment, Expiration}, _From,
State = #state{segments = Segments,
segment_size = SegmentSize}) ->
NewBoundary = new_boundary(Expiration, SegmentSize),
[{_, Segment} | _] = NewSegments = maybe_add_segment(NewBoundary, Segments),
[{_, Segment} | _] = NewSegments = maybe_add_segment(Expiration, SegmentSize, Segments),
{reply, Segment, State#state{segments = NewSegments}};
handle_call(get_segment_tables, _From, State = #state{segments = Segments}) ->
{_, Valid} = partition_expired_segments(Segments),
@ -95,18 +94,16 @@ partition_expired_segments(Segments) ->
fun({Boundary, _}) -> rabbit_auth_cache:expired(Boundary) end,
Segments).
maybe_add_segment(Boundary, OldSegments) ->
maybe_add_segment(Expiration, SegmentSize, OldSegments) ->
case OldSegments of
[{OldBoundary, _}|_] when OldBoundary > Boundary ->
[{OldBoundary, _}|_] when OldBoundary > Expiration ->
OldSegments;
_ ->
NewBoundary = Expiration + SegmentSize,
Segment = ets:new(segment, [set, public]),
[{Boundary, Segment} | OldSegments]
[{NewBoundary, Segment} | OldSegments]
end.
new_boundary(Expiration, SegmentSize) ->
((Expiration div SegmentSize) * SegmentSize) + SegmentSize.
get_from_segments(Key) ->
Tables = gen_server2:call(?MODULE, get_segment_tables),
lists:flatmap(

View File

@ -164,8 +164,11 @@ random_timing(Config, MaxTTL, Parallel) ->
Value = {tuple_with, N, TTL},
{error, not_found} = AuthCacheModule:get(Key),
ok = AuthCacheModule:put(Key, Value, TTL),
{ok, Value} = AuthCacheModule:get(Key),
% 20ms expiry error
case AuthCacheModule:get(Key) of
{ok, Value} -> ok;
Other -> error({Other, Value})
end,
% expiry error
timer:sleep(TTL + 40),
{error, not_found} = AuthCacheModule:get(Key),
Pid ! {ok, self(), Ref}