envoy: start timeout func after validation

This removes the need to check arg length in the timeout function.
This commit is contained in:
Daniel Nephin 2021-05-31 17:37:41 -04:00
parent 9d333309fe
commit 177a504e9f
2 changed files with 12 additions and 15 deletions

View File

@ -6,8 +6,9 @@ import (
"os" "os"
"time" "time"
"github.com/hashicorp/consul/command/flags"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
"github.com/hashicorp/consul/command/flags"
) )
func New(ui cli.Ui) *cmd { func New(ui cli.Ui) *cmd {
@ -27,23 +28,21 @@ func (c *cmd) init() {
} }
func (c *cmd) Run(args []string) int { func (c *cmd) Run(args []string) int {
// This should never be alive for very long. In case bad things happen and
// Envoy never starts limit how long we live before just exiting so we can't
// accumulate tons of these zombie children.
time.AfterFunc(10*time.Second, func() {
// Force cleanup
if len(args) > 0 {
os.RemoveAll(args[0])
}
os.Exit(99)
})
// Read from STDIN, write to the named pipe provided in the only positional arg // Read from STDIN, write to the named pipe provided in the only positional arg
if len(args) != 1 { if len(args) != 1 {
c.UI.Error("Expecting named pipe path as argument") c.UI.Error("Expecting named pipe path as argument")
return 1 return 1
} }
// This should never be alive for very long. In case bad things happen and
// Envoy never starts limit how long we live before just exiting so we can't
// accumulate tons of these zombie children.
time.AfterFunc(10*time.Second, func() {
// Force cleanup
os.RemoveAll(args[0])
os.Exit(99)
})
// WRONLY is very important here - the open() call will block until there is a // WRONLY is very important here - the open() call will block until there is a
// reader (Envoy) if we open it with RDWR though that counts as an opener and // reader (Envoy) if we open it with RDWR though that counts as an opener and
// we will just send the data to ourselves as the first opener and so only // we will just send the data to ourselves as the first opener and so only
@ -60,8 +59,7 @@ func (c *cmd) Run(args []string) int {
return 1 return 1
} }
err = f.Close() if err = f.Close(); err != nil {
if err != nil {
c.UI.Error(err.Error()) c.UI.Error(err.Error())
return 1 return 1
} }

View File

@ -8,7 +8,6 @@ import (
) )
func TestConnectEnvoyPipeBootstrapCommand_noTabs(t *testing.T) { func TestConnectEnvoyPipeBootstrapCommand_noTabs(t *testing.T) {
t.Parallel()
if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') { if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') {
t.Fatal("help has tabs") t.Fatal("help has tabs")
} }