forked from mongodb/mongo-go-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
98 lines (84 loc) · 2.51 KB
/
connection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package topology
import (
"context"
"net"
"strings"
"go.mongodb.org/mongo-driver/x/network/command"
"go.mongodb.org/mongo-driver/x/network/connection"
"go.mongodb.org/mongo-driver/x/network/description"
"go.mongodb.org/mongo-driver/x/network/wiremessage"
)
// sconn is a wrapper around a connection.Connection. This type is returned by
// a Server so that it can track network errors and when a non-timeout network
// error is returned, the pool on the server can be cleared.
type sconn struct {
connection.Connection
s *Server
id uint64
}
var notMasterCodes = []int32{10107, 13435}
var recoveringCodes = []int32{11600, 11602, 13436, 189, 91}
func (sc *sconn) ReadWireMessage(ctx context.Context) (wiremessage.WireMessage, error) {
wm, err := sc.Connection.ReadWireMessage(ctx)
if err != nil {
sc.processErr(err)
} else {
e := command.DecodeError(wm)
sc.processErr(e)
}
return wm, err
}
func (sc *sconn) WriteWireMessage(ctx context.Context, wm wiremessage.WireMessage) error {
err := sc.Connection.WriteWireMessage(ctx, wm)
sc.processErr(err)
return err
}
func (sc *sconn) processErr(err error) {
// Invalidate server description if not master or node recovering error occurs
if cerr, ok := err.(command.Error); ok && (isRecoveringError(cerr) || isNotMasterError(cerr)) {
desc := sc.s.Description()
desc.Kind = description.Unknown
desc.LastError = err
// updates description to unknown
sc.s.updateDescription(desc, false)
sc.s.RequestImmediateCheck()
_ = sc.s.pool.Drain()
return
}
ne, ok := err.(connection.NetworkError)
if !ok {
return
}
if netErr, ok := ne.Wrapped.(net.Error); ok && netErr.Timeout() {
return
}
if ne.Wrapped == context.Canceled || ne.Wrapped == context.DeadlineExceeded {
return
}
desc := sc.s.Description()
desc.Kind = description.Unknown
desc.LastError = err
// updates description to unknown
sc.s.updateDescription(desc, false)
}
func isRecoveringError(err command.Error) bool {
for _, c := range recoveringCodes {
if c == err.Code {
return true
}
}
return strings.Contains(err.Error(), "node is recovering")
}
func isNotMasterError(err command.Error) bool {
for _, c := range notMasterCodes {
if c == err.Code {
return true
}
}
return strings.Contains(err.Error(), "not master")
}