2 回答
TA贡献1842条经验 获得超12个赞
首先,我们需要看看mgo.Session.Copy()和mgo.Session.Clone()之间的区别。当go.Session.Clone()返回一个新会话时,该会话使用相同的套接字连接。这不一定是坏事,但请记住,在服务器端,每个连接分配一个堆栈。所以会话将共享相同的堆栈。根据您的用例,这可能会产生很大的不同。
这就是问题所在——如果你为每条记录打开一个新的套接字连接,这会导致三向握手,这很慢。重用相同的套接字减少了这种开销,但仍然存在一些并且具有上述缺点。
我倾向于做的是为每个长时间运行的工作单元建立一个新连接。一个简单的例子说明了这一点:
package main
import (
"fmt"
mgo "gopkg.in/mgo.v2"
bson "gopkg.in/mgo.v2/bson"
"net/http"
)
var (
Database *mgo.Database
)
// The listEntries lists all posts
func listPosts(w http.ResponseWriter, r *http.Request) {
// We have a rather long running unit of work
// (reading and listing all posts)
// So it is worth copying the session
collection := Database.C("posts").With( Database.Session.Copy() )
post := bson.D{}
posts := collection.Find(bson.M{}).Iter()
for posts.Next(&post) {
// Process posts and send it to w
}
}
func main() {
session, _ := mgo.Dial("mongodb://localhost:27017")
Database := session.DB("myDb")
// Count is a rather fast operation
// No need to copy the session here
count, _ := Database.C( "posts" ).Count()
fmt.Printf("Currently %d posts in the database", count )
http.HandleFunc("/posts", listPosts)
http.ListenAndServe(":8080", nil)
}
TA贡献1811条经验 获得超4个赞
是的,复制一个会话来执行一个或几个操作是好的,让mgo中的连接池来提高性能。一台 mongo 服务器的默认限制是 4096,以防止连接过多。
func newSession(consistency Mode, cluster *mongoCluster, timeout time.Duration) (session *Session) {
cluster.Acquire()
session = &Session{
cluster_: cluster,
syncTimeout: timeout,
sockTimeout: timeout,
poolLimit: 4096,
}
debugf("New session %p on cluster %p", session, cluster)
session.SetMode(consistency, true)
session.SetSafe(&Safe{})
session.queryConfig.prefetch = defaultPrefetch
return session
}
- 2 回答
- 0 关注
- 195 浏览
添加回答
举报