mgo pool limit demo

88 views
Skip to first unread message

宋亚伟

unread,
Oct 17, 2017, 10:01:41 PM10/17/17
to mgo-users
mgo no pool code


package main

import (
"fmt"
"runtime"
"time"

mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)

func main() {
session, err := mgo.Dial("localhost:27017")
if err != nil {
fmt.Println(err)
}
//session.DB("mgo-copy-20171018084604").C("Content").Insert(bson.M{"name": "songqingshu"})
session.SetMode(mgo.Monotonic, true)

//s := session.Copy()
//defer s.Close()
//s.DB("mgo-copy-20171018084916").C("Content").Insert(bson.M{"name": "songqingshu"})

for i := 0; i < 200000; i++ {
go func() {
s := session.Copy()
defer s.Close()
s.DB("mgo-copy-20171018084916").C("Content").Insert(bson.M{"name": "songqingshu"})
}()
runtime.Gosched()
}
time.Sleep(time.Second * 3)
}


mgo with pool code

package main

import (
"log"
"time"

"runtime"

"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)

func main() {

mgoDialInfo := &mgo.DialInfo{
//Addrs: []string{config.Hosts},
Addrs: []string{"localhost"},
Direct: false,
Timeout: time.Second * 1,
//PoolLimit: 4096, // Session.SetPoolLimit
PoolLimit: 10, // Session.SetPoolLimit
}

//创建一个维护套接字池的session
session, err := mgo.DialWithInfo(mgoDialInfo)
if err != nil {
log.Println(err.Error())
}

session.SetMode(mgo.Monotonic, true)

for i := 0; i < 200000; i++ {
go func() {
s := session.Copy()
defer s.Close()
s.DB("mgo-copy-20171018084916").C("Content").Insert(bson.M{"name": "songqingshu"})
}()
runtime.Gosched()
}
time.Sleep(time.Second * 30)
}


go run, then you know.



Reply all
Reply to author
Forward
0 new messages