Go 戈朗氧化镁起搏

Go 戈朗氧化镁起搏,go,mgo,Go,Mgo,我正在写一个快速写入mongodb的应用程序。对于mongodb和mgo来说,处理速度太快。我的问题是,有没有办法让我确定mongo无法跟上并开始拦截?但我也不想不必要地阻止。 下面是模拟问题的代码示例: package main import ( "labix.org/v2/mgo" "time" "fmt" ) // in database name is a string and age is an int type Dog struct{ Breed string

我正在写一个快速写入mongodb的应用程序。对于mongodb和mgo来说,处理速度太快。我的问题是,有没有办法让我确定mongo无法跟上并开始拦截?但我也不想不必要地阻止。 下面是模拟问题的代码示例:

package main

import (
  "labix.org/v2/mgo"
  "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() {
  session, _ := mgo.Dial("localhost:27017")
  bob := Person{Name : "Robert", Pet : Dog{}}
  i := 0
  for {
    time.Sleep(time.Duration(1) * time.Microsecond)
    i++
    go insert(session, bob)
  }
}
我经常会遇到如下错误:

panic: Could not insert into database


我怀疑,如果您和您的会话成功,您将获得更好的性能

为了回答您的问题,这可能是一个完美的频道用例。在一个goroutine中将项目馈送到通道中,并在另一个goroutine中将其消费/写入Mongo。您可以根据需要调整频道的大小。生产者线程在尝试发送到频道时,一旦频道已满,就会阻塞


您可能还想使用方法设置。设置W:0将使Mongo处于火灾和遗忘模式,这将极大地提高性能,但有丢失一些数据的风险。您还可以更改超时时间。

我怀疑,如果您和您的会话正常运行,您将获得更好的性能

为了回答您的问题,这可能是一个完美的频道用例。在一个goroutine中将项目馈送到通道中,并在另一个goroutine中将其消费/写入Mongo。您可以根据需要调整频道的大小。生产者线程在尝试发送到频道时,一旦频道已满,就会阻塞


您可能还想使用方法设置。设置W:0将使Mongo处于火灾和遗忘模式,这将极大地提高性能,但有丢失一些数据的风险。您还可以更改超时时间。

我还没有测试,但我认为这段代码应该可以工作。 我得到这个问题后,保持了很长一段时间的会议,使我有计时器更新会话每一个特定的时间

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)
  }

}

我还没有测试,但我认为这段代码应该可以工作。 我得到这个问题后,保持了很长一段时间的会议,使我有计时器更新会话每一个特定的时间

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)
  }

}

@EvanShaw你的建议是什么?在不了解相关应用程序的情况下,很难提出建议,但PostgreSQL通常是一个不错的默认选择。如果写意见很酷,那么我将提供一个与@EvanShaw相反的意见。我们在生产中使用Mongo,并且非常喜欢它。我们从中得到了很好的表现。不要让仇恨者来打扰你。我还没有听到一个无法反驳的反对Mongo的论点。Server Density的David Mytton在一年半前写道,我建议您阅读。@EvanShaw您的建议是什么?在不了解相关应用程序的情况下,很难提出建议,但PostgreSQL通常是一个不错的默认选择。如果写意见很酷,然后我将对@EvanShaw提出相反的意见。我们在生产中使用Mongo,并且非常喜欢它。我们从中得到了很好的表现。不要让仇恨者来打扰你。我还没有听到一个无法反驳的反对Mongo的论点。服务器密度的David Mytton在一年半前写道,我推荐阅读。好的,谢谢。我会尝试一下,明天再给你回复。我确实尝试过使用复制然后关闭。我收到一些错误,看起来像:2014/01/27 18:28:36 http:Accept错误:Accept tcp[::]:9090:打开的文件太多;在1中重试另外,我已经查看了安全参数,并且处于非常不安全的模式,因此应该非常快。我不太了解服务器对tcp的限制以及其他限制。我还打印了我的mgo.GetStats,它看起来像这样:{0 141-140 339585 170373 170373 1 1 224},所以这似乎是一个SocketUse。我猜你在使用OSX?:-您需要增加以获得更多到Mongo的并发连接。好的,谢谢。我会尝试一下,明天再给你回复。我确实尝试过使用复制然后关闭。我收到一些错误,看起来像:2014/01/27 18:28:36 http:Accept错误:Accept tcp[::]:9090:打开的文件太多;在1中重试另外,我已经查看了安全参数,并且处于非常不安全的模式,因此应该非常快。我不太了解服务器对tcp的限制以及其他限制。我还打印了我的mgo.GetStats,它看起来像这样:{0 141-140 339585 170373 170373 1 1 224},所以这似乎是一个SocketUse。我猜你在使用OSX?:-为了获得更多到Mongo的并发连接,您需要增加。