Permalink
Browse files

Uses getRequestID() only when the server is sending a response.

  • Loading branch information...
1 parent 59b5dd8 commit 63cd1142effcc431766a1d63e382059150394705 John Mac committed with Jun 21, 2010
Showing with 18 additions and 9 deletions.
  1. +10 −7 mongo/collection.go
  2. +5 −1 mongo/cursor.go
  3. +3 −1 mongo/main.go
View
17 mongo/collection.go
@@ -7,6 +7,7 @@ package mongo
import (
"bytes"
"os"
+ "rand"
)
@@ -33,17 +34,19 @@ func (self *Collection) fullName() string {
// *** Client Request Messages
// ***
-func (self *Connection) writeOp(m message) os.Error {
+func (self *Connection) sendMessage(m message) os.Error {
body := m.Bytes()
- h := header(msgHeader{int32(len(body) + _HEADER_SIZE), getRequestID(), 0, m.OpCode()})
+ h := header(msgHeader{int32(len(body) + _HEADER_SIZE), rand.Int31(), 0, m.OpCode()})
msg := bytes.Add(h, body)
_, err := self.conn.Write(msg)
return err
}
-func (self *Connection) writeOpQuery(m message, reqID int32) os.Error {
+// To use with messages that receive a response from database
+// 'opQuery', 'opGetMore'.
+func (self *Connection) sendMessageToReply(m message, reqID int32) os.Error {
body := m.Bytes()
h := header(msgHeader{int32(len(body) + _HEADER_SIZE), reqID, 0, m.OpCode()})
@@ -82,14 +85,14 @@ func (self *Collection) UpsertAll(selector, document BSON) os.Error {
}
func (self *Collection) update(msg *opUpdate) os.Error {
- return self.db.Conn.writeOp(msg)
+ return self.db.Conn.sendMessage(msg)
}
// *** OP_INSERT
func (self *Collection) Insert(doc BSON) os.Error {
msg := &opInsert{self.fullName(), doc}
- return self.db.Conn.writeOp(msg)
+ return self.db.Conn.sendMessage(msg)
}
// *** OP_QUERY
@@ -99,7 +102,7 @@ func (self *Collection) Query(query BSON, skip, limit int32) (*Cursor, os.Error)
reqID := getRequestID()
msg := &opQuery{o_NONE, self.fullName(), skip, limit, query}
- if err := conn.writeOpQuery(msg, reqID); err != nil {
+ if err := conn.sendMessageToReply(msg, reqID); err != nil {
return nil, err
}
@@ -161,7 +164,7 @@ func (self *Collection) RemoveFirst(selector BSON) os.Error {
}
func (self *Collection) remove(msg *opDelete) os.Error {
- return self.db.Conn.writeOp(msg)
+ return self.db.Conn.sendMessage(msg)
}
View
6 mongo/cursor.go
@@ -51,16 +51,20 @@ func (self *Cursor) GetMore() os.Error {
}
conn := self.collection.db.Conn
+ reqID := getRequestID()
msg := &opGetMore{self.collection.fullName(), 0, self.id}
- if err := conn.writeOp(msg); err != nil {
+ if err := conn.sendMessageToReply(msg, reqID); err != nil {
return err
}
reply, err := conn.readReply()
if err != nil {
return err
}
+ if reply.responseTo != reqID {
+ return os.NewError("wrong responseTo code")
+ }
self.pos = 0
self.docs = reply.documents
View
4 mongo/main.go
@@ -41,7 +41,9 @@ func init() {
// ***
/* Gets a random request identifier different to the last one.
- */
+
+To check anytime the server is sending a response ('opQuery', 'opGetMore').
+*/
func getRequestID() int32 {
id := rand.Int31()

0 comments on commit 63cd114

Please sign in to comment.