[easyssh] Scan stdout and stderr in Stream in seperate goroutines. (appleboy/easyssh-proxy#41)

Fixes appleboy/easyssh-proxy#40.

Splits stdout and stderr scanning into seperate goroutines, and waits on both to finish through use of a wait group.

Also changed `res` chan to use empty structs as they take up less memory.

https://github.com/appleboy/drone-ssh/commit/e788e0d12bd
This commit is contained in:
Joshua Elliott 2018-08-16 18:28:20 +10:00 committed by Wataru Ashihara
parent 442ce52def
commit b5fc545ce2
No known key found for this signature in database
GPG Key ID: CE9EC2D5AE43DEFA

View File

@ -13,6 +13,7 @@ import (
"net"
"os"
"path/filepath"
"sync"
"time"
"golang.org/x/crypto/ssh"
@ -201,17 +202,28 @@ func (ssh_conf *MakeConfig) Stream(command string, timeout time.Duration) (<-cha
defer session.Close()
timeoutChan := time.After(timeout * time.Second)
res := make(chan bool, 1)
res := make(chan struct{}, 1)
var resWg sync.WaitGroup
resWg.Add(2)
go func() {
for stdoutScanner.Scan() {
stdoutChan <- stdoutScanner.Text()
}
resWg.Done()
}()
go func() {
for stderrScanner.Scan() {
stderrChan <- stderrScanner.Text()
}
resWg.Done()
}()
go func() {
resWg.Wait()
// close all of our open resources
res <- true
res <- struct{}{}
}()
select {