feat(traffic): support limit task worker count & file stream rate (#7948)
* feat: set task workers num & client stream rate limit * feat: server stream rate limit * upgrade xhofe/tache * .
This commit is contained in:
62
internal/driver/utils.go
Normal file
62
internal/driver/utils.go
Normal file
@ -0,0 +1,62 @@
|
||||
package driver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/alist-org/alist/v3/internal/model"
|
||||
"github.com/alist-org/alist/v3/internal/stream"
|
||||
"io"
|
||||
)
|
||||
|
||||
type UpdateProgress = model.UpdateProgress
|
||||
|
||||
type Progress struct {
|
||||
Total int64
|
||||
Done int64
|
||||
up UpdateProgress
|
||||
}
|
||||
|
||||
func (p *Progress) Write(b []byte) (n int, err error) {
|
||||
n = len(b)
|
||||
p.Done += int64(n)
|
||||
p.up(float64(p.Done) / float64(p.Total) * 100)
|
||||
return
|
||||
}
|
||||
|
||||
func NewProgress(total int64, up UpdateProgress) *Progress {
|
||||
return &Progress{
|
||||
Total: total,
|
||||
up: up,
|
||||
}
|
||||
}
|
||||
|
||||
type RateLimitReader = stream.RateLimitReader
|
||||
|
||||
type RateLimitWriter = stream.RateLimitWriter
|
||||
|
||||
type RateLimitFile = stream.RateLimitFile
|
||||
|
||||
func NewLimitedUploadStream(ctx context.Context, r io.Reader) *RateLimitReader {
|
||||
return &RateLimitReader{
|
||||
Reader: r,
|
||||
Limiter: stream.ServerUploadLimit,
|
||||
Ctx: ctx,
|
||||
}
|
||||
}
|
||||
|
||||
func NewLimitedUploadFile(ctx context.Context, f model.File) *RateLimitFile {
|
||||
return &RateLimitFile{
|
||||
File: f,
|
||||
Limiter: stream.ServerUploadLimit,
|
||||
Ctx: ctx,
|
||||
}
|
||||
}
|
||||
|
||||
func ServerUploadLimitWaitN(ctx context.Context, n int) error {
|
||||
return stream.ServerUploadLimit.WaitN(ctx, n)
|
||||
}
|
||||
|
||||
type ReaderWithCtx = stream.ReaderWithCtx
|
||||
|
||||
type ReaderUpdatingProgress = stream.ReaderUpdatingProgress
|
||||
|
||||
type SimpleReaderWithSize = stream.SimpleReaderWithSize
|
Reference in New Issue
Block a user