diff options
| author | Aleksandr Nogikh <nogikh@google.com> | 2024-06-28 15:48:43 +0200 |
|---|---|---|
| committer | Aleksandr Nogikh <nogikh@google.com> | 2024-07-01 09:13:47 +0000 |
| commit | 714041c90b9b0efe7fb63d39fa6b0aa643f2450c (patch) | |
| tree | 7f808952cf73868d27cd06ce59ef0a3b5818425d /vm/vmimpl/vmimpl.go | |
| parent | a7b22031cdbe8555ef6d4a086bd11dbc9feea4fd (diff) | |
vm: refactor vm.Multiplex arguments
Introduce a MultiplexConfig structure that contains optional parameters.
Include a Scale parameter to control the intended slowdown.
Diffstat (limited to 'vm/vmimpl/vmimpl.go')
| -rw-r--r-- | vm/vmimpl/vmimpl.go | 31 |
1 files changed, 21 insertions, 10 deletions
diff --git a/vm/vmimpl/vmimpl.go b/vm/vmimpl/vmimpl.go index 00a815314..a38ca0d8b 100644 --- a/vm/vmimpl/vmimpl.go +++ b/vm/vmimpl/vmimpl.go @@ -148,8 +148,19 @@ var ( var WaitForOutputTimeout = 10 * time.Second -func Multiplex(cmd *exec.Cmd, merger *OutputMerger, console io.Closer, timeout time.Duration, - stop, closed <-chan bool, debug bool) (<-chan []byte, <-chan error, error) { +type MultiplexConfig struct { + Console io.Closer + Stop <-chan bool + Close <-chan bool + Debug bool + Scale time.Duration +} + +func Multiplex(cmd *exec.Cmd, merger *OutputMerger, timeout time.Duration, config MultiplexConfig) ( + <-chan []byte, <-chan error, error) { + if config.Scale <= 0 { + panic("slowdown must be set") + } errc := make(chan error, 1) signal := func(err error) { select { @@ -161,10 +172,10 @@ func Multiplex(cmd *exec.Cmd, merger *OutputMerger, console io.Closer, timeout t select { case <-time.After(timeout): signal(ErrTimeout) - case <-stop: + case <-config.Stop: signal(ErrTimeout) - case <-closed: - if debug { + case <-config.Close: + if config.Debug { log.Logf(0, "instance closed") } signal(fmt.Errorf("instance closed")) @@ -172,10 +183,10 @@ func Multiplex(cmd *exec.Cmd, merger *OutputMerger, console io.Closer, timeout t cmd.Process.Kill() // Once the command has exited, we might want to let the full console // output accumulate before we abort the console connection too. - time.Sleep(WaitForOutputTimeout) - if console != nil { + time.Sleep(WaitForOutputTimeout * config.Scale) + if config.Console != nil { // Only wait for the merger if we're able to control the console stream. - console.Close() + config.Console.Close() merger.Wait() } if cmdErr := cmd.Wait(); cmdErr == nil { @@ -187,8 +198,8 @@ func Multiplex(cmd *exec.Cmd, merger *OutputMerger, console io.Closer, timeout t return } cmd.Process.Kill() - if console != nil { - console.Close() + if config.Console != nil { + config.Console.Close() merger.Wait() } cmd.Wait() |
