# 无限的请求到 /presence/update

**URL:** <https://meta.discourse.org/t/infinite-requests-to-presence-update/268063>\
**Category:** Bug\
**Created:** [2023 年6 月 12 日 15:48 UTC](https://meta.discourse.org/t/infinite-requests-to-presence-update/268063 "2023-06-12T15:48:22Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

作者： ![AhmedLoud](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ahmedloud/32/576547_2.png) [@AhmedLoud](https://meta.discourse.org/u/AhmedLoud)\
发布日期： [2023 年6 月 12 日 15:48 UTC](https://meta.discourse.org/t/infinite-requests-to-presence-update/268063/1 "2023-06-12T15:48:22Z")

</div>

好的，这可能不算是严格意义上的“bug”，也许是设计如此，但我对 JS 端如何更新在线状态有些疑问。文件是 `app/assets/javascripts/discourse/app/services/presence.js`

到目前为止，我的理解是 JS 客户端：

- 当更新队列不为空时，会将更新频率限制为每秒一次
- 然后在没有更改时每 30 秒发送一次请求

我注意到的是，如果服务器因任何原因出错，JS 将继续每秒发送请求。同时会在控制台产生错误 `Uncaught (in promise)`，第 565 行。  
它不应该在 N 次错误后停止，以免使服务器过载吗？

### 如何复现？

在 `presence_controller.rb` 的 `update` 方法中模拟一个错误。（在我的例子中，我只是为了演示而渲染了状态 405）

```ruby
  def update

    # JS 客户端设计为每秒节流一次
    # 当没有进行更改时，它每 30 秒发送一次请求
    RateLimiter.new(nil, "update-presence-#{current_user.id}", 20, 10.seconds).performed!
    render json: { error: "Not authorized" }, status: 405
    return
    ...

```

然后在客户端进入任何带有在线状态管理的屏幕。我选择了主题回复屏幕并开始回复。

 ![image](https://global.discourse-cdn.com/meta/original/4X/7/9/e/79ebb115810417059fe0441775d350f8aa0a6847.png)

结果是它会永远每秒钟持续发送请求。

---

_[View the full topic](https://meta.discourse.org/t/infinite-requests-to-presence-update/268063)._
