【发布时间】:2019-10-23 18:38:15
【问题描述】:
上下文:我尝试构建一个聊天服务。在聊天服务中,我有(比如:50000+)个聊天室。 我有 20 个管理员可以访问一些特定的聊天室(比如说:可以访问大约 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 concurrency 或任何其他方法来改善 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 go firebase-realtime-database concurrency firebase-cloud-messaging