Skip to content

Commit

Permalink
perf: add worker pool
Browse files Browse the repository at this point in the history
  • Loading branch information
markthree committed Mar 10, 2023
1 parent e4442fa commit a551f9e
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/markthree/fast-cpy

go 1.20

require github.com/panjf2000/ants v1.3.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/panjf2000/ants v1.3.0 h1:8pQ+8leaLc9lys2viEEr8md0U4RN6uOSUCE9bOYjQ9M=
github.com/panjf2000/ants v1.3.0/go.mod h1:AaACblRPzq35m1g3enqYcxspbbiOJJYaxU2wMpm1cXY=
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"build-types": "tsc",
"build-go": "go build",
"build-ts": "vite build",
"bench": "node bench.mjs",
"prepublishOnly": "pnpm build",
"release": "bumpp --push --tag --all && npm publish",
"build-go-bin": "goreleaser release --snapshot --clean",
Expand Down
10 changes: 7 additions & 3 deletions src/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package core
import (
"fmt"
"io"
"io/fs"
"os"
"path"
"sync"

"github.com/panjf2000/ants"
)

var pool, _ = ants.NewPool(1000000)

func Exists(path string) bool {
_, err := os.Stat(path)
if err != nil {
Expand Down Expand Up @@ -61,7 +64,8 @@ func Copy(src, dest string) {
wg.Add(entrysLen)

for i := 0; i < entrysLen; i++ {
go func(entry fs.DirEntry) {
entry := entrys[i]
ants.Submit(func() {
defer wg.Done()

n := entry.Name()
Expand Down Expand Up @@ -108,7 +112,7 @@ func Copy(src, dest string) {

io.Copy(dstFile, srcFile)
}
}(entrys[i])
})
}

wg.Wait()
Expand Down

0 comments on commit a551f9e

Please sign in to comment.