[GODRIVER-503] mongo.Connect is always succeeding even the db is not up Created: 18/Jul/18  Updated: 27/Oct/23  Resolved: 31/Aug/18

Status: Closed
Project: Go Driver
Component/s: Connections, Error Handling
Affects Version/s: 0.0.7
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: jeyanthi Assignee: Kristofer Brandow (Inactive)
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

 

Gopkg.lock
 
[[projects]]
  name = "github.com/mongodb/mongo-go-driver"
  packages = [
    "bson",
    "bson/builder",
    "bson/decimal",
    "bson/elements",
    "bson/internal/jsonparser",
    "bson/objectid",
    "bson/parser",
    "bson/parser/ast",
    "core/address",
    "core/auth",
    "core/auth/internal/gssapi",
    "core/command",
    "core/connection",
    "core/connstring",
    "core/description",
    "core/dispatch",
    "core/option",
    "core/readconcern",
    "core/readpref",
    "core/result",
    "core/tag",
    "core/topology",
    "core/version",
    "core/wiremessage",
    "core/writeconcern",
    "internal",
    "mongo"
  ]
  revision = "bb548a5e4d4cabb086e6425b113b07270e40bbb8"
  version = "v0.0.7"

 

My golang connection code 

MongoConnect : Mongo Connection clientfunc MongoConnect() (*mongo.Client, error) {    
  MongoConnURL := "mongodb://127.0.0.1:27017"
  ctx := context.Background()    
  client, err := mongo.Connect(ctx, MongoConnURL, nil)
  fmt.Println("connection error is ", err)    
  if err != nil {
    print("Error in creating connection with context", err)        
    return nil, err    
  }
  fmt.Println("Monogo connection success ... \n", client)
  return client, nil
}

Output while mongodb is local is up

connection error is <nil>
Monogo connection success ... &{[] 0xc4201c39e0 {mongodb://127.0.0.1:27017 map[] admin 0 false 0 false 0 false [127.0.0.1:27017] false false 0 false 0 false 0 0 false 0 false false [] 0 false 0 false 0 false false false false <nil> false false false false 0 false 0 false false map[] map[]} 1500000 0 0x1ce6d40 <nil> <nil>} 

 

while mongodb in local is down

connection error is  <nil>
Monogo connection success ...
 &{[] 0xc420354090 {mongodb://127.0.0.1:27017   map[] admin 0 false 0 false  0 false [127.0.0.1:27017] false false 0 false 0 false 0 0 false 0 false  false   [] 0 false  0 false 0 false false false  false <nil> false false false  false  0 false  0 false false map[] map[]} 15000000 0x1ce6d40 <nil> <nil>}



 Comments   
Comment by Kristofer Brandow (Inactive) [ 20/Jul/18 ]

Hi jeyanthi,

If you want to know if mongo.Client.Connect has found an actual server, you send a single ping command. The commands themselves will timeout if a suitable server cannot be found. You don't have to do a ping at all, but if the driver can't find a mongo deployment all of the commands you run will have a server selection timeout.

--Kris

Comment by jeyanthi [ 20/Jul/18 ]

@kris Brandow Thanks for the reply

But in that case , before running every command I need to test ping isn't it ? instead of one stop for connection.

instead you can return some kind of context from connect to make sure database is up and running, and that would do trick isn't it ?

Comment by Kristofer Brandow (Inactive) [ 20/Jul/18 ]

Hi jeyanthi,

The mongo.Client.Connect method doesn't ensure a database has been connected to before returning, it just starts background monitoring threads that will attempt to discover a mongo deployment. If you want to know if a *mongo.Client has successfully connected to a server, you can run a ping command (via mongo.Database.RunCommand) with a context.Context with a low timeout. We will be adding a Ping method to the mongo.Client type, and you can track that via GODRIVER-372. This behavior is consistent with the database/sql package's semantics.

Let me know if you have any other questions and thanks for opening a ticket for this!

--Kris

Comment by jeyanthi [ 18/Jul/18 ]

This is even happening with the latest version

 

Gopkg.lock
 
revision = "4503c54ec045b406a0b3533901c6d7a5614cfef3"
version = "v0.0.9"

Generated at Thu Feb 08 08:34:21 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.