From dc379ed09f6afa0ef5f7692f87d76d2e9664aedd Mon Sep 17 00:00:00 2001 From: Jim Date: Mon, 5 Oct 2020 17:01:22 -0400 Subject: [PATCH] handle unlimited connections when terminating sessions. (#536) --- internal/db/migrations/postgres.gen.go | 14 ++++++++----- .../migrations/postgres/51_connection.up.sql | 14 ++++++++----- internal/session/query.go | 6 +++++- internal/session/repository_session_test.go | 20 +++++++++++++++++++ 4 files changed, 43 insertions(+), 11 deletions(-) diff --git a/internal/db/migrations/postgres.gen.go b/internal/db/migrations/postgres.gen.go index 61f1635310..dea5eada71 100644 --- a/internal/db/migrations/postgres.gen.go +++ b/internal/db/migrations/postgres.gen.go @@ -4172,11 +4172,15 @@ create or replace function now() > us.expiration_time or -- connection limit reached... ( - select count (*) - from session_connection sc - where - sc.session_id = us.public_id - ) >= connection_limit or + -- handle unlimited connections... + connection_limit != -1 and + ( + select count (*) + from session_connection sc + where + sc.session_id = us.public_id + ) >= connection_limit + ) or -- canceled sessions us.public_id in ( select diff --git a/internal/db/migrations/postgres/51_connection.up.sql b/internal/db/migrations/postgres/51_connection.up.sql index 9f0b5b241d..d9a752df79 100644 --- a/internal/db/migrations/postgres/51_connection.up.sql +++ b/internal/db/migrations/postgres/51_connection.up.sql @@ -336,11 +336,15 @@ create or replace function now() > us.expiration_time or -- connection limit reached... ( - select count (*) - from session_connection sc - where - sc.session_id = us.public_id - ) >= connection_limit or + -- handle unlimited connections... + connection_limit != -1 and + ( + select count (*) + from session_connection sc + where + sc.session_id = us.public_id + ) >= connection_limit + ) or -- canceled sessions us.public_id in ( select diff --git a/internal/session/query.go b/internal/session/query.go index 23400c1944..050ac0dc85 100644 --- a/internal/session/query.go +++ b/internal/session/query.go @@ -182,11 +182,15 @@ where now() > us.expiration_time or -- connection limit reached... ( + -- handle unlimited connections... + connection_limit != -1 and + ( select count (*) from session_connection sc where sc.session_id = us.public_id - ) >= connection_limit or + ) >= connection_limit + ) or -- canceled sessions us.public_id in ( select diff --git a/internal/session/repository_session_test.go b/internal/session/repository_session_test.go index 51ff80335c..f889a6564d 100644 --- a/internal/session/repository_session_test.go +++ b/internal/session/repository_session_test.go @@ -879,6 +879,26 @@ func TestRepository_TerminateCompletedSessions(t *testing.T) { } }, }, + { + name: "sessions-with-unlimited-connections", + setup: func() testArgs { + cnt := 5 + wantTermed := map[string]TerminationReason{} + sessions := make([]*Session, 0, 5) + for i := 0; i < cnt; i++ { + // make one with unlimited connections + s := setupFn(-1, time.Hour+1, false) + // make one with limit of one all connections closed + s2 := setupFn(1, time.Hour+1, false) + sessions = append(sessions, s, s2) + wantTermed[s2.PublicId] = ConnectionLimit + } + return testArgs{ + sessions: sessions, + wantTermed: wantTermed, + } + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {