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 concurrency #6

Merged
merged 1 commit into from Oct 18, 2017
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+36 −12
Diff settings

Always

Just for now

Add concurrency

We run the individual search-replace operations asyncronously, but use a
buffered channel to ensure output remains in-order. Using a channel of
channels, we syncronously put a chan string placeholder in the channel
and send to it in a goroutine (async).

To ensure we don't get stuck in deadlock, we use a waitgroup to close
the channel (which effectively ends the master process) after all
channels have been processed.
  • Loading branch information...
joshbetz committed Oct 18, 2017
commit a8c06ec3ce0de9bda15289344e7c52a22848e2f5
Copy path View file
@@ -7,6 +7,7 @@ import (
"os"
"regexp"
"strings"
"sync"
)

const (
@@ -40,22 +41,45 @@ func main() {
return
}

// Replace
r := bufio.NewReaderSize(os.Stdin, 2*1024*1024)
for {
line, err := r.ReadString('\n')
var wg sync.WaitGroup
wg.Add(1)
lines := make(chan chan string, 10)

if err == io.EOF {
break
}
go func() {
defer wg.Done()

r := bufio.NewReaderSize(os.Stdin, 2*1024*1024)
for {
line, err := r.ReadString('\n')

if err == io.EOF {
break
}

if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
break
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
break
}

wg.Add(1)
ch := make(chan string)
lines <- ch

go func(line string) {
defer wg.Done()
line = replaceAndFix(line, from, to)
ch <- line
}(line)
}
}()

go func() {
wg.Wait()
close(lines)
}()

line = replaceAndFix(line, from, to)
fmt.Print(line)
for line := range lines {
fmt.Print(<-line)
}
}

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.