Skip to content

Commit 2ab94d1

Browse files
committed
1 parent 5f63f1b commit 2ab94d1

File tree

2 files changed

+65
-36
lines changed

2 files changed

+65
-36
lines changed

x/mongo/driver/topology/server.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"context"
1111
"errors"
1212
"fmt"
13+
"log"
1314
"net"
1415
"sync"
1516
"sync/atomic"
@@ -554,7 +555,9 @@ func (s *Server) update() {
554555
defer s.processErrorLock.Unlock()
555556

556557
s.updateDescription(desc)
557-
if lastError := desc.LastError; lastError != nil {
558+
lastError := desc.LastError
559+
log.Println("lastError", lastError)
560+
if lastError != nil {
558561
// Retry after the first timeout in case of a FAAS pause as described in GODRIVER-2577.
559562
if err := unwrapConnectionError(lastError); err != nil {
560563
err, ok := err.(net.Error)

x/mongo/driver/topology/server_test.go

Lines changed: 61 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@ package topology
1111

1212
import (
1313
"context"
14+
"crypto/tls"
15+
"crypto/x509"
1416
"errors"
17+
"fmt"
18+
"io/ioutil"
19+
"log"
1520
"net"
21+
"os"
1622
"runtime"
1723
"sync"
1824
"sync/atomic"
@@ -54,14 +60,14 @@ type errorQueue struct {
5460
mutex sync.Mutex
5561
}
5662

57-
func (eq *errorQueue) head() error {
58-
eq.mutex.Lock()
59-
defer eq.mutex.Unlock()
60-
if len(eq.errors) > 0 {
61-
return eq.errors[0]
62-
}
63-
return nil
64-
}
63+
// func (eq *errorQueue) head() error {
64+
// eq.mutex.Lock()
65+
// defer eq.mutex.Unlock()
66+
// if len(eq.errors) > 0 {
67+
// return eq.errors[0]
68+
// }
69+
// return nil
70+
// }
6571

6672
func (eq *errorQueue) dequeue() bool {
6773
eq.mutex.Lock()
@@ -73,78 +79,95 @@ func (eq *errorQueue) dequeue() bool {
7379
return false
7480
}
7581

82+
/*
7683
type timeoutConn struct {
7784
net.Conn
7885
errors *errorQueue
7986
}
8087
8188
func (c *timeoutConn) Read(b []byte) (int, error) {
82-
n, err := 0, c.errors.head()
83-
if err == nil {
84-
n, err = c.Conn.Read(b)
85-
}
89+
//n, err := 0, c.errors.head()
90+
//if err == nil {
91+
n, err := c.Conn.Read(b)
92+
//}
93+
log.Println("read", n, err)
8694
return n, err
8795
}
8896
8997
func (c *timeoutConn) Write(b []byte) (int, error) {
90-
n, err := 0, c.errors.head()
91-
if err == nil {
92-
n, err = c.Conn.Write(b)
93-
}
98+
//n, err := 0, c.errors.head()
99+
//if err == nil {
100+
n, err := c.Conn.Write(b)
101+
//}
102+
log.Println("write", n, err)
94103
return n, err
95104
}
105+
*/
96106

97107
type timeoutDialer struct {
98108
Dialer
99109
errors *errorQueue
100110
}
101111

102112
func (d *timeoutDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
103-
var dialer net.Dialer
104-
c, e := dialer.DialContext(ctx, network, address)
105-
return &timeoutConn{c, d.errors}, e
106-
}
113+
c, e := d.Dialer.DialContext(ctx, network, address)
114+
115+
caFile := os.Getenv("MONGO_GO_DRIVER_CA_FILE")
116+
log.Println("dial", caFile)
117+
if len(caFile) > 0 {
118+
pem, err := ioutil.ReadFile(caFile)
119+
if err != nil {
120+
return nil, err
121+
}
107122

108-
type timeoutErr struct {
109-
net.UnknownNetworkError
110-
}
123+
ca := x509.NewCertPool()
124+
if !ca.AppendCertsFromPEM(pem) {
125+
return nil, fmt.Errorf("unable to load CA file")
126+
}
111127

112-
func (e *timeoutErr) Timeout() bool {
113-
return true
128+
config := &tls.Config{
129+
InsecureSkipVerify: true,
130+
RootCAs: ca,
131+
}
132+
c = tls.Client(c, config)
133+
}
134+
return c, e
114135
}
115136

116-
var timeout = &timeoutErr{"test timeout"}
117-
118137
// TestServerHeartbeatTimeout tests timeout retry for GODRIVER-2577.
119138
func TestServerHeartbeatTimeout(t *testing.T) {
139+
networkTimeoutError := &net.DNSError{
140+
IsTimeout: true,
141+
}
142+
120143
testCases := []struct {
121144
desc string
122145
ioErrors []error
123146
expectPoolCleared bool
124147
}{
125148
{
126149
desc: "one single timeout should not clear the pool",
127-
ioErrors: []error{nil, timeout, nil, timeout, nil},
150+
ioErrors: []error{nil, networkTimeoutError, nil, networkTimeoutError, nil},
128151
expectPoolCleared: false,
129152
},
130-
{
131-
desc: "continuous timeouts should clear the pool",
132-
ioErrors: []error{nil, timeout, timeout, nil},
133-
expectPoolCleared: true,
134-
},
153+
// {
154+
// desc: "continuous timeouts should clear the pool",
155+
// ioErrors: []error{nil, networkTimeoutError, networkTimeoutError, nil},
156+
// expectPoolCleared: true,
157+
// },
135158
}
136159
for _, tc := range testCases {
137160
tc := tc
138161
t.Run(tc.desc, func(t *testing.T) {
139-
t.Parallel()
162+
//t.Parallel()
140163

141164
var wg sync.WaitGroup
142165
wg.Add(1)
143166

144167
errors := &errorQueue{errors: tc.ioErrors}
145168
tpm := monitor.NewTestPoolMonitor()
146169
server := NewServer(
147-
address.Address("localhost:27017"),
170+
address.Address("localhost"),
148171
primitive.NewObjectID(),
149172
WithConnectionPoolMonitor(func(*event.PoolMonitor) *event.PoolMonitor {
150173
return tpm.PoolMonitor
@@ -165,6 +188,9 @@ func TestServerHeartbeatTimeout(t *testing.T) {
165188
},
166189
}
167190
}),
191+
WithHeartbeatInterval(func(time.Duration) time.Duration {
192+
return 3 * time.Second
193+
}),
168194
)
169195
require.NoError(t, server.Connect(nil))
170196
wg.Wait()

0 commit comments

Comments
 (0)