/
mongo_conn.go
163 lines (144 loc) · 3.75 KB
/
mongo_conn.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package mongoproxy
import (
"errors"
"io"
"net"
"time"
)
// TODO add time outs
// Look at http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/ for the protocol.
const (
opReply = 1
opMessage = 1000
opUpdate = 2001
opInsert = 2002
reserved = 2003
opQuery = 2004
opGetMore = 2005
opDelete = 2006
opKillCursors = 2007
)
// MsgHeader is the mongo MessageHeader
type MsgHeader struct {
// MessageLength is the total message size, including this header
MessageLength int32
// RequestID is the identifier for this miessage
RequestID int32
// ResponseTo is the RequestID of the message being responded to. used in DB responses
ResponseTo int32
// OpCode is the request type, see consts above.
OpCode int32
}
// WaitForResponse tells if mongo will reply to this message
func (m *MsgHeader) WaitForResponse() bool {
return m.OpCode == opInsert || m.OpCode == opUpdate || m.OpCode == opDelete
}
// ToWire converts the MsgHeader to the wire protocol
func (m *MsgHeader) ToWire() []byte {
b := make([]byte, 16)
setInt32(b, 0, m.MessageLength)
setInt32(b, 4, m.RequestID)
setInt32(b, 8, m.ResponseTo)
setInt32(b, 12, m.OpCode)
return b
}
// FromWire reads the wirebytes into this object
func (m *MsgHeader) FromWire(b []byte) {
m.MessageLength = getInt32(b, 0)
m.RequestID = getInt32(b, 4)
m.ResponseTo = getInt32(b, 8)
m.OpCode = getInt32(b, 12)
}
// MongoConn provides operations on top of a mongo connection.
type MongoConn struct {
conn net.Conn
// ReadWriteTimeout sets the read and write timeouets associated
// with the connection
ReadWriteTimeout time.Duration
}
// NewMongoConn creates a mongo connection using the provided connection
func NewMongoConn(c net.Conn) *MongoConn {
return &MongoConn{conn: c}
}
// ReadHeader reads the MsgHeader from the mongo connection
func (m *MongoConn) ReadHeader() (*MsgHeader, error) {
m.setDeadline()
b := make([]byte, 16)
if err := fill(m.conn, b); err != nil {
return nil, err
}
h := MsgHeader{}
h.FromWire(b)
return &h, nil
}
var errWrite = errors.New("incorrect number of bytes written")
// WriteHeader writes a mongo MsgHeader into this connection
func (m *MongoConn) WriteHeader(h *MsgHeader) error {
m.setDeadline()
b := h.ToWire()
n, err := m.conn.Write(b)
if err != nil {
return err
}
if n != len(b) {
return errWrite
}
return nil
}
// CopyResponse copies one entire response from the given connection.
func (m *MongoConn) CopyResponse(fromConn *MongoConn) error {
m.setDeadline()
header, err := fromConn.ReadHeader()
if err != nil {
return err
}
if err := m.WriteHeader(header); err != nil {
return err
}
return m.CopyN(fromConn, int64(header.MessageLength-16))
}
// CopyN copies n bytes from the fromConn into this connection.
func (m *MongoConn) CopyN(fromConn *MongoConn, n int64) error {
m.setDeadline()
written, err := io.CopyN(m.conn, fromConn.conn, n)
if err != nil {
return err
}
if written != n {
return errWrite
}
return nil
}
func (m *MongoConn) setDeadline() {
if m.ReadWriteTimeout != 0 {
m.conn.SetDeadline(time.Now().Add(m.ReadWriteTimeout))
}
}
// Close closes the connection
func (m *MongoConn) Close() error {
return m.conn.Close()
}
// all data in the MongoDB wire protocol is little-endian.
// all the read/write functions below are little-endian.
func getInt32(b []byte, pos int) int32 {
return (int32(b[pos+0])) |
(int32(b[pos+1]) << 8) |
(int32(b[pos+2]) << 16) |
(int32(b[pos+3]) << 24)
}
func fill(r net.Conn, b []byte) error {
l := len(b)
n, err := r.Read(b)
for n != l && err == nil {
var ni int
ni, err = r.Read(b[n:])
n += ni
}
return err
}
func setInt32(b []byte, pos int, i int32) {
b[pos] = byte(i)
b[pos+1] = byte(i >> 8)
b[pos+2] = byte(i >> 16)
b[pos+3] = byte(i >> 24)
}