fix: use IdleConnTimeout/ConnectTimeout
continuous-integration/drone/push Build is passing Details

This is an attempt to set sensible timeouts on abra connections. This
might not be the last word on this but it seems that SSH connections now
bail out correctly and other kinds of commands don't explode (e.g.
logs).

Closes coop-cloud/organising#222.
Closes coop-cloud/organising#218.
This commit is contained in:
decentral1se 2021-11-02 15:49:11 +01:00
parent ede5a59562
commit e37b49201f
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
5 changed files with 12 additions and 11 deletions

1
go.mod
View File

@ -44,4 +44,5 @@ require (
github.com/sfreiberg/simplessh v0.0.0-20180301191542-495cbb862a9c
github.com/theupdateframework/notary v0.7.0 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
golang.org/x/sys v0.0.0-20210910150752-751e447fb3d0 // indirect
)

View File

@ -6,6 +6,7 @@ import (
"os"
"time"
commandconnPkg "coopcloud.tech/abra/pkg/upstream/commandconn"
"github.com/docker/docker/client"
"github.com/sirupsen/logrus"
)
@ -14,8 +15,6 @@ import (
func New(contextName string) (*client.Client, error) {
var clientOpts []client.Opt
clientOpts = append(clientOpts, client.WithTimeout(3*time.Second))
if contextName != "default" {
context, err := GetContext(contextName)
if err != nil {
@ -27,11 +26,12 @@ func New(contextName string) (*client.Client, error) {
return nil, err
}
helper := newConnectionHelper(ctxEndpoint)
helper := commandconnPkg.NewConnectionHelper(ctxEndpoint)
httpClient := &http.Client{
// No tls, no proxy
Transport: &http.Transport{
DialContext: helper.Dialer,
DialContext: helper.Dialer,
IdleConnTimeout: 30 * time.Second,
},
}

View File

@ -4,6 +4,7 @@ import (
"errors"
"fmt"
commandconnPkg "coopcloud.tech/abra/pkg/upstream/commandconn"
command "github.com/docker/cli/cli/command"
dConfig "github.com/docker/cli/cli/config"
context "github.com/docker/cli/cli/context"
@ -44,7 +45,7 @@ func createContext(name string, host string) error {
Endpoints: make(map[string]contextStore.EndpointTLSData),
}
dockerEP, dockerTLS, err := getDockerEndpointMetadataAndTLS(host)
dockerEP, dockerTLS, err := commandconnPkg.GetDockerEndpointMetadataAndTLS(host)
if err != nil {
return err
}

View File

@ -1,11 +1,10 @@
package client
package commandconn
import (
"context"
"net"
"net/url"
commandconnPkg "coopcloud.tech/abra/pkg/client/commandconn"
"github.com/docker/cli/cli/connhelper"
"github.com/docker/cli/cli/connhelper/ssh"
"github.com/docker/cli/cli/context/docker"
@ -20,7 +19,7 @@ import (
//
// ssh://<user>@<host> URL requires Docker 18.09 or later on the remote host.
func GetConnectionHelper(daemonURL string) (*connhelper.ConnectionHelper, error) {
return getConnectionHelper(daemonURL, nil)
return getConnectionHelper(daemonURL, []string{"-o ConnectTimeout=5"})
}
func getConnectionHelper(daemonURL string, sshFlags []string) (*connhelper.ConnectionHelper, error) {
@ -36,7 +35,7 @@ func getConnectionHelper(daemonURL string, sshFlags []string) (*connhelper.Conne
}
return &connhelper.ConnectionHelper{
Dialer: func(ctx context.Context, network, addr string) (net.Conn, error) {
return commandconnPkg.New(ctx, "ssh", append(sshFlags, sp.Args("docker", "system", "dial-stdio")...)...)
return New(ctx, "ssh", append(sshFlags, sp.Args("docker", "system", "dial-stdio")...)...)
},
Host: "http://docker.example.com",
}, nil
@ -46,7 +45,7 @@ func getConnectionHelper(daemonURL string, sshFlags []string) (*connhelper.Conne
return nil, err
}
func newConnectionHelper(daemonURL string) *connhelper.ConnectionHelper {
func NewConnectionHelper(daemonURL string) *connhelper.ConnectionHelper {
helper, err := GetConnectionHelper(daemonURL)
if err != nil {
logrus.Fatal(err)
@ -74,7 +73,7 @@ func getDockerEndpoint(host string) (docker.Endpoint, error) {
return ep, nil
}
func getDockerEndpointMetadataAndTLS(host string) (docker.EndpointMeta, *dCliContextStore.EndpointTLSData, error) {
func GetDockerEndpointMetadataAndTLS(host string) (docker.EndpointMeta, *dCliContextStore.EndpointTLSData, error) {
ep, err := getDockerEndpoint(host)
if err != nil {
return docker.EndpointMeta{}, nil, err