I haven't tested yet but I think this code should work.
I get this issue after keep a session for long time so that I have timer to renew session every certain time.
package main
import (
"gopkg.in/mgo.v2"
"time"
"fmt"
)
// in database name is a string and age is an int
type Dog struct{
Breed string "breed"
}
type Person struct{
Name string "name"
Pet Dog `bson:",inline"`
Ts time.Time
}
func insert(session *mgo.Session, bob Person){
err := session.DB("db_log").C("people").Insert(&bob)
if err != nil {
panic("Could not insert into database")
}
}
func main() {
current_session, _ := mgo.Dial("localhost:27017")
using_session := current_session
bob := Person{Name : "Robert", Pet : Dog{}}
/*
* this technical to prevent connect timeout after long time connection on mongodb from golang session
* Idea is simple: the session will be renew after certain time such as 1 hour
*/
//ticker := time.NewTicker(time.Hour * 1)
//Set 10 seconds for test
ticker := time.NewTicker(time.Second * 10)
go func() {
for t := range ticker.C {
fmt.Println("Tick at", t)
new_session := current_session.Copy()
fmt.Printf("Current session here %p\n", current_session)
fmt.Printf("New session here %p\n", new_session)
using_session = new_session
//setTimeout 30 second before close old sesion, to make sure current instance use current connection isn't affect
//time.AfterFunc(time.Second * 30, func() {
//Set 2 seconds for test
time.AfterFunc(time.Second * 2, func() {
//close previous session
current_session.Close()
current_session = new_session
//assign to new session
})
}
}()
i := 0
for {
time.Sleep(time.Duration(1) * time.Microsecond)
i++
go insert(using_session, bob)
}
}