Skip to content

Commit 7356bca

Browse files
committed
chore: enable and fix noctx issues
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
1 parent f346339 commit 7356bca

12 files changed

Lines changed: 26 additions & 15 deletions

File tree

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ linters:
1212
- ineffassign
1313
- misspell
1414
- modernize
15+
- noctx
1516
- revive
1617
- unconvert
1718
- unused

agent/testutils/fakes.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,17 +233,18 @@ func NewMockDispatcher(t *testing.T, secConfig *ca.SecurityConfig, local bool) (
233233
addr string
234234
cleanup func()
235235
)
236+
lc := &net.ListenConfig{}
236237
if local {
237238
tempDir, err := os.MkdirTemp("", "local-dispatcher-socket")
238239
require.NoError(t, err)
239240
addr = filepath.Join(tempDir, "socket")
240-
l, err = net.Listen("unix", addr)
241+
l, err = lc.Listen(t.Context(), "unix", addr)
241242
require.NoError(t, err)
242243
cleanup = func() {
243244
os.RemoveAll(tempDir)
244245
}
245246
} else {
246-
l, err = net.Listen("tcp", "127.0.0.1:0")
247+
l, err = lc.Listen(t.Context(), "tcp", "127.0.0.1:0")
247248
require.NoError(t, err)
248249
addr = l.Addr().String()
249250
}

ca/testutils/cautils.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,19 @@ func newTestCA(t *testing.T, tempBaseDir string, apiRootCA api.RootCA, krwGenera
154154
}
155155

156156
var (
157+
ctx context.Context
157158
externalSigningServer *ExternalSigningServer
158159
externalCAs []*api.ExternalCA
159160
err error
160161
rootCA ca.RootCA
161162
)
162163

164+
if t != nil {
165+
ctx = t.Context()
166+
} else {
167+
ctx = context.Background()
168+
}
169+
163170
if apiRootCA.RootRotation != nil {
164171
rootCA, err = ca.NewRootCA(
165172
apiRootCA.CACert, apiRootCA.RootRotation.CACert, apiRootCA.RootRotation.CAKey, ca.DefaultNodeCertExpiration, apiRootCA.RootRotation.CrossSignedCACert)
@@ -230,7 +237,7 @@ func newTestCA(t *testing.T, tempBaseDir string, apiRootCA api.RootCA, krwGenera
230237
assert.NoError(t, err)
231238
}
232239

233-
l, err := net.Listen("tcp", "127.0.0.1:0")
240+
l, err := (&net.ListenConfig{}).Listen(ctx, "tcp", "127.0.0.1:0")
234241
if t != nil {
235242
assert.NoError(t, err)
236243
}

ca/transport.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (c *MutableTLSCreds) ClientHandshake(ctx context.Context, addr string, rawC
8787
var err error
8888
errChannel := make(chan error, 1)
8989
go func() {
90-
errChannel <- conn.Handshake()
90+
errChannel <- conn.HandshakeContext(ctx)
9191
}()
9292
select {
9393
case err = <-errChannel:
@@ -106,7 +106,7 @@ func (c *MutableTLSCreds) ServerHandshake(rawConn net.Conn) (net.Conn, credentia
106106
c.Lock()
107107
conn := tls.Server(rawConn, c.config)
108108
c.Unlock()
109-
if err := conn.Handshake(); err != nil {
109+
if err := conn.HandshakeContext(context.Background()); err != nil {
110110
rawConn.Close()
111111
return nil, nil, err
112112
}

cmd/swarm-bench/collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type Collector struct {
2222
// once they come online.
2323
func (c *Collector) Listen(port int) error {
2424
var err error
25-
c.ln, err = net.Listen("tcp", ":"+strconv.Itoa(port))
25+
c.ln, err = (&net.ListenConfig{}).Listen(context.Background(), "tcp", ":"+strconv.Itoa(port))
2626
return err
2727
}
2828

connectionbroker/broker.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package connectionbroker
55

66
import (
7+
"context"
78
"net"
89
"sync"
910
"time"
@@ -69,7 +70,7 @@ func (b *Broker) SelectRemote(dialOpts ...grpc.DialOption) (*Conn, error) {
6970
grpc.WithUnaryInterceptor(grpc_prometheus.UnaryClientInterceptor),
7071
grpc.WithStreamInterceptor(grpc_prometheus.StreamClientInterceptor),
7172
grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
72-
return net.DialTimeout("tcp", addr, timeout)
73+
return (&net.Dialer{Timeout: timeout}).DialContext(context.Background(), "tcp", addr)
7374
}))
7475

7576
cc, err := grpc.Dial(peer.Addr, dialOpts...)

manager/controlapi/ca_rotation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func validateExternalCAURL(dialer *net.Dialer, tlsOpts *tls.Config, caURL string
117117
port = "443"
118118
}
119119

120-
conn, err := tls.DialWithDialer(dialer, "tcp", net.JoinHostPort(host, port), tlsOpts)
120+
conn, err := (&tls.Dialer{NetDialer: dialer, Config: tlsOpts}).DialContext(context.Background(), "tcp", net.JoinHostPort(host, port))
121121
if conn != nil {
122122
conn.Close()
123123
}

manager/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ func (m *Manager) BindRemote(ctx context.Context, addrs RemoteAddrs) error {
401401
advertiseAddr = net.JoinHostPort("0.0.0.0", advertiseAddrPort)
402402
}
403403

404-
l, err := net.Listen("tcp", addrs.ListenAddr)
404+
l, err := (&net.ListenConfig{}).Listen(context.Background(), "tcp", addrs.ListenAddr)
405405
if err != nil {
406406
return errors.Wrap(err, "failed to listen on remote API address")
407407
}

manager/state/raft/testutils/testutils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ var _ raft.EncryptionKeyRotator = NewSimpleKeyRotator(raft.EncryptionKeys{})
290290

291291
// NewNode creates a new raft node to use for tests
292292
func NewNode(t *testing.T, clockSource *fakeclock.FakeClock, tc *cautils.TestCA, opts ...raft.NodeOptions) *TestNode {
293-
l, err := net.Listen("tcp", "127.0.0.1:0")
293+
l, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0")
294294
require.NoError(t, err, "can't bind to raft service port")
295295
wrappedListener := NewWrappedListener(l)
296296

manager/state/raft/transport/transport.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,8 @@ func (t *Transport) dial(addr string) (*grpc.ClientConn, error) {
351351
// gRPC dialer connects to proxy first. Provide a custom dialer here avoid that.
352352
// TODO(anshul) Add an option to configure this.
353353
grpcOptions = append(grpcOptions,
354-
grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
355-
return net.DialTimeout("tcp", addr, timeout)
354+
grpc.WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) {
355+
return (&net.Dialer{}).DialContext(ctx, "tcp", addr)
356356
}))
357357

358358
// TODO(dperny): this changes the max received message size for outgoing

0 commit comments

Comments
 (0)