[GODRIVER-1792] Dial TCP failure with concurrent queries Created: 13/Nov/20  Updated: 27/Oct/23  Resolved: 16/Nov/20

Status: Closed
Project: Go Driver
Component/s: Connections, Networking
Affects Version/s: 1.4.1
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Liam Brown Assignee: Unassigned
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 64 bit



 Description   

I have an application that spins up a lot of concurrent queries to the database across multiple threads. To begin with it happily queries and returns successfully within a few milliseconds for each query. However, after a short while of running (approx. 30 seconds), the `collection.Find` command begins returning the following error intermittently:

connection() : dial tcp 127.0.0.1:27017: connectex: Only one usage of each socket address (protocol/network address/port) is normally permitted.

This kind of network error would usually imply to me that it was trying to listen on that socket but the socket was already in use - not sure how that really applies in the network level of the mongo go driver though?

 

Each separate part of the application does its own `mongo.Connect` and then calls `client.Disconnect` when it is completed. There are only ever around a dozen or so clients connected at the same time, but its fair to say that a lot of separate queries will be taking place on the clients concurrently, as well as multiple clients being around while in use.

 

Is this a bug or am I doing something wrong?



 Comments   
Comment by Liam Brown [ 16/Nov/20 ]

Thanks Kevin for the quick and very helpful answer. Verified with netstat -a that that is in fact what is going on here. I'll try sharing Clients and see how that goes, or take other steps with my application code to work around this.

Please feel free to close this as it is clearly not an issue with the Go Driver.

Thanks again.

Comment by Kevin Albertson [ 13/Nov/20 ]

Hi lotawif879@aalyaa.com, thank you for the report!

At a glance, this may be an issue with sockets entering the TIME_WAIT state on Windows.
See: https://help.socketlabs.com/docs/how-to-fix-error-only-one-usage-of-each-socket-address-protocolnetwork-addressport-is-normally-permitted

You can try running netstat -a to verify this.

Each separate part of the application does its own `mongo.Connect` and then calls `client.Disconnect` when it is completed. There are only ever around a dozen or so clients connected at the same time, but its fair to say that a lot of separate queries will be taking place on the clients concurrently, as well as multiple clients being around while in use.

Using separate Clients would contribute to a higher amount connections. And recreating them would end up recreating the connection pool to each server. If this is an issue with sockets entering the TIME_WAIT state, try sharing Clients between goroutines, as it is safe and recommended to do so: https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo#Client. That will share each server's connection pool with each goroutine and should reduce the number of total connections. You can also modify the maximum number of connections a single Client will create to a server by setting a maxPoolSize: https://pkg.go.dev/go.mongodb.org/mongo-driver@v1.4.3/mongo/options#ClientOptions.SetMaxPoolSize

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