[HttpApiController] implement the disconnection of a single client

The http route returns as soon as the client has fully disconnected.
This commit is contained in:
Jakob Ackermann 2020-02-24 13:28:22 +01:00 committed by Jakob Ackermann
parent 91e296533f
commit de35fc5ecf
2 changed files with 16 additions and 1 deletions

View file

@ -19,3 +19,17 @@ module.exports = HttpApiController =
logger.log {rate}, "setting client drain rate" logger.log {rate}, "setting client drain rate"
DrainManager.startDrain io, rate DrainManager.startDrain io, rate
res.send 204 res.send 204
disconnectClient: (req, res, next) ->
io = req.app.get("io")
client_id = req.params.client_id
client = io.sockets.sockets[client_id]
if !client
logger.info({client_id}, "api: client already disconnected")
res.sendStatus(404)
return
logger.warn({client_id}, "api: requesting client disconnect")
client.on "disconnect", () ->
res.sendStatus(204)
client.disconnect()

View file

@ -53,6 +53,7 @@ module.exports = Router =
app.post "/project/:project_id/message/:message", httpAuth, bodyParser.json(limit: "5mb"), HttpApiController.sendMessage app.post "/project/:project_id/message/:message", httpAuth, bodyParser.json(limit: "5mb"), HttpApiController.sendMessage
app.post "/drain", httpAuth, HttpApiController.startDrain app.post "/drain", httpAuth, HttpApiController.startDrain
app.post "/client/:client_id/disconnect", httpAuth, HttpApiController.disconnectClient
session.on 'connection', (error, client, session) -> session.on 'connection', (error, client, session) ->
client?.on "error", (err) -> client?.on "error", (err) ->