use correct context for communicator.Retry

The timeout for a provisioner is expected to only apply to the initial
connection. Keep the context for the communicator.Retry separate from
the global cancellation context.
This commit is contained in:
James Bardin 2018-03-20 13:06:28 -04:00
parent 38e6309f03
commit 9b4b5f2a72
5 changed files with 26 additions and 23 deletions

View File

@ -312,11 +312,11 @@ func applyFn(ctx context.Context) error {
return err
}
ctx, cancel := context.WithTimeout(ctx, comm.Timeout())
retryCtx, cancel := context.WithTimeout(ctx, comm.Timeout())
defer cancel()
// Wait and retry until we establish the connection
err = communicator.Retry(ctx, func() error {
err = communicator.Retry(retryCtx, func() error {
return comm.Connect(o)
})
if err != nil {

View File

@ -48,9 +48,6 @@ func applyFn(ctx context.Context) error {
return err
}
ctx, cancel := context.WithTimeout(ctx, comm.Timeout())
defer cancel()
// Get the source
src, deleteSource, err := getSrc(data)
if err != nil {
@ -99,8 +96,11 @@ func getSrc(data *schema.ResourceData) (string, bool, error) {
// copyFiles is used to copy the files from a source to a destination
func copyFiles(ctx context.Context, comm communicator.Communicator, src, dst string) error {
retryCtx, cancel := context.WithTimeout(ctx, comm.Timeout())
defer cancel()
// Wait and retry until we establish the connection
err := communicator.Retry(ctx, func() error {
err := communicator.Retry(retryCtx, func() error {
return comm.Connect(nil)
})
if err != nil {

View File

@ -231,10 +231,10 @@ func applyFn(ctx context.Context) error {
return err
}
ctx, cancel := context.WithTimeout(ctx, comm.Timeout())
retryCtx, cancel := context.WithTimeout(ctx, comm.Timeout())
defer cancel()
err = communicator.Retry(ctx, func() error {
err = communicator.Retry(retryCtx, func() error {
return comm.Connect(o)
})

View File

@ -157,20 +157,23 @@ func runScripts(
comm communicator.Communicator,
scripts []io.ReadCloser) error {
// Wait for the context to end and then disconnect
go func() {
<-ctx.Done()
comm.Disconnect()
}()
retryCtx, cancel := context.WithTimeout(ctx, comm.Timeout())
defer cancel()
// Wait and retry until we establish the connection
err := communicator.Retry(ctx, func() error {
err := communicator.Retry(retryCtx, func() error {
return comm.Connect(o)
})
if err != nil {
return err
}
// Wait for the context to end and then disconnect
go func() {
<-ctx.Done()
comm.Disconnect()
}()
for _, script := range scripts {
var cmd *remote.Cmd

View File

@ -131,17 +131,11 @@ func applyFn(ctx context.Context) error {
return err
}
ctx, cancelFunc := context.WithTimeout(ctx, comm.Timeout())
defer cancelFunc()
// Wait for the context to end and then disconnect
go func() {
<-ctx.Done()
comm.Disconnect()
}()
retryCtx, cancel := context.WithTimeout(ctx, comm.Timeout())
defer cancel()
// Wait and retry until we establish the connection
err = communicator.Retry(ctx, func() error {
err = communicator.Retry(retryCtx, func() error {
return comm.Connect(o)
})
@ -149,6 +143,12 @@ func applyFn(ctx context.Context) error {
return err
}
// Wait for the context to end and then disconnect
go func() {
<-ctx.Done()
comm.Disconnect()
}()
var src, dst string
o.Output("Provisioning with Salt...")