Permalink
Browse files

Remove output goroutines

This simplification is possible due to Go's locking within the file's
Write implementation:
https://github.com/golang/go/blob/8d6fc84986cc0cb0bf77503828a2e7740f8ccac1/src/internal/poll/fd_unix.go#L255

Thanks to @jcbwlkr for pointing this out
  • Loading branch information...
frioux committed Sep 27, 2018
1 parent e4918a6 commit 68565964b187c8a4ab66f36cf4389610087b1648
Showing with 2 additions and 27 deletions.
  1. +2 −27 deferLWN.go
@@ -29,26 +29,6 @@ func DeferLWN(args []string, stdin io.Reader) error {
// wg ensures that we block till all lines are done
wg := sync.WaitGroup{}

// stgwg ensures that the following two channels will run to completion
stdwg := sync.WaitGroup{}
stdwg.Add(2)

outC := make(chan string)
go func() { // finishes when outC is closed
for line := range outC {
fmt.Fprintln(os.Stdout, line)
}
stdwg.Done()
}()

errC := make(chan string)
go func() { // finishes when errC is closed
for line := range errC {
fmt.Fprintln(os.Stderr, line)
}
stdwg.Done()
}()

s := bufio.NewScanner(stdin)

for s.Scan() {
@@ -60,20 +40,15 @@ func DeferLWN(args []string, stdin io.Reader) error {
go func() {
err := deferLink(line, dir)
if err != nil {
errC <- err.Error()
outC <- line
fmt.Fprintln(os.Stderr, err)
fmt.Println(line)
}
<-tokens
wg.Done()
}()
}

wg.Wait()
close(outC)
close(errC)

stdwg.Wait()

if s.Err() != nil {
return errors.Wrap(s.Err(), "bufio.Scanner.Scan()")
}

0 comments on commit 6856596

Please sign in to comment.