Skip to content

Instantly share code, notes, and snippets.

View mhrlife's full-sized avatar
😁
Learning Go

Mohammad hoseini rad mhrlife

😁
Learning Go
View GitHub Profile
/*
create a file and name it: food
meat kebab 5
meat stake 5
meat ice-cream 0
meat baklava 0
meat tea 0
meat coffee 0
meat burger 4
meat hot-dog 2
@mhrlife
mhrlife / render.go
Created January 1, 2024 16:55
Render templ component in Go-Echo
func Render(c echo.Context, comp templ.Component) error {
c.Response().Header().Set(echo.HeaderContentType, echo.MIMETextHTML)
return comp.Render(c.Request().Context(), c.Response().Writer)
}
//go:embed gcra.lua
var gcraScript string
type RateLimit struct {
rdb *redis.Client
prefix string
gcra *redis.Script
timeout time.Duration
}
@mhrlife
mhrlife / gcra.lua
Created November 7, 2023 16:09
GCRA Rate limiter implementation with Lua
redis.replicate_commands()
local rate_limit_key = KEYS[1]
local burst = ARGV[1]
local emission_interval = tonumber(ARGV[2])
-- calculating time using this idea (https://github.com/rwz/redis-gcra/blob/master/vendor/perform_gcra_ratelimit.lua)
local jan_1_2017 = 1483228800
local now = redis.call("TIME")
now = (now[1] - jan_1_2017) + (now[2] / 1000000)
package main
import (
"context"
"errors"
"fmt"
"github.com/redis/go-redis/v9"
"sync"
"time"
)
package main
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
"log"
"time"
)
import asyncio
import time
import aiohttp
import requests
from typing import List, Callable
def get_user_info(user_id: int) -> dict | None:
response = requests.get(f"http://127.0.0.1:8000/user/{user_id}").json()
if 'ok' not in response or not response['ok']:
import asyncio
from fastapi import FastAPI, Depends
from starlette.websockets import WebSocket
import redis.asyncio as redis
app = FastAPI()
redis_connection_pool = redis.ConnectionPool(
host='...',