Skip to content

Commit

Permalink
Send FYN before destroying socket
Browse files Browse the repository at this point in the history
  • Loading branch information
donchev7 committed Nov 4, 2020
1 parent 1cf19a9 commit 9c419ed
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/k8s-graceful-shutdown.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import http, { IncomingMessage, ServerResponse } from 'http'
import https from 'https'
import { Http2ServerRequest, Http2ServerResponse } from 'http2'
import { Socket } from 'net'

interface HandlerOptions {
/**
Expand Down Expand Up @@ -127,10 +128,10 @@ const getHealthContextHandler = (options: ContextHandlerOptions) => {
}

function shutdown<T extends http.Server>(server: T) {
const connections = new Set()
const connections = new Set<Socket>()
const closeFn = server.close.bind(server)

function onConnection(socket: any) {
function onConnection(socket: Socket) {
connections.add(socket)
}

Expand All @@ -141,7 +142,12 @@ function shutdown<T extends http.Server>(server: T) {
}

return (callback?: (err: Error) => void) => {
connections.forEach((socket: any) => socket.destroy())
connections.forEach((socket: Socket) => {
if (!socket.destroyed) {
socket.end()
socket.destroy()
}
})
return closeFn(callback)
}
}
Expand Down

0 comments on commit 9c419ed

Please sign in to comment.