summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Radestock <matthias@lshift.net>2009-01-20 08:35:16 +0000
committerMatthias Radestock <matthias@lshift.net>2009-01-20 08:35:16 +0000
commit7d904a367bf4b9eb8b7ac780bc07381b6190877b (patch)
tree67490baa898e4479d92ad9cd795249a67707edfd
parent55d51ed27ed18dc32d61168f0797c01f508ada78 (diff)
downloadrabbitmq-server-7d904a367bf4b9eb8b7ac780bc07381b6190877b.tar.gz
replace simple permission cache with lru cache, to make it bounded
-rw-r--r--src/rabbit_channel.erl42
1 files changed, 28 insertions, 14 deletions
diff --git a/src/rabbit_channel.erl b/src/rabbit_channel.erl
index ce061467..0ae3c186 100644
--- a/src/rabbit_channel.erl
+++ b/src/rabbit_channel.erl
@@ -48,6 +48,8 @@
-define(HIBERNATE_AFTER, 1000).
+-define(MAX_PERMISSION_CACHE_SIZE, 12).
+
%%----------------------------------------------------------------------------
-ifdef(use_specs).
@@ -202,28 +204,40 @@ return_queue_declare_ok(State, NoWait, Q) ->
{reply, Reply, NewState}
end.
-check_resource_access(Username, Resource, Perm, PermIndex) ->
- K = {resource_permission, Resource, Perm},
- %% TODO: we may want to make the cache bounded
- case get(K) of
- undefined -> R = rabbit_access_control:check_resource_access(
- Username, Resource, PermIndex),
- put(K, R),
- R;
- Other -> Other
+lru_cache_lookup(K, LookupFun, MaxSize, Cache) ->
+ case lists:keytake(K, 1, Cache) of
+ {value, E = {_, V}, Cache1} ->
+ {V, [E | Cache1]};
+ false ->
+ V = LookupFun(K),
+ {V, [{K, V} | lists:sublist(Cache, MaxSize - 1)]}
end.
+check_resource_access(Username, Resource, Perm) ->
+ Cache = case get(permission_cache) of
+ undefined -> [];
+ Other -> Other
+ end,
+ {Value, NewCache} =
+ lru_cache_lookup(
+ {Resource, Perm},
+ fun ({R, P}) -> rabbit_access_control:check_resource_access(
+ Username, R, P)
+ end,
+ ?MAX_PERMISSION_CACHE_SIZE,
+ Cache),
+ put(permission_cache, NewCache),
+ Value.
+
clear_permission_cache() ->
- [erase(R) || R = {resource_permission, _, _} <- get()],
+ erase(permission_cache),
ok.
check_configuration_permitted(Resource, #ch{ username = Username}) ->
- check_resource_access(Username, Resource, configuration,
- #permission.configuration).
+ check_resource_access(Username, Resource, #permission.configuration).
check_messaging_permitted(Resource, #ch{ username = Username}) ->
- check_resource_access(Username, Resource, messaging,
- #permission.messaging).
+ check_resource_access(Username, Resource, #permission.messaging).
expand_queue_name_shortcut(<<>>, #ch{ most_recently_declared_queue = <<>> }) ->
rabbit_misc:protocol_error(