Skip to content

Commit

Permalink
tarantool: add index for cleansing fiber
Browse files Browse the repository at this point in the history
  • Loading branch information
lowitea committed Aug 31, 2024
1 parent 09eaf3d commit 27673cf
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions hitbox-tarantool/src/init.lua
Original file line number Diff line number Diff line change
@@ -1,35 +1,51 @@
local fiber = require("fiber")
local log = require("log")

local SCAN_INTERVAL = 0.1
local MAX_TUPLES_FOR_DELETE = 1000

box.cfg({})

local space_name = ...

box.schema.space.create(space_name, { if_not_exists = true })
box.space[space_name]:create_index("primary", {
type = 'HASH',
type = "HASH",
parts = { { 1, "string" } },
if_not_exists = true,
})
box.space[space_name]:create_index("by_ttl", {
parts = { { 2, "integer" } },
if_not_exists = true,
})

if not _G.__hitbox_cache_fiber then
_G.__hitbox_cache_fiber = fiber.create(function()
fiber.name("hitbox_cache_fiber")
while true do
box.ctl.wait_rw()

local ok, err = pcall(function()
for _, t in box.space[space_name]:pairs() do
if t[2] <= fiber.time() then
local for_del = box.space[space_name].index.by_ttl
:pairs({ math.floor(fiber.time()) }, { iterator = "LE" })
:take(MAX_TUPLES_FOR_DELETE)
:totable()

box.atomic(function()
for _, t in pairs(for_del) do
box.space[space_name]:delete(t[1])
end
end
end)

return true
end)

if not ok then
log.error(err)
end

fiber.testcancel()
fiber.sleep(1)
fiber.sleep(SCAN_INTERVAL)
end
end)
end
Expand Down

0 comments on commit 27673cf

Please sign in to comment.