Add cleanup of dead connections no longer reported by a worker (#1220)

This commit is contained in:
Jeff Mitchell authored and GitHub committed 2021-05-14 13:23:12 -04:00
1 parent 0b5ac930ac
commit 6f34da8923
24 files changed
+741 -223

No files matched your search

+49 -2
View File
@@ -89,12 +89,14 @@ select * from terminated;
authorizeConnectionCte = `
insert into session_connection (
session_id,
public_id
public_id,
server_id
)
with active_session as (
select
$1 as session_id,
$2 as public_id
$2 as public_id,
$3 as server_id
from
session s
where
@@ -232,4 +234,49 @@ where
)
)
`
// connectionsToCloseCte finds connections that are:
//
// * not closed
// * not announced by a given server in its latest update
//
// and marks them as closed.
connectionsToCloseCte = `
with
-- Find connections that are not closed so we can reference those IDs
unclosed_connections as (
select connection_id
from session_connection_state
where
-- It's the current state
end_time is null
and
-- Current state isn't closed state
state in ('authorized', 'connected')
and
-- It's not in limbo between when it moved into this state and when
-- it started being reported by the worker, which is roughly every
-- 2-3 seconds
start_time < now() - interval '10 seconds'
),
connections_to_close as (
select public_id
from session_connection
where
-- Related to the worker that just reported to us
server_id = $1
and
-- These are connection IDs that just got reported to us by the given
-- worker, so they should not be considered closed.
%s
-- Only unclosed ones
public_id in (select connection_id from unclosed_connections)
)
update session_connection
set
closed_reason = 'system error'
where
public_id in (select public_id from connections_to_close)
returning public_id
`
)