本文整理汇总了Golang中github.com/coralproject/shelf/internal/platform/db.DB.ExecuteMGOTimeout方法的典型用法代码示例。如果您正苦于以下问题:Golang DB.ExecuteMGOTimeout方法的具体用法?Golang DB.ExecuteMGOTimeout怎么用?Golang DB.ExecuteMGOTimeout使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/coralproject/shelf/internal/platform/db.DB
的用法示例。
在下文中一共展示了DB.ExecuteMGOTimeout方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: execPipeline
//.........这里部分代码省略.........
defer cleanupView(context, db, viewCol)
}
// Do we want the explain output.
if explain {
// Build the pipeline function for the execution for explain.
var m bson.M
f := func(c *mgo.Collection) error {
log.Dev(context, "executePipeline", "MGO Explain :\ndb.%s.aggregate([\n%s])", c.Name, agg)
return c.Pipe(pipeline).Explain(&m)
}
// Execute the pipeline.
if err := db.ExecuteMGO(context, q.Collection, f); err != nil {
return docs{}, commands, err
}
return docs{q.Name, []bson.M{m}}, commands, nil
}
// Set the default timeout for the session.
timeout := 25 * time.Second
if q.Timeout != "" {
if d, err := time.ParseDuration(q.Timeout); err != nil {
log.Dev(context, "executePipeline", "WARNING : Unable to Set Timeout[%s], using default.", q.Timeout)
} else {
timeout = d
}
}
log.Dev(context, "executePipeline", "MGO Timeout Set[%s]", timeout)
// Build the pipeline function for the execution.
var results []bson.M
f := func(c *mgo.Collection) error {
log.Dev(context, "executePipeline", "MGO Started\ndb.%s.aggregate([\n%s])", c.Name, agg)
err := c.Pipe(pipeline).All(&results)
return err
}
// Set the channel to one because we might not be around
// waiting for the result on timeouts.
wait := make(chan error, 1)
// Execute the pipeline.
go func() {
defer func() {
if r := recover(); r != nil {
log.Dev(context, "executePipeline", "******> Recovered from timing out")
}
log.Dev(context, "executePipeline", "MGO Response Complete")
}()
wait <- db.ExecuteMGOTimeout(context, timeout, q.Collection, f)
}()
// Did any errors occur.
select {
// Wait for the response from executing the pipeline.
case err := <-wait:
if err != nil {
if _, ok := err.(*net.OpError); ok {
log.Error(context, "executePipeline", err, "Timed out Network")
return docs{}, commands, errors.New("Completed : Timed out executing commands")
}
log.Error(context, "executePipeline", err, "Completed")
return docs{}, commands, err
}
// Wait to timeout the entire operation.
case <-time.After(timeout):
err := errors.New("Timedout executing commands")
log.Error(context, "executePipeline", err, "Completed : Timed out Processing")
return docs{}, commands, err
}
log.Dev(context, "executePipeline", "Completed")
// If there were no results, return an empty array.
if results == nil {
return docs{q.Name, []bson.M{}}, commands, nil
}
// Perform any masking that is required.
if err := processMasks(context, db, q.Collection, results); err != nil {
return docs{}, commands, err
}
// Do we need to save the result.
if save != nil {
if err := saveResult(context, save, results, data); err != nil {
return docs{}, commands, err
}
}
return docs{q.Name, results}, commands, nil
}