Skip to content

Blocked in function cluster.AcquireSocket #134

Closed
@dackon

Description

@dackon

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:

  1. Compile the following program.
  2. Run the program
  3. 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
  4. Check the output of the program, there is no '-------------ERROR is blah blah---------------' or '-------------OK---------------' printed.
  5. 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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions