TCP连接管理

huangapple go评论97阅读模式
英文:

TCP connection management

问题

我在Go邮件列表中提出了这个问题,但我认为从Stack Overflow上获得更好的回答会更普遍。

在使用Java/.Net平台时,我从未手动管理过数据库连接,因为驱动程序会处理它。现在,当尝试使用非关系型数据库并且只有基本驱动程序支持时,我需要负责管理连接。驱动程序允许连接、关闭、重新连接到一个TCP端口,但我不确定应该如何管理它(参见链接)。我是否需要为每个数据库请求创建一个新的连接?我可以使用其他第三方连接池库吗?

谢谢。

英文:

I have this question asked in the Go mailing list, but I think it is more general to get better response from SO.

When work with Java/.Net platform, I never had to manage database connection manually as the drivers handle it. Now, when try to connect to a no sql db with very basic driver support, it is my responsibility to manage the connection. The driver let connect, close, reconnect to a tcp port, but not sure how should i manage it (see the link). Do i have to create a new connection for each db request? can I use other 3rd party connection pooling libraries?

thanks.

答案1

得分: 1

我对MongoDB的了解不足以直接回答这个问题,但是你知道MongoDB如何处理TCP请求吗?例如,单个TCP连接的一个问题可能是数据库会按顺序处理每个请求,这可能导致高延迟,即使它可能在单台机器上成为瓶颈并且可以处理更高的容量。

这些机器都在本地网络上运行吗?如果是这样,打开一个新连接的成本不会太高,从性能角度来看甚至可能是微不足道的。

我的建议是:每个请求使用一个TCP连接,然后进行性能分析,看看会发生什么。如果你自己进行DoS攻击,很容易添加连接池,但这可能永远不会成为一个问题。这样现在就可以工作了,而且你不必去处理可能带来更多问题的第三方库。

此外,TCP编程非常简单。不要被它吓到,检测关闭的套接字并同步或异步重新连接很简单。

英文:

I don't know enough about MongoDB to answer this question directly, but do you know how MongoDB handles requests over TCP? For example, one problem with a single TCP connection can be that the db will handle each request serially, potentially causing high latency even though it may be bottlenecking on a single machine and could handle a higher capacity.

Are the machines all running on a local network? If so, the cost of opening a new connection won't be too high, and might even be insignificant from a performance perspective regardless.

My two cents: Do one TCP connection per request and just profile it and see what happens. It is very easy to add pooling later if you're DoSing yourself, but it may never be a problem. That'll work right now, and you won't have to mess around with a third party library that may cause more problems than it solves.

Also, TCP programming is really easy. Don't be intimidated by it, detecting a closed socket, and reconnecting synchronously or asynchronously is simple.

答案2

得分: 0

大多数MongoDB驱动程序(客户端)在连接到服务器时会创建和使用连接池。每个套接字(连接)在服务器上一次只能执行一个操作;由于数据是从套接字读取的方式,您可以发出多个请求,服务器将按顺序获取它们并在每个请求完成后返回数据。

有一个Go的MongoDB驱动程序,但似乎没有连接池功能。http://github.com/mikejs/gomongo

英文:

Most mongodb drivers (clients) will create and use a connection pool when connecting to the server. Each socket (connection) can do one operation at a time at the server; because of how data is read off the socket you can issue many requests and server will just get them one after another and return data as each one completes.

There is a Go mongo db driver but it doesn't seem to do connection pooling. http://github.com/mikejs/gomongo

答案3

得分: 0

除了这里的答案之外:如果你发现你确实需要做一些连接池的工作,redis.go 是一个很好的数据库驱动程序连接池的例子。具体来说,看一下源代码中的 Client.popConClient.pushCon 方法。

英文:

In addition to the answers here: if you find you do need to do some kind of connection pooling redis.go is a decent example of a database driver that pools connections. Specifically, look at the Client.popCon and Client.pushCon methods in the source.

huangapple
  • 本文由 发表于 2011年2月12日 04:35:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/4973763.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定