Skip to content

Commit 8ca134c

Browse files
committed
fix: add timeout to psql activity command and close connection (#660)
1 parent 06ac987 commit 8ca134c

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

engine/internal/retrieval/engine/postgres/logical/activity.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ func dbSourceActivity(ctx context.Context, dbCfg Connection) ([]activity.PGEvent
4343
return nil, fmt.Errorf("failed to connect to DB: %w", err)
4444
}
4545

46+
defer func() { _ = querier.Close(context.Background()) }()
47+
4648
rows, err := querier.Query(ctx, statQuery)
4749
if err != nil {
4850
return nil, fmt.Errorf("failed to perform query to get DB activity: %w", err)
@@ -77,7 +79,10 @@ func pgContainerActivity(ctx context.Context, docker *client.Client, containerID
7779
return []activity.PGEvent{}, nil
7880
}
7981

80-
activityCmd := []string{"psql", "-U", db.User(), "-d", db.Name(), "--record-separator='" + customRecordSeparator + "'", "-XAtc", statQuery}
82+
activityCmd := []string{"psql", "-U", db.User(), "-d", db.Name(),
83+
"--set=connect_timeout=5",
84+
"-c", "SET statement_timeout = '10s';",
85+
"--record-separator='" + customRecordSeparator + "'", "-XAtc", statQuery}
8186

8287
log.Msg("Running activity command: ", activityCmd)
8388

engine/internal/retrieval/engine/postgres/tools/tools.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,10 @@ func processAttachResponse(ctx context.Context, reader io.Reader) ([]byte, error
624624
break
625625

626626
case <-ctx.Done():
627+
if closer, ok := reader.(io.Closer); ok {
628+
_ = closer.Close()
629+
}
630+
627631
return nil, ctx.Err()
628632
}
629633

engine/test/_cleanup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ set -euxo pipefail
33

44
DLE_TEST_MOUNT_DIR="/var/lib/test/dblab_mount"
55
DLE_TEST_POOL_NAME="test_dblab_pool"
6-
TMP_DATA_DIR="/tmp/dle_test/logical_generic"
6+
TMP_DATA_DIR="/tmp/dle_test"
77
ZFS_FILE="$(pwd)/zfs_file"
88

99
# Stop and remove test Docker containers

0 commit comments

Comments
 (0)