Skip to content

Commit

Permalink
Update ratelimit
Browse files Browse the repository at this point in the history
* Added ratelimit docs
* Added tests
  • Loading branch information
gaymeowing committed Jul 27, 2024
1 parent 9a45695 commit 193a260
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 3 deletions.
89 changes: 87 additions & 2 deletions docs/ratelimit.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,89 @@
# Ratelimit

> [!NOTE]
> The docs for this library are incomplete
Object for handling ratelimits intuitively, can be used without a Ratelimits many keys in a very intuitive interface. Ratelimits can also be used without any keys

```luau
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ratelmit = require("ratelimit")
local check_limit = ratelimit(10, 60)
local event = Instance.new("RemoteEvent")
event.Parent = ReplicatedStorage
event.OnClientEvent:Connect(function(player, ...)
if check_limit(player) then
-- do stuff
end
end)
```

## Type

```luau
type Ratelimit<K> = {
@metatable {
__call: (self: Ratelimit<K>, key: K?) -> boolean
}
count_map: { [K]: number },
interval: number,
limit: number,
count: number,
}
```

## Constructor

Creates a new ratelimit object, with [`limit`](#limit) being the amount of times a key can be called every [`interval`](#interval) seconds.

> [!TIP]
> The limit is inclusive, use the maximum amount of calls you want to allow as the [`limit`](#limit).
```luau
local check_limit = ratelimit(10, 60)
```

## Methods

### `__call`

Checks if a given key's count or [`count`](#count) has exceeded the [`limit`](#limit) within the current [`interval`](#interval)

```luau
local check_limit = ratelimit(10, 60)
check_limit("meow")
```

## Properties

### `count`

The count for when the ratelimit object has its [`__call`](#__call) metamethod invoked without the key argument

```luau
type count = number
```

### `count_map`

A map with keys, with their values being the count for the specified key

```luau
type count_map<K> = { [K]: number }
```

### `limit`

The limit for the ratelimit object

```luau
type limit = number
```

### `interval`

The interval for the ratelimit object

```luau
type interval = number
```
12 changes: 11 additions & 1 deletion libs/Ratelimit/ratelimit.luau
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,17 @@ export type Ratelimit<K> = typeof(setmetatable({} :: {
}, {} :: RatelimitPrototype<K>))

local ratelimit_mt = ({} :: any) :: RatelimitPrototype<any>
local delay = task.delay
local delay = (function()
if task then
return task.delay
elseif string.find(_VERSION, "lune") then
return (require)("@lune/task").delay
else
return function()

end :: any
end
end)()

function ratelimit_mt.__call(ratelimit, key)
if key then
Expand Down
29 changes: 29 additions & 0 deletions libs/Ratelimit/ratelimit.test.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
local ratelimit = require("ratelimit")
local testkit = require("../testkit")

local TEST, CASE, CHECK, FINISH = testkit.test()

TEST("ratelimit", function()
do CASE "create ratelimit"
local check_limit = ratelimit(10, 60)

check_limit("meow")
CHECK(testkit.deq(check_limit :: any, {
count_map = { meow = 1 },
interval = 60,
limit = 10,
count = 0,
}))
end

do CASE "will get throttled"
local check_limit = ratelimit(10, 60)

for index = 1, 10 do
check_limit("meow")
end
CHECK(check_limit("meow") == false)
end
end)

if not FINISH() then error(nil, 0) end

0 comments on commit 193a260

Please sign in to comment.