firebase之如何增加Firebase数据库读取时间并防止Nginx超时
jackei
阅读:81
2025-06-02 22:19:02
评论:0
上下文:我尝试建立一个聊天服务。在聊天服务中,我有(说: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服务器进行写操作。我会考虑在大小合理的多位置更新中执行此操作,在此确保每个多位置更新最多不超过几秒钟。
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。



