I am writing an application which writes to the mongodb rapidly. Too rapidly for mongodb and mgo to handle. My question is, is there a way for me to determine that mongo cannot keep up and start to block? But I also do not want to block unnecessarily. Here is a sample of code that emulates the problem:
package main
import (
"labix.org/v2/mgo"
"time"
"fmt"
)
// in database name is a string and age is an int
type Dog struct{
Breed string "breed"
}
type Person struct{
Name string "name"
Pet Dog `bson:",inline"`
Ts time.Time
}
func insert(session *mgo.Session, bob Person){
err := session.DB("db_log").C("people").Insert(&bob)
if err != nil {
panic("Could not insert into database")
}
}
func main() {
session, _ := mgo.Dial("localhost:27017")
bob := Person{Name : "Robert", Pet : Dog{}}
i := 0
for {
time.Sleep(time.Duration(1) * time.Microsecond)
i++
go insert(session, bob)
}
}
I often get errors like:
panic: Could not insert into database
or
panic: write tcp 127.0.0.1:27017: i/o timeout