cleanupConnections closes hijacked connections. Depending on the value of StreamCloseDelay it does that either immediately or sets up a timer that will do that later.
()
| 427 | // Depending on the value of StreamCloseDelay it does that either immediately |
| 428 | // or sets up a timer that will do that later. |
| 429 | func (h *Handler) cleanupConnections() error { |
| 430 | if h.StreamCloseDelay == 0 { |
| 431 | return h.closeConnections() |
| 432 | } |
| 433 | |
| 434 | h.connectionsMu.Lock() |
| 435 | defer h.connectionsMu.Unlock() |
| 436 | // the handler is shut down, no new connection can appear, |
| 437 | // so we can skip setting up the timer when there are no connections |
| 438 | if len(h.connections) > 0 { |
| 439 | delay := time.Duration(h.StreamCloseDelay) |
| 440 | h.connectionsCloseTimer = time.AfterFunc(delay, func() { |
| 441 | if c := h.logger.Check(zapcore.DebugLevel, "closing streaming connections after delay"); c != nil { |
| 442 | c.Write(zap.Duration("delay", delay)) |
| 443 | } |
| 444 | err := h.closeConnections() |
| 445 | if err != nil { |
| 446 | if c := h.logger.Check(zapcore.ErrorLevel, "failed to closed connections after delay"); c != nil { |
| 447 | c.Write( |
| 448 | zap.Error(err), |
| 449 | zap.Duration("delay", delay), |
| 450 | ) |
| 451 | } |
| 452 | } |
| 453 | }) |
| 454 | } |
| 455 | return nil |
| 456 | } |
| 457 | |
| 458 | // writeCloseControl sends a best-effort Close control message to the given |
| 459 | // WebSocket connection. Thanks to @pascaldekloe who provided inspiration |