登录
首页 >  Golang >  Go问答

如何增加 Firebase 数据库读取时间并防止 Nginx 超时

来源:stackoverflow

时间:2024-04-09 19:18:34 128浏览 收藏

亲爱的编程学习爱好者,如果你点开了这篇文章,说明你对《如何增加 Firebase 数据库读取时间并防止 Nginx 超时》很感兴趣。本篇文章就来给大家详细解析一下,主要介绍一下,希望所有认真读完的童鞋们,都有实质性的提高。

问题内容

上下文:我尝试构建聊天服务。在聊天服务中,我有(比如:50000+)聊天室。 我有 20 名管理员,他们可以访问一些特定的聊天室(比如说:可以访问大约 5000 个聊天室)。因此,我想创建功能,以便我可以添加新管理员并根据我的查询获取聊天室列表(比如说:我从查询中获得了 5000 个聊天室),使用以下命令将该新管理员添加到这 5000 个聊天室中单一端点。我正在使用 golang 和 firebase。

//getadmin user take a userid and it's return a user.
func getadminuser(userid int) (user *user, err error) {
    // it will query on the database 
    // then return the a user
    return user, nil
}

问题是当我传递患者列表并尝试从 firebase 数据库读取患者主题时,大约需要 20 分钟才能读取。因此,nginx 将会超时。

有什么方法可以使用 go 并发来改善 firebase 读取时间,或者任何其他方式可以改善 firebase 的读取并添加它们,而不会造成任何超时错误。

func AddNewAdminToPatientTopics(ctx context.Context, user User, patients []User) error {
    for _, patient := range patients {
        oldTopics := firebase.database.NewRef(fmt.Sprintf("USER_TOPICS/%d", patient.ID))
        for topicID, t := range topics {
            newUserTopics := firebase.database.NewRef(fmt.Sprintf("USER_TOPICS/%d/%s", user.ID, topicID))

            // Add this new admin as a participant in this topic
            topic := firebase.database.NewRef(fmt.Sprintf("TOPICS/%s/Participants/%d", topicID, user.ID))
            participant := &Participant{
                UserID: strconv.Itoa(user.ID),
                LastTimeSeenOnline: time.Now().Unix(),
                .......
            }
            err = topic.Set(ctx, participant)
            if err != nil {
                return err
            }
        }
    }

    return nil
}


func AddManager(w http.ResponseWriter, r *http.Request) {
    // Don't worry about error, I handle them gracefully

    // Get User
    user, err := GetAdminUser(UserID)

    // get patients list
    // Say, In this case you we have 5000+ patients
    patients, err := GetPatients(user.CustomerID)


    // Join this user to all chat rooms that the first admin has
    err = AddNewAdminToTopics(context.Background(), *user, patients)
}

Routers: 

http.HandleFunc("chat/managers/new/add", Post).Then(clinic.AddManager))

解决方案


请不要对真实数据库执行大量读取或写入操作。当服务器正在处理大量读/写时,它不会为来自客户端的请求提供服务。

对于此类处理,我强烈建议在 Firebase 控制台中设置自动备份,然后对该备份中的原始数据执行操作。这样您就可以优化数据处理方式,而不依赖于 Firebase 数据库服务器的响应时间。

当然,您仍将依赖 Firebase 服务器进行写入操作。我会考虑在合理大小的多位置更新中执行此操作,确保每个多位置更新最多花费不超过几秒钟。

好了,本文到此结束,带大家了解了《如何增加 Firebase 数据库读取时间并防止 Nginx 超时》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>