handle flood errors and changed reconnection login

This commit is contained in:
divyam234 2023-08-20 22:05:12 +05:30
parent e74082984f
commit 4d00404ca0
7 changed files with 40 additions and 24 deletions

View file

@ -126,7 +126,6 @@ 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

@ -55,7 +55,9 @@ func InitDB() {
sqlDB.SetConnMaxLifetime(time.Hour)
go func() {
DB.Exec(`create collation if not exists numeric (provider = icu, locale = 'en@colnumeric=yes');`)
migrate()
if utils.GetConfig().RunMigrations {
migrate()
}
}()
}

4
go.mod
View file

@ -2,8 +2,6 @@ module github.com/divyam234/teldrive
go 1.21
toolchain go1.21.0
require (
github.com/allegro/bigcache/v3 v3.1.0
github.com/divyam234/cors v1.4.2
@ -28,7 +26,7 @@ require github.com/robfig/cron/v3 v3.0.1 // indirect
require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.1
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect

View file

@ -1,6 +1,7 @@
package main
import (
"fmt"
"time"
"github.com/divyam234/teldrive/cache"
@ -56,9 +57,9 @@ func main() {
ok, _ := utils.PathExists("./sslcerts")
config := utils.GetConfig()
if ok && config.Https {
router.RunTLS(":8080", "./sslcerts/cert.pem", "./sslcerts/key.pem")
router.RunTLS(fmt.Sprintf(":%d", config.Port), "./sslcerts/cert.pem", "./sslcerts/key.pem")
} else {
router.Run(":8080")
router.Run(fmt.Sprintf(":%d", config.Port))
}
scheduler.StartAsync()
}

View file

@ -94,7 +94,7 @@ func (us *UploadService) UploadFile(c *gin.Context) (*schemas.UploadPartOut, *ty
api := tgClient.Tg.API()
u := uploader.NewUploader(api).WithThreads(16).WithPartSize(512 * 1024)
u := uploader.NewUploader(api).WithThreads(8).WithPartSize(512 * 1024)
sender := message.NewSender(api).WithUploader(u)

View file

@ -22,6 +22,8 @@ type Config struct {
TgClientLangCode string `envconfig:"TG_CLIENT_LANG_CODE" default:"en"`
TgClientSystemLangCode string `envconfig:"TG_CLIENT_SYSTEM_LANG_CODE" default:"en"`
TgClientLangPack string `envconfig:"TG_CLIENT_LANG_PACK" default:"webk"`
RunMigrations bool `envconfig:"RUN_MIGRATIONS" default:"true"`
Port int `envconfig:"PORT" default:"8080"`
}
var config Config

View file

@ -9,8 +9,11 @@ import (
"strings"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/gotd/contrib/bg"
"github.com/gotd/contrib/middleware/floodwait"
"github.com/gotd/contrib/middleware/ratelimit"
tdclock "github.com/gotd/td/clock"
"github.com/gotd/td/session"
"github.com/gotd/td/telegram"
"github.com/pkg/errors"
@ -38,20 +41,31 @@ func getDeviceConfig() telegram.DeviceConfig {
}
return config
}
func reconnectionBackoff() backoff.BackOff {
_clock := tdclock.System
b := backoff.NewExponentialBackOff()
b.Multiplier = 1.1
b.MaxElapsedTime = time.Duration(120) * time.Second
b.Clock = _clock
return b
}
func getBotClient(appID int, appHash, clientName, sessionDir string) *telegram.Client {
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))
}
middlewares := []telegram.Middleware{floodwait.NewSimpleWaiter()}
options := telegram.Options{
SessionStorage: sessionStorage,
Middlewares: middlewares,
Device: getDeviceConfig(),
NoUpdates: true,
SessionStorage: sessionStorage,
Middlewares: middlewares,
ReconnectionBackoff: reconnectionBackoff,
RetryInterval: 5 * time.Second,
MaxRetries: 5,
Device: getDeviceConfig(),
Clock: tdclock.System,
}
client := telegram.NewClient(appID, appHash, options)
@ -141,15 +155,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))
}
middlewares := []telegram.Middleware{floodwait.NewSimpleWaiter()}
client := telegram.NewClient(config.AppId, config.AppHash, telegram.Options{
SessionStorage: storage,
Middlewares: middlewares,
Device: getDeviceConfig(),
NoUpdates: true,
SessionStorage: storage,
Middlewares: middlewares,
ReconnectionBackoff: reconnectionBackoff,
RetryInterval: 5 * time.Second,
MaxRetries: 5,
Device: getDeviceConfig(),
Clock: tdclock.System,
})
stop, err := bg.Connect(client)