Skip to content

Commit

Permalink
refactor: db params
Browse files Browse the repository at this point in the history
  • Loading branch information
divyam234 committed Jun 4, 2024
1 parent 75e7b63 commit 306a508
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 139 deletions.
12 changes: 5 additions & 7 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ func NewRun() *cobra.Command {

runCmd.Flags().StringVar(&config.DB.DataSource, "db-data-source", "", "Database connection string")
runCmd.Flags().IntVar(&config.DB.LogLevel, "db-log-level", 1, "Database log level")
runCmd.Flags().BoolVar(&config.DB.Migrate.Enable, "db-migrate-enable", true, "Enable database migration")
runCmd.Flags().BoolVar(&config.DB.PrepareStmt, "db-prepare-stmt", true, "Enable prepared statements")
runCmd.Flags().BoolVar(&config.DB.Pool.Enable, "db-pool-enable", true, "Enable database pool")
runCmd.Flags().IntVar(&config.DB.Pool.MaxIdleConnections, "db-pool-max-open-connections", 25, "Database max open connections")
runCmd.Flags().IntVar(&config.DB.Pool.MaxIdleConnections, "db-pool-max-idle-connections", 25, "Database max idle connections")
duration.DurationVar(runCmd.Flags(), &config.DB.Pool.MaxLifetime, "db-pool-max-lifetime", 10*time.Minute, "Database max connection lifetime")
Expand All @@ -83,6 +84,7 @@ func NewRun() *cobra.Command {
runCmd.Flags().StringVar(&config.TG.Proxy, "tg-proxy", "", "HTTP OR SOCKS5 proxy URL")
runCmd.Flags().IntVar(&config.TG.BgBotsLimit, "tg-bg-bots-limit", 5, "Background bots limit")
runCmd.Flags().BoolVar(&config.TG.DisableStreamBots, "tg-disable-stream-bots", false, "Disable stream bots")
runCmd.Flags().BoolVar(&config.TG.EnableLogging, "tg-enable-logging", false, "Enable telegram client logging")
runCmd.Flags().StringVar(&config.TG.Uploads.EncryptionKey, "tg-uploads-encryption-key", "", "Uploads encryption key")
runCmd.Flags().IntVar(&config.TG.Uploads.Threads, "tg-uploads-threads", 8, "Uploads threads")
runCmd.Flags().IntVar(&config.TG.Uploads.MaxRetries, "tg-uploads-max-retries", 10, "Uploads Retries")
Expand Down Expand Up @@ -133,12 +135,6 @@ func runApplication(conf *config.Config) {
),
)

defer func() {
if r := recover(); r != nil {
logging.FromContext(context.TODO()).Errorf("Recovered from panic: %v", r)
}
}()

app.Run()
}

Expand Down Expand Up @@ -245,7 +241,9 @@ func initApp(lc fx.Lifecycle, cfg *config.Config, c *controller.Controller) *gin
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
logging.FromContext(ctx).Infof("Started server http://localhost:%d", cfg.Server.Port)

go func() {

if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logging.DefaultLogger().Errorw("failed to close http server", "err", err)
}
Expand Down
6 changes: 2 additions & 4 deletions config.sample.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
[db]
data-source = ""
prepare-stmt = true
log-level = 1

[db.migrate]
enable = true

[db.pool]
enable = true
max-idle-connections = 25
max-lifetime = "10m"
max-open-connections = 25
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,20 @@ require (
go.uber.org/zap v1.27.0
golang.org/x/time v0.5.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
gorm.io/datatypes v1.2.0
gorm.io/driver/postgres v1.5.7
gorm.io/gorm v1.25.10
)

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-sql-driver/mysql v1.8.1 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
Expand All @@ -57,6 +60,7 @@ require (
golang.org/x/exp v0.0.0-20240409090435-93d18d7e34b8 // indirect
google.golang.org/appengine v1.6.8 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gorm.io/driver/mysql v1.5.6 // indirect

)

Expand Down
16 changes: 16 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,15 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 h1:au07oEsX2xN0ktxqI+Sida1w446QrXBRJ0nee3SNZlA=
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
github.com/golang-sql/sqlexp v0.1.0 h1:ZCD6MBpcuOVfGVqsEmY5/4FtYiKz6tSyUv9LPEDei6A=
github.com/golang-sql/sqlexp v0.1.0/go.mod h1:J4ad9Vo8ZCWQ2GMrC4UCQy1JpCbwU9m3EOqtpKwwwHI=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
Expand Down Expand Up @@ -136,8 +141,12 @@ github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0V
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.15 h1:vfoHhTN1af61xCRSWzFIWzx2YskyMTwHLrExkBOjvxI=
github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/mfridman/interpolate v0.0.2 h1:pnuTK7MQIxxFz1Gr+rjSIx9u7qVjf5VOoM/u6BbAxPY=
github.com/mfridman/interpolate v0.0.2/go.mod h1:p+7uk6oE07mpE/Ik1b8EckO0O4ZXiGAfshKBWLUM9Xg=
github.com/microsoft/go-mssqldb v1.7.0 h1:sgMPW0HA6Ihd37Yx0MzHyKD726C2kY/8KJsQtXHNaAs=
github.com/microsoft/go-mssqldb v1.7.0/go.mod h1:kOvZKUdrhhFQmxLZqbwUV0rHkNkZpthMITIb2Ko1IoA=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
Expand Down Expand Up @@ -309,10 +318,17 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/datatypes v1.2.0 h1:5YT+eokWdIxhJgWHdrb2zYUimyk0+TaFth+7a0ybzco=
gorm.io/datatypes v1.2.0/go.mod h1:o1dh0ZvjIjhH/bngTpypG6lVRJ5chTBxE09FH/71k04=
gorm.io/driver/mysql v1.5.6 h1:Ld4mkIickM+EliaQZQx3uOJDJHtrd70MxAUqWqlx3Y8=
gorm.io/driver/mysql v1.5.6/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM=
gorm.io/driver/postgres v1.5.7 h1:8ptbNJTDbEmhdr62uReG5BGkdQyeasu/FZHxI0IMGnM=
gorm.io/driver/postgres v1.5.7/go.mod h1:3e019WlBaYI5o5LIdNV+LyxCMNtLOQETBXL2h4chKpA=
gorm.io/driver/sqlite v1.4.3 h1:HBBcZSDnWi5BW3B3rwvVTc510KGkBkexlOg0QrmLUuU=
gorm.io/driver/sqlite v1.4.3/go.mod h1:0Aq3iPO+v9ZKbcdiz8gLWRw5VOPcBOPUQJFLq5e2ecI=
gorm.io/driver/sqlserver v1.4.1 h1:t4r4r6Jam5E6ejqP7N82qAJIJAht27EGT41HyPfXRw0=
gorm.io/driver/sqlserver v1.4.1/go.mod h1:DJ4P+MeZbc5rvY58PnmN1Lnyvb5gw5NPzGshHDnJLig=
gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
gorm.io/gorm v1.25.10 h1:dQpO+33KalOA+aFYGlK+EfxcI5MbO7EP2yYygwh9h+s=
gorm.io/gorm v1.25.10/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 h1:5D53IMaUuA5InSeMu9eJtlQXS2NxAhyWQvkKEgXZhHI=
Expand Down
12 changes: 6 additions & 6 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type TGConfig struct {
Proxy string
ReconnectTimeout time.Duration
PoolSize int64
EnableLogging bool
Uploads struct {
EncryptionKey string
Threads int
Expand All @@ -56,12 +57,11 @@ type JWTConfig struct {
}

type DBConfig struct {
DataSource string
LogLevel int
Migrate struct {
Enable bool
}
Pool struct {
DataSource string
PrepareStmt bool
LogLevel int
Pool struct {
Enable bool
MaxOpenConnections int
MaxIdleConnections int
MaxLifetime time.Duration
Expand Down
32 changes: 19 additions & 13 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ func NewDatabase(cfg *config.Config) (*gorm.DB, error) {
err error
logger = NewLogger(time.Second, true, zapcore.Level(cfg.DB.LogLevel))
)

for i := 0; i <= 10; i++ {
db, err = gorm.Open(postgres.Open(cfg.DB.DataSource), &gorm.Config{
for i := 0; i <= 5; i++ {
db, err = gorm.Open(postgres.New(postgres.Config{
DSN: cfg.DB.DataSource,
PreferSimpleProtocol: !cfg.DB.PrepareStmt,
}), &gorm.Config{
Logger: logger,
NamingStrategy: schema.NamingStrategy{
TablePrefix: "teldrive.",
Expand All @@ -38,24 +40,28 @@ func NewDatabase(cfg *config.Config) (*gorm.DB, error) {
logging.DefaultLogger().Warnf("failed to open database: %v", err)
time.Sleep(500 * time.Millisecond)
}
db.Use(extraClausePlugin.New())

if err != nil {
logging.DefaultLogger().Fatalf("database: %v", err)
}

rawDB, err := db.DB()
if err != nil {
return nil, err
}
rawDB.SetMaxOpenConns(cfg.DB.Pool.MaxOpenConnections)
rawDB.SetMaxIdleConns(cfg.DB.Pool.MaxIdleConnections)
rawDB.SetConnMaxLifetime(cfg.DB.Pool.MaxLifetime)
db.Use(extraClausePlugin.New())

if cfg.DB.Migrate.Enable {
err := migrateDB(rawDB)
if cfg.DB.Pool.Enable {
rawDB, err := db.DB()
if err != nil {
return nil, err
}
rawDB.SetMaxOpenConns(cfg.DB.Pool.MaxOpenConnections)
rawDB.SetMaxIdleConns(cfg.DB.Pool.MaxIdleConnections)
rawDB.SetConnMaxLifetime(cfg.DB.Pool.MaxLifetime)
}

sqlDb, _ := db.DB()
err = migrateDB(sqlDb)
if err != nil {
return nil, err
}

return db, nil
}
9 changes: 9 additions & 0 deletions internal/tgc/tgc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/cenkalti/backoff/v4"
"github.com/divyam234/teldrive/internal/config"
"github.com/divyam234/teldrive/internal/kv"
"github.com/divyam234/teldrive/internal/logging"
"github.com/divyam234/teldrive/internal/recovery"
"github.com/divyam234/teldrive/internal/retry"
"github.com/divyam234/teldrive/internal/utils"
Expand All @@ -16,6 +17,7 @@ import (
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/dcs"
"github.com/pkg/errors"
"go.uber.org/zap"
"golang.org/x/net/proxy"
"golang.org/x/time/rate"
)
Expand All @@ -31,6 +33,12 @@ func New(ctx context.Context, config *config.TGConfig, handler telegram.UpdateHa
dialer = d.DialContext
}

var logger *zap.Logger
if config.EnableLogging {
logger = logging.FromContext(ctx).Desugar().Named("td")

}

opts := telegram.Options{
Resolver: dcs.Plain(dcs.PlainOptions{
Dial: dialer,
Expand All @@ -52,6 +60,7 @@ func New(ctx context.Context, config *config.TGConfig, handler telegram.UpdateHa
DialTimeout: 10 * time.Second,
Middlewares: middlewares,
UpdateHandler: handler,
Logger: logger,
}

return telegram.NewClient(config.AppId, config.AppHash, opts), nil
Expand Down
48 changes: 9 additions & 39 deletions pkg/cron/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,33 @@ package cron

import (
"context"
"database/sql/driver"
"encoding/json"
"time"

"github.com/divyam234/teldrive/internal/config"
"github.com/divyam234/teldrive/internal/logging"
"github.com/divyam234/teldrive/pkg/models"
"github.com/divyam234/teldrive/pkg/schemas"
"github.com/divyam234/teldrive/pkg/services"
"github.com/go-co-op/gocron"
"go.uber.org/zap"
"gorm.io/datatypes"
"gorm.io/gorm"
)

type Files []File
type File struct {
ID string `json:"id"`
Parts []models.Part `json:"parts"`
}

func (a Files) Value() (driver.Value, error) {
return json.Marshal(a)
}

func (a *Files) Scan(value interface{}) error {
if err := json.Unmarshal(value.([]byte), &a); err != nil {
return err
}
return nil
}

type UpParts []int

func (a UpParts) Value() (driver.Value, error) {
return json.Marshal(a)
}

func (a *UpParts) Scan(value interface{}) error {
if err := json.Unmarshal(value.([]byte), &a); err != nil {
return err
}
return nil
ID string `json:"id"`
Parts []schemas.Part `json:"parts"`
}

type Result struct {
Files Files
Files datatypes.JSONSlice[File]
Session string
UserId int64
ChannelId int64
}

type UploadResult struct {
Parts UpParts
Parts datatypes.JSONSlice[int]
Session string
UserId int64
ChannelId int64
Expand Down Expand Up @@ -137,20 +112,15 @@ func (c *CronService) CleanUploads(ctx context.Context) {

for _, result := range upResults {

parts := []int{}
for _, id := range result.Parts {
parts = append(parts, id)
}

if result.Session == "" && len(parts) > 0 {
c.db.Where("part_id = any($1)", parts).Delete(&models.Upload{})
if result.Session == "" && len(result.Parts) > 0 {
c.db.Where("part_id = any($1)", result.Parts).Delete(&models.Upload{})
break
}
err := services.DeleteTGMessages(ctx, &c.cnf.TG, result.Session, result.ChannelId, result.UserId, result.Parts)
c.logger.Errorw("failed to delete messages", err)

if err == nil {
c.db.Where("part_id = any($1)", parts).Delete(&models.Upload{})
c.db.Where("part_id = any($1)", result.Parts).Delete(&models.Upload{})
}
}
}
Expand Down
15 changes: 7 additions & 8 deletions pkg/mapper/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,17 @@ func ToFileOut(file models.File) *schemas.FileOut {
}

func ToFileOutFull(file models.File) *schemas.FileOutFull {
parts := []schemas.Part{}
for _, part := range *file.Parts {
parts = append(parts, schemas.Part{
ID: part.ID,
Salt: part.Salt,
})

var channelId int64

if file.ChannelID != nil {
channelId = *file.ChannelID
}

return &schemas.FileOutFull{
FileOut: ToFileOut(file),
Parts: parts,
ChannelID: *file.ChannelID,
Parts: file.Parts,
ChannelID: channelId,
Encrypted: file.Encrypted,
}
}
Expand Down
Loading

0 comments on commit 306a508

Please sign in to comment.