Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): gracefully close websocket connections on server shutdown #143

Merged
merged 3 commits into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/server/src/launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ export class Launcher {
if (!this.shuttingDown) {
this.shuttingDown = true

this.logger.info('Server gracefully closing down...')

laurentlp marked this conversation as resolved.
Show resolved Hide resolved
await this.api.sockets.destroy()
await this.httpTerminator?.terminate()
await this.app.destroy()
}
Expand Down
25 changes: 23 additions & 2 deletions packages/server/src/socket/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,38 @@ import { SocketService } from './service'

export class SocketManager {
private logger = new Logger('Socket')
private ws: Socket.Server | undefined
private handlers: { [type: string]: SocketHandler } = {}

constructor(private sockets: SocketService) {}

async setup(server: Server) {
if (yn(process.env.ENABLE_EXPERIMENTAL_SOCKETS)) {
const ws = new Socket.Server(server, { cors: { origin: '*' } })
ws.on('connection', this.handleSocketConnection.bind(this))
this.ws = new Socket.Server(server, { cors: { origin: '*' } })
this.ws.on('connection', this.handleSocketConnection.bind(this))
}
}

async destroy() {
if (this.ws !== undefined) {
laurentlp marked this conversation as resolved.
Show resolved Hide resolved
await new Promise((resolve, reject) => {
// This is kind of hack to make sure that socket.io does not
// try to close the HTTP server before http-terminator
this.ws!['httpServer'] = undefined
samuelmasse marked this conversation as resolved.
Show resolved Hide resolved

this.ws!.close((err) => {
if (err) {
return reject(err)
}
laurentlp marked this conversation as resolved.
Show resolved Hide resolved

resolve(undefined)
})
})
}

await this.sockets.destroy()
laurentlp marked this conversation as resolved.
Show resolved Hide resolved
}

public handle(type: string, callback: SocketHandler) {
this.handlers[type] = callback
}
Expand Down
13 changes: 13 additions & 0 deletions packages/server/src/socket/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ export class SocketService extends Service {
this.cacheByUserId = await this.cachingService.newServerCache('cache_sockets_by_user_id')
}

async destroy() {
for (const sockets of Object.values(this.socketsByUserId)) {
if (!sockets) {
continue
}

for (const socket of sockets) {
// TODO: Delete the socket from the cache and invalidate once we distribute the socket usage
socket.disconnect()
}
}
}
laurentlp marked this conversation as resolved.
Show resolved Hide resolved

public create(socket: Socket) {
this.sockets[socket.id] = {}
}
Expand Down