Closed
Description
When mongodb is offline, the client will be blocked in the function cluster.AcquireSocket. Can you add a timeout for this situation? For example, if the client cannot get a live socket in 5s, it will return an error. Thank you.
To reproduce the problem:
- Compile the following program.
- Run the program
- Login to your DB server, and execute the following commands, please replace $CLIENT_IP with your IP:
sudo iptables -I INPUT -p tcp --dport 27017 -s $CLIENT_IP -j REJECT --reject-with tcp-reset - Check the output of the program, there is no '-------------ERROR is blah blah---------------' or '-------------OK---------------' printed.
- Don't forget to delete the iptables commands:
sudo iptables -D INPUT 1
Codes:
package main
import (
"log"
"os"
"time"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
)
type barDoc struct {
bar string `bson:"bar"`
}
func main() {
var logger = log.New(os.Stderr, "", log.LstdFlags)
mgo.SetLogger(logger)
mgo.SetDebug(true)
url := "user:password@IP:27017/foodb"
sess, err := mgo.Dial(url)
if err != nil {
panic(err)
}
for {
var results []*barDoc
s := sess.Copy()
col := sess.DB("foodb").C("bars")
iter := col.Find(bson.M{}).Iter()
err = iter.All(&results)
if err != nil {
logger.Printf("-------------ERROR is %s---------------", err)
} else {
logger.Printf("-------------OK---------------")
}
iter.Close()
s.Close()
time.Sleep(time.Second)
}
}
Metadata
Metadata
Assignees
Labels
No labels