【问题标题】:How can I increase firebase database read time and prevent timeout from Nginx如何增加 Firebase 数据库读取时间并防止 Nginx 超时
【发布时间】: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


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-23
      • 2023-03-17
      相关资源
      最近更新 更多