Skip to content

Commit

Permalink
fix(prj): set part size to max by default
Browse files Browse the repository at this point in the history
* fix part size

* remove useless --size flag in e2e test
  • Loading branch information
iyear authored Nov 7, 2024
1 parent 98dac73 commit 0a084ef
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 14 deletions.
2 changes: 0 additions & 2 deletions app/dl/dl.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ func Run(ctx context.Context, c *telegram.Client, kvd kv.KV, opts Options) (rerr

options := downloader.Options{
Pool: pool,
PartSize: viper.GetInt(consts.FlagPartSize),
Threads: viper.GetInt(consts.FlagThreads),
Iter: it,
Progress: newProgress(dlProgress, it, opts),
Expand All @@ -114,7 +113,6 @@ func Run(ctx context.Context, c *telegram.Client, kvd kv.KV, opts Options) (rerr
zap.String("dir", opts.Dir),
zap.Bool("rewrite_ext", opts.RewriteExt),
zap.Bool("skip_same", opts.SkipSame),
zap.Int("part_size", options.PartSize),
zap.Int("threads", options.Threads),
zap.Int("limit", limit))

Expand Down
1 change: 0 additions & 1 deletion app/forward/forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ func Run(ctx context.Context, c *telegram.Client, kvd kv.KV, opts Options) (rerr
delay: viper.GetDuration(consts.FlagDelay),
}),
Progress: newProgress(fwProgress),
PartSize: viper.GetInt(consts.FlagPartSize),
Threads: viper.GetInt(consts.FlagThreads),
})

Expand Down
1 change: 0 additions & 1 deletion app/up/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func Run(ctx context.Context, c *telegram.Client, kvd kv.KV, opts Options) (rerr

options := uploader.Options{
Client: pool.Default(ctx),
PartSize: viper.GetInt(consts.FlagPartSize),
Threads: viper.GetInt(consts.FlagThreads),
Iter: newIter(files, to, opts.Photo, opts.Remove, viper.GetDuration(consts.FlagDelay)),
Progress: newProgress(upProgress),
Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ func New() *cobra.Command {
cmd.PersistentFlags().Bool(consts.FlagDebug, false, "enable debug mode")

cmd.PersistentFlags().IntP(consts.FlagPartSize, "s", 512*1024, "part size for transfer")
_ = cmd.PersistentFlags().MarkDeprecated(consts.FlagPartSize, "part size has been set to maximum by default, this flag will be removed in the future")

cmd.PersistentFlags().IntP(consts.FlagThreads, "t", 4, "max threads for transfer one item")
cmd.PersistentFlags().IntP(consts.FlagLimit, "l", 2, "max number of concurrent tasks")
cmd.PersistentFlags().Int(consts.FlagPoolSize, 8, "specify the size of the DC pool, zero means infinity")
Expand Down
8 changes: 5 additions & 3 deletions core/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import (
"github.com/iyear/tdl/core/util/tutil"
)

// MaxPartSize refer to https://core.telegram.org/api/files#downloading-files
const MaxPartSize = 1024 * 1024

type Downloader struct {
opts Options
}

type Options struct {
Pool dcpool.Pool
PartSize int
Threads int
Iter Iter
Progress Progress
Expand Down Expand Up @@ -77,10 +79,10 @@ func (d *Downloader) download(ctx context.Context, elem Elem) error {
client = d.opts.Pool.Takeout(ctx, elem.File().DC())
}

_, err := downloader.NewDownloader().WithPartSize(d.opts.PartSize).
_, err := downloader.NewDownloader().WithPartSize(MaxPartSize).
Download(client, elem.File().Location()).
WithThreads(tutil.BestThreads(elem.File().Size(), d.opts.Threads)).
Parallel(ctx, newWriteAt(elem, d.opts.Progress, d.opts.PartSize))
Parallel(ctx, newWriteAt(elem, d.opts.Progress, MaxPartSize))
if err != nil {
return errors.Wrap(err, "download")
}
Expand Down
6 changes: 4 additions & 2 deletions core/forwarder/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
"go.uber.org/atomic"
"go.uber.org/multierr"

tdownloader "github.com/iyear/tdl/core/downloader"
"github.com/iyear/tdl/core/tmedia"
tuploader "github.com/iyear/tdl/core/uploader"
"github.com/iyear/tdl/core/util/tutil"
)

Expand Down Expand Up @@ -47,7 +49,7 @@ func (f *Forwarder) cloneMedia(ctx context.Context, opts cloneOptions, dryRun bo
threads := tutil.BestThreads(opts.media.Size, f.opts.Threads)

_, err = downloader.NewDownloader().
WithPartSize(f.opts.PartSize).
WithPartSize(tdownloader.MaxPartSize).
Download(f.opts.Pool.Client(ctx, opts.media.DC), opts.media.InputFileLoc).
WithThreads(threads).
Parallel(ctx, writeAt{
Expand All @@ -66,7 +68,7 @@ func (f *Forwarder) cloneMedia(ctx context.Context, opts cloneOptions, dryRun bo

upload := uploader.NewUpload(opts.media.Name, temp, opts.media.Size)
file, err = uploader.NewUploader(f.opts.Pool.Default(ctx)).
WithPartSize(f.opts.PartSize).
WithPartSize(tuploader.MaxPartSize).
WithThreads(threads).
WithProgress(uploaded{
opts: opts,
Expand Down
1 change: 0 additions & 1 deletion core/forwarder/forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ type Mode int

type Options struct {
Pool dcpool.Pool
PartSize int
Threads int
Iter Iter
Progress Progress
Expand Down
6 changes: 4 additions & 2 deletions core/uploader/uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ import (
"github.com/iyear/tdl/core/util/mediautil"
)

// MaxPartSize refer to https://core.telegram.org/api/files#uploading-files
const MaxPartSize = 512 * 1024

type Uploader struct {
opts Options
}

type Options struct {
Client *tg.Client
PartSize int
Threads int
Iter Iter
Progress Progress
Expand Down Expand Up @@ -72,7 +74,7 @@ func (u *Uploader) upload(ctx context.Context, elem Elem) error {
}

up := uploader.NewUploader(u.opts.Client).
WithPartSize(u.opts.PartSize).
WithPartSize(MaxPartSize).
WithThreads(u.opts.Threads).
WithProgress(&wrapProcess{
elem: elem,
Expand Down
2 changes: 1 addition & 1 deletion pkg/consts/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const (
FlagProxy = "proxy"
FlagNamespace = "ns"
FlagDebug = "debug"
FlagPartSize = "size"
FlagPartSize = "size" // Deprecated: all part size should be set to maximum by default
FlagThreads = "threads"
FlagLimit = "limit"
FlagPoolSize = "pool"
Expand Down
1 change: 0 additions & 1 deletion test/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ func exec(cmd *cobra.Command, args []string, success bool) {

log.Printf("args: %s\n", args)
cmd.SetArgs(append([]string{
"-s", "131072", // self-hosted Telegram server don't support 1MiB
"-n", testAccount,
"--storage", fmt.Sprintf("type=file,path=%s", sessionFile),
}, args...))
Expand Down

0 comments on commit 0a084ef

Please sign in to comment.