Skip to content

Commit 2e16943

Browse files
authored
chore: Add context timeout to GetLocalIP (#1536)
* chore(golangci): fix noctx issue to use (*net.Dialer).DialContext See, $ golangci-lint run ./... internal/util/net.go:6:23: net.Dial must not be called. use (*net.Dialer).DialContext (noctx) conn, err := net.Dial("udp", "8.8.8.8:80") ^ 1 issues: * noctx: 1 Signed-off-by: Mario Trangoni <[email protected]> * chore(golangci): Upgrade golangci from v2.2.2 to v2.4.0 Signed-off-by: Mario Trangoni <[email protected]> --------- Signed-off-by: Mario Trangoni <[email protected]>
1 parent f9ff593 commit 2e16943

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ CRD_REF_DOCS = $(LOCALBIN)/crd-ref-docs-$(CRD_REF_DOCS_VERSION)
4747
KUSTOMIZE_VERSION ?= v5.6.0
4848
CONTROLLER_TOOLS_VERSION ?= v0.17.2
4949
ENVTEST_VERSION ?= release-0.17
50-
GOLANGCI_LINT_VERSION ?= v2.2.2
50+
GOLANGCI_LINT_VERSION ?= v2.4.0
5151
KUTTL_VERSION ?= 0.15.0
5252
KIND_VERSION ?= v0.24.0
5353
CRD_REF_DOCS_VERSION ?= v0.0.12

internal/util/net.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
package util
22

3-
import "net"
3+
import (
4+
"context"
5+
"net"
6+
"time"
7+
)
48

59
func GetLocalIP() (string, error) {
6-
conn, err := net.Dial("udp", "8.8.8.8:80")
10+
dialer := net.Dialer{}
11+
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
12+
defer cancel()
13+
14+
conn, err := dialer.DialContext(ctx, "udp", "8.8.8.8:80")
715
if err != nil {
816
return "", err
917
}

0 commit comments

Comments
 (0)