Golang/MGO-恐慌:没有可访问的服务器 [英] Golang / MGO -- panic: no reachable servers

查看:56
本文介绍了Golang/MGO-恐慌:没有可访问的服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有连接到Mongo的以下功能.为了进行测试,我关闭了mongod,并希望允许该程序在w/0 mongo不可用的情况下继续运行.如果无法连接服务器,MGO似乎会引发紧急情况,因此我在下面编写了一个延迟/恢复操作,但是紧急情况仍然导致程序退出.从中恢复的正确方法是什么?

I have the following function that connects to Mongo. For testing, I shutdown mongod, and want to allow the program to continue w/0 mongo if it's not available. It seems that MGO throws a panic if the server can't be connected, so I wrote a defer/recover below, but the panic still causes the program to exit. What's the proper way to recover from this?

func connectToMongo(sess *mgo.Session, coll *mgo.Collection, sessionErr error) bool {
    fmt.Println("enter main - connecting to mongo")

    // tried doing this - doesn't work as intended
    defer func() {
        if r := recover(); r != nil {
            var ok bool
            err, ok := r.(error)
            if !ok {
                fmt.Printf("pkg:  %v,  error: %s", r, err)
            }
        }
        return false
    }()

    maxWait := time.Duration(5 * time.Second)
    sess, sessionErr = mgo.DialWithTimeout("localhost", maxWait)
    if sessionErr == nil {
        session.SetMode(mgo.Monotonic, true)
        coll = session.DB("MyDB").C("MyCollection")
    } else { // never gets here
        fmt.Println("Unable to connect to local mongo instance!")
    }
    return true
}

推荐答案

运行发布的代码的以下版本.尽量不要修改代码,至少不要更改行号的位置.这样,如果您发布堆栈跟踪,则数字将匹配.

Run the following version of your posted code. Try to not modify the code, at least not changing the position of the line numbers. That way, if you post a stacktrace, the numbers will match.

package main

import (
    "fmt"
    "time"
)

import (
    "labix.org/v2/mgo"
)

func connectToMongo() bool {
    ret := false
    fmt.Println("enter main - connecting to mongo")

    // tried doing this - doesn't work as intended
    defer func() {
        if r := recover(); r != nil {
            fmt.Println("Detected panic")
            var ok bool
            err, ok := r.(error)
            if !ok {
                fmt.Printf("pkg:  %v,  error: %s", r, err)
            }
        }
    }()

    maxWait := time.Duration(5 * time.Second)
    session, sessionErr := mgo.DialWithTimeout("localhost:27017", maxWait)
    if sessionErr == nil {
        session.SetMode(mgo.Monotonic, true)
        coll := session.DB("MyDB").C("MyCollection")
        if ( coll != nil ) {
            fmt.Println("Got a collection object")
            ret = true
        }
    } else { // never gets here
        fmt.Println("Unable to connect to local mongo instance!")
    }
    return ret
}

func main() {
    if ( connectToMongo() ) {
        fmt.Println("Connected")
    } else {
        fmt.Println("Not Connected")
    }
}

当MongoDB启动时,我看到:

When MongoDB is up, I see:

enter main - connecting to mongo
Got a collection object
Connected

当MongoDB关闭时,我看到:

When MongoDB is down, I see:

enter main - connecting to mongo
Unable to connect to local mongo instance!
Not Connected

如果您没有看到相同的行为,请发布输出,包括您看到的恐慌.

If you don't see the same behavior, post the output, including the panic you see.

这篇关于Golang/MGO-恐慌:没有可访问的服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆