RunReaper 阻塞运行死连接清理:每 ReaperInterval 扫描一次注册表, 关闭超过 DeadConnectionTimeout 没收到任何帧的连接。ctx 取消时返回。 一般由 ListenAndServe 在 goroutine 中调用;外部直接持有 Hub 时也可手动调。
(ctx context.Context)
| 154 | // 关闭超过 DeadConnectionTimeout 没收到任何帧的连接。ctx 取消时返回。 |
| 155 | // 一般由 ListenAndServe 在 goroutine 中调用;外部直接持有 Hub 时也可手动调。 |
| 156 | func (h *Hub) RunReaper(ctx context.Context) { |
| 157 | t := time.NewTicker(h.reaperInterval) |
| 158 | defer t.Stop() |
| 159 | for { |
| 160 | select { |
| 161 | case <-ctx.Done(): |
| 162 | return |
| 163 | case now := <-t.C: |
| 164 | h.reapOnce(now) |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | func (h *Hub) reapOnce(now time.Time) { |
| 170 | threshold := now.Add(-h.deadConnTimeout) |