feat(forward): support —delay flag

This commit is contained in:
XMLHexagram 2024-03-24 22:44:25 +08:00
parent 735bbc74f7
commit 12c4f060de
6 changed files with 16 additions and 1 deletions

View File

@ -99,7 +99,7 @@ func (i *iter) HasNext() bool {
defer i.mu.Unlock()
j := i.j + 1
return i.i < len(i.dialogs) && j < len(i.dialogs[i.i].Messages)
return i.err == nil && i.i < len(i.dialogs) && j < len(i.dialogs[i.i].Messages)
}
func (i *iter) Next(ctx context.Context) bool {

View File

@ -99,6 +99,7 @@ func Run(ctx context.Context, c *telegram.Client, kvd kv.KV, opts Options) (rerr
Progress: newProgress(fwProgress),
PartSize: viper.GetInt(consts.FlagPartSize),
Threads: viper.GetInt(consts.FlagThreads),
Delay: viper.GetDuration(consts.FlagDelay),
})
go fwProgress.Render()

View File

@ -80,6 +80,10 @@ func newIter(opts iterOptions) *iter {
}
}
func (i *iter) HasNext() bool {
return i.err == nil && i.i < len(i.opts.dialogs)
}
func (i *iter) Next(ctx context.Context) bool {
select {
case <-ctx.Done():

View File

@ -55,6 +55,7 @@ func (d *Downloader) Download(ctx context.Context, limit int) error {
}
if d.opts.Delay != 0 && d.opts.Iter.HasNext() {
logger.From(ctx).Debug("Delay", zap.Duration("delay", d.opts.Delay))
color.Yellow("Delay %s", d.opts.Delay.String())
<-time.After(d.opts.Delay)
}

View File

@ -2,6 +2,7 @@ package forwarder
import (
"context"
"github.com/fatih/color"
"math/rand"
"time"
@ -30,6 +31,7 @@ type Options struct {
Threads int
Iter Iter
Progress Progress
Delay time.Duration
}
type Forwarder struct {
@ -79,6 +81,12 @@ func (f *Forwarder) Forward(ctx context.Context) error {
}
continue
}
if f.opts.Delay != 0 && f.opts.Iter.HasNext() {
logger.From(ctx).Debug("Delay", zap.Duration("delay", f.opts.Delay))
color.Yellow("Delay %s", f.opts.Delay.String())
<-time.After(f.opts.Delay)
}
}
return f.opts.Iter.Err()

View File

@ -9,6 +9,7 @@ import (
type Iter interface {
Next(ctx context.Context) bool
HasNext() bool
Value() Elem
Err() error
}