Skip to content

Commit

Permalink
monitoring stuff and revised implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Sep 4, 2020
1 parent 60f888c commit 938fd3a
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 22 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/luacheck.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: luacheck

on: [push, pull_request]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: apt
run: sudo apt-get install -y luarocks
- name: luacheck install
run: luarocks install --local luacheck
- name: luacheck run
run: $HOME/.luarocks/bin/luacheck ./
19 changes: 19 additions & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

globals = {
"minetest",
"find_nodes_in_area_cache",
"jumpdrive"
}

read_globals = {
-- Stdlib
string = {fields = {"split"}},
table = {fields = {"copy", "getn"}},

-- Minetest
"vector", "ItemStack",
"dump", "VoxelArea",

-- deps
"monitoring"
}
63 changes: 42 additions & 21 deletions init.lua
Original file line number Diff line number Diff line change
@@ -1,32 +1,53 @@
local has_monitoring_mod = minetest.get_modpath("monitoring")

local old_check_player_privs = minetest.check_player_privs
local hit_count, miss_count, cache_size

if has_monitoring_mod then
hit_count = monitoring.counter("get_player_privs_cache_hit", "cache hits")
miss_count = monitoring.counter("get_player_privs_cache_miss", "cache misses")
cache_size = monitoring.gauge("get_player_privs_cache_size", "Count of all cached players")
end

local cache = {}

minetest.check_player_privs = function(player_or_name, ...)
local playername = player_or_name
if type(player_or_name) == "table" and player_or_name.get_player_name then
playername = player_or_name:get_player_name()
local old_get_player_privs = minetest.get_player_privs
minetest.get_player_privs = function(name)
local privs = cache[name]
if privs == nil then
if has_monitoring_mod then
miss_count.inc()
end
privs = old_get_player_privs(name)
cache[name] = privs
else
hit_count.inc()
end

local requested_privs = {...}
print(dump(requested_privs))
return privs
end

local result = cache[playername]
if result == nil then
-- cache miss
result = {true, {}}
result[1], result[2] = old_check_player_privs(player_or_name, unpack(requested_privs))
print("db hit", player_or_name, result[1], dump(result[2]))
cache[playername] = { result[1], result[2] }
end
-- invalidation on set_privs and leave-player

return result[1], result[2]
local old_set_player_privs = minetest.set_player_privs
minetest.set_player_privs = function(name, privs)
cache[name] = nil
old_set_player_privs(name, privs);
end

local function invalidate()
cache = {}
minetest.after(2, invalidate)
end
minetest.register_on_leaveplayer(function(player)
cache[player:get_player_name()] = nil
end)

-- monitoring stuff
if has_monitoring_mod then
local function count_entries()
local count = 0
for _ in ipairs(cache) do
count = count + 1
end
cache_size.set(count)
minetest.after(5, count_entries)
end

minetest.after(2, invalidate)
minetest.after(5, count_entries)
end
2 changes: 1 addition & 1 deletion mod.conf
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
name = check_player_privs_cache
name = get_player_privs_cache
optional_depends = monitoring

0 comments on commit 938fd3a

Please sign in to comment.