Skip to content

Commit

Permalink
feat(rclone): respect RCLONE_TRANSFERS in environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyXiang committed Jul 29, 2024
1 parent 329131a commit 40fa6d4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 29 deletions.
49 changes: 30 additions & 19 deletions rclone/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,11 @@ var (
delayBeforeTransfer time.Duration
excludeFileTypes []string

argMultiThreadCutoff int64
argLargeFileTransfers int64
argSmallFileTransfers int64
argMaxTransfers int64

cmdEnv []string
moveArgs []string
largeFileArgs []string
smallFileArgs []string
folderWeight int64

taskChan chan *putio.FileInfo
transferSem *semaphore.Weighted
Expand All @@ -45,25 +41,17 @@ var (

func init() {
rcGlobalConfig := fs.GetConfig(nil)
argMultiThreadCutoff = int64(rcGlobalConfig.MultiThreadCutoff)
argLargeFileTransfers = int64(rcGlobalConfig.Transfers)
argSmallFileTransfers = argLargeFileTransfers * 2
argMaxTransfers = argSmallFileTransfers + 2
argMultiThreadCutoff := int64(rcGlobalConfig.MultiThreadCutoff)
argLargeFileTransfers := int64(rcGlobalConfig.Transfers)
argSmallFileTransfers := argLargeFileTransfers * 2

moveArgs = []string{
"--check-first",
"--no-traverse",
"--use-mmap",
}
largeFileArgs = []string{
fmt.Sprintf("--transfers=%d", argLargeFileTransfers),
fmt.Sprintf("--checkers=%d", rcGlobalConfig.Checkers),
fmt.Sprintf("--min-size=%db", argMultiThreadCutoff),
}
smallFileArgs = []string{
fmt.Sprintf("--transfers=%d", argSmallFileTransfers),
fmt.Sprintf("--checkers=%d", rcGlobalConfig.Checkers*2),
}
largeFileArgs = make([]string, 0, 4)
smallFileArgs = make([]string, 0, 3)

osEnv := os.Environ()
maxTransfers := 0
Expand Down Expand Up @@ -98,6 +86,15 @@ func init() {
largeFileArgs = append(largeFileArgs, filterArgs)
smallFileArgs = append(smallFileArgs, filterArgs)
}
case "RCLONE_TRANSFERS":
transfers, err := strconv.ParseInt(pair[1], 10, 64)
if err != nil {
break
}
if transfers > argLargeFileTransfers {
argSmallFileTransfers = transfers * 2
}
argLargeFileTransfers = transfers
default:
if pair[0] == "HOME" || strings.HasPrefix(pair[0], "RCLONE_") {
cmdEnv = append(cmdEnv, env)
Expand All @@ -108,8 +105,22 @@ func init() {
accessToken := parseRCloneConfig()
Put = putio.New(accessToken, maxTransfers)

maxWeight := argLargeFileTransfers + 1
folderWeight = maxWeight - 1
largeFileArgs = append(
largeFileArgs,
fmt.Sprintf("--min-size=%db", argMultiThreadCutoff),
fmt.Sprintf("--transfers=%d", argLargeFileTransfers),
fmt.Sprintf("--checkers=%d", argLargeFileTransfers*2),
)
smallFileArgs = append(
largeFileArgs,
fmt.Sprintf("--transfers=%d", argSmallFileTransfers),
fmt.Sprintf("--checkers=%d", argSmallFileTransfers*2),
)

taskChan = make(chan *putio.FileInfo, 1)
transferSem = semaphore.NewWeighted(int64(argMaxTransfers))
transferSem = semaphore.NewWeighted(maxWeight)
}

func Start() {
Expand Down
14 changes: 4 additions & 10 deletions rclone/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ func moveFolder(folder *putio.FileInfo) {
return
}

if err := transferSem.Acquire(context.Background(), argSmallFileTransfers); err != nil {
if err := transferSem.Acquire(context.Background(), folderWeight); err != nil {
log.Printf("Failed acquiring semaphore while moving folder %s", folder.Name)
return
}
defer transferSem.Release(argSmallFileTransfers)
defer transferSem.Release(folderWeight)

if folder.Size > 0 {
log.Printf("Moving folder %s...", folder.Name)
Expand Down Expand Up @@ -130,17 +130,11 @@ func moveFile(file *putio.FileInfo) {
return
}

var weight int64
if file.Size < argMultiThreadCutoff {
weight = 1
} else {
weight = 2
}
if err := transferSem.Acquire(context.Background(), weight); err != nil {
if err := transferSem.Acquire(context.Background(), 1); err != nil {
log.Printf("Failed acquiring semaphore while moving file %s", file.Name)
return
}
defer transferSem.Release(weight)
defer transferSem.Release(1)

newFilename := file.Name
if strings.HasPrefix(file.ContentType, putio.ContentTypeVideo) {
Expand Down

0 comments on commit 40fa6d4

Please sign in to comment.