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
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='...',
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']:
package main
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
"log"
"time"
)
package main
import (
"context"
"errors"
"fmt"
"github.com/redis/go-redis/v9"
"sync"
"time"
)
@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)
//go:embed gcra.lua
var gcraScript string
type RateLimit struct {
rdb *redis.Client
prefix string
gcra *redis.Script
timeout time.Duration
}
@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)
}
/*
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
func validateInitData(inputData, botToken string) (bool, error) {
initData, err := url.ParseQuery(inputData)
if err != nil {
logrus.WithError(err).Errorln("couldn't parse web app input data")
return false, err
}
dataCheckString := make([]string, 0, len(initData))
for k, v := range initData {
if k == "hash" {