Skip to content

Commit 725de01

Browse files
committed
Timeout isDockerDesktopRunning and memoize it for creation of http transport
Signed-off-by: Guillaume Tardif <guillaume.tardif@gmail.com>
1 parent 32ebaf5 commit 725de01

5 files changed

Lines changed: 27 additions & 4 deletions

File tree

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ require (
4242
github.com/gorilla/websocket v1.5.3
4343
github.com/junegunn/fzf v0.70.0
4444
github.com/k3a/html2text v1.4.0
45+
github.com/kofalt/go-memoize v0.0.0-20240506050413-9e5eb99a0f2a
4546
github.com/labstack/echo/v4 v4.15.1
4647
github.com/mattn/go-isatty v0.0.20
4748
github.com/mattn/go-runewidth v0.0.21
@@ -183,6 +184,7 @@ require (
183184
github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90 // indirect
184185
github.com/opencontainers/go-digest v1.0.0 // indirect
185186
github.com/opencontainers/image-spec v1.1.1 // indirect
187+
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
186188
github.com/perimeterx/marshmallow v1.1.5 // indirect
187189
github.com/pjbgf/sha1cd v0.3.2 // indirect
188190
github.com/pmezard/go-difflib v1.0.0 // indirect

go.sum

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,8 @@ github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4
311311
github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
312312
github.com/klauspost/compress v1.18.4 h1:RPhnKRAQ4Fh8zU2FY/6ZFDwTVTxgJ/EMydqSTzE9a2c=
313313
github.com/klauspost/compress v1.18.4/go.mod h1:R0h/fSBs8DE4ENlcrlib3PsXS61voFxhIs2DeRhCvJ4=
314+
github.com/kofalt/go-memoize v0.0.0-20240506050413-9e5eb99a0f2a h1:yyeZ0oZLWgSakB9QzPuL/Kyx9kcXYblDOswXaOEx0tg=
315+
github.com/kofalt/go-memoize v0.0.0-20240506050413-9e5eb99a0f2a/go.mod h1:EUxMohcCc4AiiO1SImzCQo3EdrEYj9Xkyrxbepg02nQ=
314316
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
315317
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
316318
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
@@ -385,6 +387,8 @@ github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8
385387
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
386388
github.com/opencontainers/image-spec v1.1.1 h1:y0fUlFfIZhPF1W537XOLg0/fcx6zcHCJwooC2xJA040=
387389
github.com/opencontainers/image-spec v1.1.1/go.mod h1:qpqAh3Dmcf36wStyyWU+kCeDgrGnAve2nCC8+7h8Q0M=
390+
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
391+
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
388392
github.com/perimeterx/marshmallow v1.1.5 h1:a2LALqQ1BlHM8PZblsDdidgv1mWi1DgC2UmX50IvK2s=
389393
github.com/perimeterx/marshmallow v1.1.5/go.mod h1:dsXbUu8CRzfYP5a87xpp0xq9S3u0Vchtcl8we9tYaXw=
390394
github.com/pjbgf/sha1cd v0.3.2 h1:a9wb0bp1oC2TGwStyn0Umc/IGKQnEgF0vVaZ8QF8eo4=
@@ -431,10 +435,13 @@ github.com/skeema/knownhosts v1.3.1 h1:X2osQ+RAjK76shCbvhHHHVl3ZlgDm8apHEHFqRjnB
431435
github.com/skeema/knownhosts v1.3.1/go.mod h1:r7KTdC8l4uxWRyK2TpQZ/1o5HaSzh06ePQNxPwTcfiY=
432436
github.com/smacker/go-tree-sitter v0.0.0-20240827094217-dd81d9e9be82 h1:6C8qej6f1bStuePVkLSFxoU22XBS165D3klxlzRg8F4=
433437
github.com/smacker/go-tree-sitter v0.0.0-20240827094217-dd81d9e9be82/go.mod h1:xe4pgH49k4SsmkQq5OT8abwhWmnzkhpgnXeekbx2efw=
434-
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
435438
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
439+
github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs=
440+
github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo=
436441
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
437442
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
443+
github.com/smartystreets/gunit v1.4.2 h1:tyWYZffdPhQPfK5VsMQXfauwnJkqg7Tv5DLuQVYxq3Q=
444+
github.com/smartystreets/gunit v1.4.2/go.mod h1:ZjM1ozSIMJlAz/ay4SG8PeKF00ckUp+zMHZXV9/bvak=
438445
github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
439446
github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
440447
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=

pkg/desktop/running.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ package desktop
22

33
import (
44
"context"
5+
"time"
56
)
67

78
func IsDockerDesktopRunning(ctx context.Context) bool {
9+
ctx, cancel := context.WithTimeout(ctx, time.Second*3)
10+
defer cancel()
811
err := ClientBackend.Get(ctx, "/ping", nil)
912
return err == nil
1013
}

pkg/desktop/sockets_linux.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func getDockerDesktopPaths() (DockerDesktopPaths, error) {
2424
// Inside WSL2
2525
return DockerDesktopPaths{
2626
BackendSocket: "/mnt/wsl/docker-desktop/shared-sockets/host-services/backend.sock",
27+
ProxySocket: "/mnt/wsl/docker-desktop/shared-sockets/host-services/httpproxy.sock",
2728
}, nil
2829
}
2930

@@ -39,5 +40,6 @@ func getDockerDesktopPaths() (DockerDesktopPaths, error) {
3940
// On Linux
4041
return DockerDesktopPaths{
4142
BackendSocket: filepath.Join(home, ".docker", "desktop", "backend.sock"),
43+
ProxySocket: filepath.Join(home, ".docker", "desktop", "httpproxy.sock"),
4244
}, nil
4345
}

pkg/remote/transport.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@ import (
55
"net"
66
"net/http"
77
"net/url"
8+
"time"
9+
10+
"github.com/kofalt/go-memoize"
811

912
"github.com/docker/docker-agent/pkg/desktop"
1013
socket "github.com/docker/docker-agent/pkg/desktop/socket"
1114
)
1215

16+
var memoizer = memoize.NewMemoizer(1*time.Minute, 1*time.Minute)
17+
1318
// NewTransport returns an HTTP transport that uses Docker Desktop proxy if available.
1419
func NewTransport(ctx context.Context) http.RoundTripper {
1520
t, ok := http.DefaultTransport.(*http.Transport)
@@ -18,9 +23,13 @@ func NewTransport(ctx context.Context) http.RoundTripper {
1823
}
1924
transport := t.Clone()
2025

21-
if desktop.IsDockerDesktopRunning(ctx) {
22-
// Route all traffic through Docker Desktop's HTTP proxy socket
23-
// Set a dummy proxy URL - the actual connection happens via DialContext
26+
desktopRunning, err, _ := memoizer.Memoize("desktopRunning", func() (any, error) {
27+
return desktop.IsDockerDesktopRunning(context.Background()), nil
28+
})
29+
if err != nil {
30+
return transport
31+
}
32+
if running, ok := desktopRunning.(bool); ok && running {
2433
transport.Proxy = http.ProxyURL(&url.URL{
2534
Scheme: "http",
2635
})

0 commit comments

Comments
 (0)