diff options
author | Matthias Radestock <matthias@lshift.net> | 2009-01-20 08:35:16 +0000 |
---|---|---|
committer | Matthias Radestock <matthias@lshift.net> | 2009-01-20 08:35:16 +0000 |
commit | 7d904a367bf4b9eb8b7ac780bc07381b6190877b (patch) | |
tree | 67490baa898e4479d92ad9cd795249a67707edfd /src/rabbit_channel.erl | |
parent | 55d51ed27ed18dc32d61168f0797c01f508ada78 (diff) | |
download | rabbitmq-server-7d904a367bf4b9eb8b7ac780bc07381b6190877b.tar.gz |
replace simple permission cache with lru cache, to make it bounded
Diffstat (limited to 'src/rabbit_channel.erl')
-rw-r--r-- | src/rabbit_channel.erl | 42 |
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( |