Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问sponge自带的mysql和mgo 在用户访问量少的时候会断开重连吗 #72

Open
hanwenbo opened this issue Oct 16, 2024 · 5 comments

Comments

@hanwenbo
Copy link
Contributor

守护进程模式 mysql和mgo 会自动重新连接吗,因为我最近发现用户访问少的时候 突然访问一下 mgo会查询失败 我目前猜测是长时间没操作 断开了 遇到过几次

@zhufuyi
Copy link
Owner

zhufuyi commented Oct 17, 2024

mongodb服务重启网络断开后恢复之后都可以正常访问api获取到mongodb数据的。查询mongodb失败有日志记录的,可以从日志看查询失败原因。

@hanwenbo
Copy link
Contributor Author

排查到了 确实mgo会断开 不是长链接,我需要琢磨下怎么保持长连接

@hanwenbo
Copy link
Contributor Author

我在model加了一个 CheckAndReconnect 测试一段时间 没问题的话我再反馈

@zhufuyi
Copy link
Owner

zhufuyi commented Oct 18, 2024

可以设置连接池来保持连接的活跃,示例:

    // case 1: 在dsn设置连接池
    db, err := mgo.Init("mongodb://root:[email protected]:27017/account?socketTimeoutMS=30000&maxPoolSize=100&minPoolSize=1&maxConnIdleTimeMS=300000")

    // 或者 在代码设置连接池
    // import "go.mongodb.org/mongo-driver/mongo/options"
    db, err := mgo.Init("mongodb://root:[email protected]:27017/account",
        options.Client().SetMaxPoolSize(100),
        options.Client().SetMinPoolSize(1),
        options.Client().SetMaxConnIdleTime(10*time.Minute),
        options.Client().SetSocketTimeout(30*time.Second),
    )

@hanwenbo
Copy link
Contributor Author

和maxConnIdleTimeMS应该没关系 因为mongo-driver默认是0 无限使用,我还在排查

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants