Skip to content

Instantly share code, notes, and snippets.

View deitrix's full-sized avatar

Big Phil deitrix

View GitHub Profile
package main
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"net/http"
"os"
"path/filepath"
@deitrix
deitrix / template_test.go
Last active May 30, 2023 16:03
Test cases for Luna
package main
import (
"bytes"
"strings"
"testing"
"text/template"
)
type Data struct {
@deitrix
deitrix / script.lua
Last active May 23, 2023 20:36
Redis/Lua Leaky-bucket Rate Limit
-- Input parameters
local tokens_key = KEYS[1]..":tokens" -- Key for the bucket's token counter
local last_access_key = KEYS[1]..":last_access" -- Key for the bucket's last access time
local capacity = tonumber(ARGV[1]) -- Maximum number of tokens in the bucket
local rate = tonumber(ARGV[2]) -- Rate of token generation (tokens/second)
local now = tonumber(ARGV[3]) -- Current timestamp in microseconds
local requested = tonumber(ARGV[4]) -- Number of tokens requested for the operation
-- Fetch the current token count