Skip to content

Commit

Permalink
ensure consistent timeout behaviour for health checks
Browse files Browse the repository at this point in the history
  • Loading branch information
adamtulinius committed Feb 19, 2019
1 parent 3761136 commit bfb367a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
9 changes: 8 additions & 1 deletion healthchecks/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"github.com/dbcdk/morph/ssh"
"github.com/dbcdk/morph/utils"
"net/http"
"time"
)
Expand Down Expand Up @@ -55,7 +56,7 @@ func (healthCheck CmdHealthCheck) GetPeriod() int {
}

func (healthCheck CmdHealthCheck) Run(host Host) error {
ctx, cancel := context.WithTimeout(context.TODO(), time.Duration(healthCheck.Timeout)*time.Second)
ctx, cancel := utils.ContextWithConditionalTimeout(context.TODO(), healthCheck.Timeout)
defer cancel()

cmd, err := healthCheck.SshContext.CmdContext(ctx, host, healthCheck.Cmd...)
Expand Down Expand Up @@ -92,6 +93,12 @@ func (healthCheck HttpHealthCheck) Run(host Host) error {
healthCheck.Host = &replacementHostname
}

// http.Client interprets a timeout of 0 as "no timeout", but we still have to avoid passing
// a negative timeout to it
if healthCheck.Timeout < 0 {
healthCheck.Timeout = 0
}

transport := &http.Transport{}

transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: healthCheck.InsecureSSL}
Expand Down
4 changes: 2 additions & 2 deletions ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"context"
"errors"
"fmt"
"github.com/dbcdk/morph/utils"
"golang.org/x/crypto/ssh/terminal"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"time"
)

type Context interface {
Expand Down Expand Up @@ -154,7 +154,7 @@ func valCommand(parts []string) ([]string, error) {
}

func (sshCtx *SSHContext) CmdInteractive(host Host, timeout int, parts ...string) {
ctx, cancel := context.WithTimeout(context.TODO(), time.Duration(timeout)*time.Second)
ctx, cancel := utils.ContextWithConditionalTimeout(context.TODO(), timeout)
defer cancel()

cmd, err := sshCtx.CmdContext(ctx, host, parts...)
Expand Down
22 changes: 22 additions & 0 deletions utils/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package utils

import (
"context"
"time"
)

// Create a context with a timeout, but only if the timeout is longer than 0
func ContextWithConditionalTimeout(parent context.Context, timeout int) (context.Context, context.CancelFunc) {
var (
ctx context.Context
cancel context.CancelFunc
)

if timeout <= 0 {
ctx, cancel = context.WithCancel(context.TODO())
} else {
ctx, cancel = context.WithTimeout(context.TODO(), time.Duration(timeout)*time.Second)
}

return ctx, cancel
}

0 comments on commit bfb367a

Please # to comment.