@@ -37,7 +37,7 @@ type connection struct {
37
37
nc net.Conn // When nil, the connection is closed.
38
38
addr address.Address
39
39
idleTimeout time.Duration
40
- idleDeadline time.Time
40
+ idleDeadline atomic. Value // Stores a time.Time
41
41
lifetimeDeadline time.Time
42
42
readTimeout time.Duration
43
43
writeTimeout time.Duration
@@ -87,6 +87,7 @@ func newConnection(ctx context.Context, addr address.Address, opts ...Connection
87
87
// connect handles the I/O for a connection. It will dial, configure TLS, and perform
88
88
// initialization handshakes.
89
89
func (c * connection ) connect (ctx context.Context ) {
90
+
90
91
if ! atomic .CompareAndSwapInt32 (& c .connected , initialized , connected ) {
91
92
return
92
93
}
@@ -151,8 +152,10 @@ func (c *connection) connect(ctx context.Context) {
151
152
}
152
153
}
153
154
154
- func (c * connection ) connectWait () error {
155
- <- c .connectDone
155
+ func (c * connection ) wait () error {
156
+ if c .connectDone != nil {
157
+ <- c .connectDone
158
+ }
156
159
return c .connectErr
157
160
}
158
161
@@ -259,7 +262,11 @@ func (c *connection) close() error {
259
262
return nil
260
263
}
261
264
if c .pool == nil {
262
- err := c .nc .Close ()
265
+ var err error
266
+
267
+ if c .nc != nil {
268
+ err = c .nc .Close ()
269
+ }
263
270
atomic .StoreInt32 (& c .connected , disconnected )
264
271
return err
265
272
}
@@ -268,7 +275,8 @@ func (c *connection) close() error {
268
275
269
276
func (c * connection ) expired () bool {
270
277
now := time .Now ()
271
- if ! c .idleDeadline .IsZero () && now .After (c .idleDeadline ) {
278
+ idleDeadline , ok := c .idleDeadline .Load ().(time.Time )
279
+ if ok && now .After (idleDeadline ) {
272
280
return true
273
281
}
274
282
@@ -281,7 +289,7 @@ func (c *connection) expired() bool {
281
289
282
290
func (c * connection ) bumpIdleDeadline () {
283
291
if c .idleTimeout > 0 {
284
- c .idleDeadline = time .Now ().Add (c .idleTimeout )
292
+ c .idleDeadline . Store ( time .Now ().Add (c .idleTimeout ) )
285
293
}
286
294
}
287
295
0 commit comments