forked from mongodb/mongo-go-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
52 lines (43 loc) · 1.33 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
// 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"
"github.com/mongodb/mongo-go-driver/core/connection"
"github.com/mongodb/mongo-go-driver/core/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
}
func (sc *sconn) ReadWireMessage(ctx context.Context) (wiremessage.WireMessage, error) {
wm, err := sc.Connection.ReadWireMessage(ctx)
sc.processErr(err)
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) {
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
}
_ = sc.s.Drain()
}