optional rate limit

This commit is contained in:
divyam234 2023-08-20 02:48:04 +05:30
parent 3e6f19e58f
commit aeb0434356
3 changed files with 24 additions and 18 deletions

View file

@ -85,6 +85,7 @@ An example of `.env` file:
APP_ID=1234
APP_HASH=abc
CHANNEL_ID=1234
RATE_LIMIT=true
HTTPS=false
COOKIE_SAME_SITE=true
JWT_SECRET=abc
@ -125,7 +126,7 @@ Before running the bot, you will need to set up the following mandatory variable
### Optional Vars
In addition to the mandatory variables, you can also set the following optional variables:
- `RATE_LIMIT` : Rate Limit Calls to prevent flood errors
- `HTTPS` : Only needed when frontend is deployed on vercel.
- `COOKIE_SAME_SITE` : Only needed when frontend is deployed on vercel.

View file

@ -15,6 +15,7 @@ type Config struct {
Https bool `envconfig:"HTTPS" default:"false"`
CookieSameSite bool `envconfig:"COOKIE_SAME_SITE" default:"true"`
DatabaseUrl string `envconfig:"DATABASE_URL" required:"true"`
RateLimit bool `envconfig:"RATE_LIMIT" default:"true"`
TgClientDeviceModel string `envconfig:"TG_CLIENT_DEVICE_MODEL" required:"true"`
TgClientSystemVersion string `envconfig:"TG_CLIENT_SYSTEM_VERSION" default:"Win32"`
TgClientAppVersion string `envconfig:"TG_CLIENT_APP_VERSION" default:"2.1.9 K"`

View file

@ -43,13 +43,15 @@ func getBotClient(appID int, appHash, clientName, sessionDir string) *telegram.C
sessionStorage := &telegram.FileSessionStorage{
Path: filepath.Join(sessionDir, clientName+".json"),
}
middlewares := []telegram.Middleware{}
if config.RateLimit {
middlewares = append(middlewares, ratelimit.New(rate.Every(time.Millisecond*100), 5))
}
options := telegram.Options{
SessionStorage: sessionStorage,
Middlewares: []telegram.Middleware{
ratelimit.New(rate.Every(time.Millisecond*100), 5),
},
Device: getDeviceConfig(),
NoUpdates: true,
Middlewares: middlewares,
Device: getDeviceConfig(),
NoUpdates: true,
}
client := telegram.NewClient(appID, appHash, options)
@ -139,14 +141,15 @@ func GetAuthClient(sessionStr string, userId int) (*Client, bg.StopFunc, error)
if err := loader.Save(ctx, data); err != nil {
return nil, nil, err
}
middlewares := []telegram.Middleware{}
if config.RateLimit {
middlewares = append(middlewares, ratelimit.New(rate.Every(time.Millisecond*100), 5))
}
client := telegram.NewClient(config.AppId, config.AppHash, telegram.Options{
SessionStorage: storage,
Middlewares: []telegram.Middleware{
ratelimit.New(rate.Every(time.Millisecond*100), 5),
},
Device: getDeviceConfig(),
NoUpdates: true,
Middlewares: middlewares,
Device: getDeviceConfig(),
NoUpdates: true,
})
stop, err := bg.Connect(client)
@ -181,14 +184,15 @@ func GetBotClient() *Client {
}
func GetNonAuthClient(handler telegram.UpdateHandler, storage telegram.SessionStorage) (*telegram.Client, bg.StopFunc, error) {
middlewares := []telegram.Middleware{}
if config.RateLimit {
middlewares = append(middlewares, ratelimit.New(rate.Every(time.Millisecond*100), 5))
}
client := telegram.NewClient(config.AppId, config.AppHash, telegram.Options{
SessionStorage: storage,
Middlewares: []telegram.Middleware{
ratelimit.New(rate.Every(time.Millisecond*100), 5),
},
Device: getDeviceConfig(),
UpdateHandler: handler,
Middlewares: middlewares,
Device: getDeviceConfig(),
UpdateHandler: handler,
})
stop, err := bg.Connect(client)