Skip to content

Commit

Permalink
coding style: calculate timeout only once
Browse files Browse the repository at this point in the history
  • Loading branch information
hoffie committed Jan 18, 2024
1 parent 29aab8d commit dcf5d4a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
24 changes: 13 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,22 @@ import (
)

var (
verbose = kingpin.Flag("verbose", "Verbose mode.").Short('v').Bool()
trace = kingpin.Flag("trace", "Trace mode.").Bool()
proxyAddr = kingpin.Flag("proxy.listen-addr", "address the proxy will listen on").Required().String()
nextProxyAddr = kingpin.Flag("next-proxy.addr", "optional address of another http proxy when cascading usage is required").String()
metricsAddr = kingpin.Flag("metrics.listen-addr", "adress the service will listen on for metrics request about itself").String()
sshUser = kingpin.Flag("ssh.user", "username used for connecting via ssh").Required().String()
sshKeyFile = kingpin.Flag("ssh.key-file", "private key file used for connecting via ssh").Required().String()
sshKnownHostsFile = kingpin.Flag("ssh.known-hosts-file", "known hosts file used for connecting via ssh").Required().String()
sshPort = kingpin.Flag("ssh.port", "port used for connecting via ssh").Default("22").Int()
timeout = kingpin.Flag("timeout", "full roundtrip request timeout in seconds").Default("50").Int()
verbose = kingpin.Flag("verbose", "Verbose mode.").Short('v').Bool()
trace = kingpin.Flag("trace", "Trace mode.").Bool()
proxyAddr = kingpin.Flag("proxy.listen-addr", "address the proxy will listen on").Required().String()
nextProxyAddr = kingpin.Flag("next-proxy.addr", "optional address of another http proxy when cascading usage is required").String()
metricsAddr = kingpin.Flag("metrics.listen-addr", "adress the service will listen on for metrics request about itself").String()
sshUser = kingpin.Flag("ssh.user", "username used for connecting via ssh").Required().String()
sshKeyFile = kingpin.Flag("ssh.key-file", "private key file used for connecting via ssh").Required().String()
sshKnownHostsFile = kingpin.Flag("ssh.known-hosts-file", "known hosts file used for connecting via ssh").Required().String()
sshPort = kingpin.Flag("ssh.port", "port used for connecting via ssh").Default("22").Int()
timeout = kingpin.Flag("timeout", "full roundtrip request timeout in seconds").Default("50").Int()
timeoutDurationSeconds time.Duration
)

func main() {
kingpin.Parse()
timeoutDurationSeconds = time.Duration(*timeout) * time.Second
if *trace {
log.SetLevel(log.TraceLevel)
} else if *verbose {
Expand All @@ -48,7 +50,7 @@ func main() {
Addr: *proxyAddr,
Handler: ph,
ReadTimeout: 10 * time.Second,
WriteTimeout: time.Duration(*timeout) * time.Second,
WriteTimeout: timeoutDurationSeconds,
MaxHeaderBytes: 1 << 20,
}

Expand Down
3 changes: 1 addition & 2 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io"
"net/http"
"strings"
"time"

"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -142,7 +141,7 @@ func (pr *proxyRequest) buildRequest() error {
}
pr.upstreamClient = &http.Client{
Transport: transport,
Timeout: time.Duration(*timeout) * time.Second,
Timeout: timeoutDurationSeconds,
}
return nil
}
Expand Down
8 changes: 4 additions & 4 deletions sshtransport.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ func (t *sshTransport) createTransports() {
Proxy: nil,
DialContext: t.dialContext,
MaxIdleConns: 100,
IdleConnTimeout: time.Duration(*timeout) * 2 * time.Second,
ResponseHeaderTimeout: time.Duration(*timeout) * time.Second,
IdleConnTimeout: 2 * timeoutDurationSeconds,
ResponseHeaderTimeout: timeoutDurationSeconds,
ExpectContinueTimeout: 1 * time.Second,
}
transportTLSSkipVerify := transportRegular.Clone()
Expand Down Expand Up @@ -131,7 +131,7 @@ func (t *sshTransport) dialContext(ctx context.Context, network, addr string) (n
log.WithFields(log.Fields{"host": targetHost}).Debug("keepalive worked, this is not an ssh conn problem")
return nil, err
}
case <-time.After(time.Duration(*timeout) * time.Second):
case <-time.After(timeoutDurationSeconds):
keepAliveErr = fmt.Errorf("failed to receive keepalive within %d seconds, reconnecting", timeout)
}
log.WithFields(log.Fields{"host": targetHost, "err": keepAliveErr}).Debug("keepalive failed, reconnecting")
Expand Down Expand Up @@ -182,7 +182,7 @@ func (t *sshTransport) getSSHClient(host string) (*ssh.Client, error) {
Auth: t.auth,
HostKeyCallback: t.knownHostsCallback,
HostKeyAlgorithms: upgradedHostKeyAlgos,
Timeout: time.Duration(*timeout) * time.Second,
Timeout: timeoutDurationSeconds,
}
// TODO: This should use DialContext once this PR is merged:
// https://github.com/golang/go/issues/64686
Expand Down

0 comments on commit dcf5d4a

Please sign in to comment.