Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add CPU-level parallelism for computing binary diffs #39

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 41 additions & 22 deletions cmd/go-selfupdate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import (
"flag"
"fmt"
"io"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"sync"

"github.com/kr/binarydist"
)
Expand Down Expand Up @@ -60,17 +62,6 @@ func newGzReader(r io.ReadCloser) io.ReadCloser {
}

func createUpdate(path string, platform string) {
c := current{Version: version, Sha256: generateSha256(path)}

b, err := json.MarshalIndent(c, "", " ")
if err != nil {
fmt.Println("error:", err)
}
err = ioutil.WriteFile(filepath.Join(genDir, platform+".json"), b, 0755)
if err != nil {
panic(err)
}

os.MkdirAll(filepath.Join(genDir, version), 0755)

var buf bytes.Buffer
Expand All @@ -83,17 +74,12 @@ func createUpdate(path string, platform string) {
w.Close() // You must close this first to flush the bytes to the buffer.
err = ioutil.WriteFile(filepath.Join(genDir, version, platform+".gz"), buf.Bytes(), 0755)

files, err := ioutil.ReadDir(genDir)
if err != nil {
fmt.Println(err)
}

for _, file := range files {
processUpdate := func(file fs.FileInfo) {
if file.IsDir() == false {
continue
return
}
if file.Name() == version {
continue
return
}

os.Mkdir(filepath.Join(genDir, file.Name(), version), 0755)
Expand All @@ -102,14 +88,13 @@ func createUpdate(path string, platform string) {
old, err := os.Open(fName)
if err != nil {
// Don't have an old release for this os/arch, continue on
continue
return
}

fName = filepath.Join(genDir, version, platform+".gz")
newF, err := os.Open(fName)
if err != nil {
fmt.Fprintf(os.Stderr, "Can't open %s: error: %s\n", fName, err)
os.Exit(1)
panic(fmt.Sprintf("Can't open %s: error: %s", fName, err))
}

ar := newGzReader(old)
Expand All @@ -122,6 +107,40 @@ func createUpdate(path string, platform string) {
}
ioutil.WriteFile(filepath.Join(genDir, file.Name(), version, platform), patch.Bytes(), 0755)
}

files, err := ioutil.ReadDir(genDir)
if err != nil {
fmt.Println(err)
}

// spin up parallel workers to process the files:
numWorkers := runtime.NumCPU()
filesChan := make(chan fs.FileInfo)
var wg sync.WaitGroup
wg.Add(numWorkers)
for i := 0; i < numWorkers; i++ {
go func() {
for file := range filesChan {
processUpdate(file)
}
wg.Done()
}()
}
for _, file := range files {
filesChan <- file
}
close(filesChan)
wg.Wait()

c := current{Version: version, Sha256: generateSha256(path)}
b, err := json.MarshalIndent(c, "", " ")
if err != nil {
fmt.Println("error:", err)
}
err = ioutil.WriteFile(filepath.Join(genDir, platform+".json"), b, 0755)
if err != nil {
panic(err)
}
}

func printUsage() {
Expand Down