| 1276 | } |
| 1277 | |
| 1278 | void StopHTTPServer() |
| 1279 | { |
| 1280 | LogDebug(BCLog::HTTP, "Stopping HTTP server"); |
| 1281 | |
| 1282 | LogDebug(BCLog::HTTP, "Waiting for HTTP worker threads to exit\n"); |
| 1283 | g_threadpool_http.Stop(); |
| 1284 | |
| 1285 | if (g_http_server) { |
| 1286 | // Must precede DisconnectAllClients(): a connection accepted after |
| 1287 | // GetConnectionsCount() returns 0 would survive into the destructor. |
| 1288 | g_http_server->StopAccepting(); |
| 1289 | // Disconnect clients as their remaining responses are flushed |
| 1290 | g_http_server->DisconnectAllClients(); |
| 1291 | // Wait 30 seconds for all disconnections |
| 1292 | LogDebug(BCLog::HTTP, "Waiting for HTTP clients to disconnect gracefully"); |
| 1293 | const auto deadline{NodeClock::now() + 30s}; |
| 1294 | while (g_http_server->GetConnectionsCount() != 0) { |
| 1295 | if (NodeClock::now() > deadline) { |
| 1296 | LogWarning("Timeout waiting for HTTP clients to disconnect gracefully, continuing shutdown"); |
| 1297 | break; |
| 1298 | } |
| 1299 | std::this_thread::sleep_for(50ms); |
| 1300 | } |
| 1301 | // Break HTTPServer I/O loop: stop accepting connections, sending and receiving data |
| 1302 | g_http_server->InterruptNet(); |
| 1303 | // Wait for HTTPServer I/O thread to exit |
| 1304 | g_http_server->JoinSocketsThreads(); |
| 1305 | // Force-remove any clients that survived the graceful wait |
| 1306 | g_http_server->ClearConnectedClients(); |
| 1307 | // Close all listening sockets |
| 1308 | g_http_server->StopListening(); |
| 1309 | } |
| 1310 | LogDebug(BCLog::HTTP, "Stopped HTTP server"); |
| 1311 | } |
| 1312 | } // namespace http_bitcoin |
no test coverage detected