Refacto load all functions
Unbounded LoadAll* loaders materialised an entire result set in one query with no ceiling. A table that is small in development can grow without bound in production, so these loaders were a latent memory and query-time hazard. Remove the LoadAll* methods from pkg/coredata and walk the cursor- paginated LoadBy* siblings instead through a shared page.LoadAll helper. The helper advances a MaxCursorSize forward cursor until the result set is exhausted and concatenates the pages. It caps a single call at MaxLoadAllPages (20) batches of 500 rows and errors past that rather than materialising an unbounded set, so a runaway caller fails loudly instead of exhausting memory. Callers that genuinely need every row now express that explicitly, and the coredata load-naming rule and docs are updated to discourage new unbounded loaders. Signed-off-by: Sacha Al Himdani <sacha@probo.com>
This commit is contained in:
committed by
Sacha Al Himdani
parent
853f2404a6
commit
9ab8ea2085
@@ -634,75 +634,6 @@ WHERE
|
||||
return nil
|
||||
}
|
||||
|
||||
func (tps *TrackerPatterns) LoadAllByCookieBannerID(
|
||||
ctx context.Context,
|
||||
conn pg.Querier,
|
||||
scope Scoper,
|
||||
cookieBannerID gid.GID,
|
||||
filter *TrackerPatternFilter,
|
||||
trackerType *TrackerType,
|
||||
) error {
|
||||
trackerTypeFragment := "TRUE"
|
||||
if trackerType != nil {
|
||||
trackerTypeFragment = "tracker_type = @tracker_type"
|
||||
}
|
||||
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
organization_id,
|
||||
cookie_banner_id,
|
||||
cookie_category_id,
|
||||
common_tracker_pattern_id,
|
||||
third_party_id,
|
||||
tracker_type,
|
||||
pattern,
|
||||
match_type,
|
||||
display_name,
|
||||
description,
|
||||
excluded,
|
||||
max_age_seconds,
|
||||
source,
|
||||
last_matched_at,
|
||||
mapping_requested_at,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
tracker_patterns
|
||||
WHERE
|
||||
%s
|
||||
AND cookie_banner_id = @cookie_banner_id
|
||||
AND %s
|
||||
AND %s
|
||||
ORDER BY
|
||||
created_at ASC, id ASC;
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment(), trackerTypeFragment, filter.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"cookie_banner_id": cookieBannerID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
maps.Copy(args, filter.SQLArguments())
|
||||
|
||||
if trackerType != nil {
|
||||
args["tracker_type"] = *trackerType
|
||||
}
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query tracker patterns: %w", err)
|
||||
}
|
||||
|
||||
patterns, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[TrackerPattern])
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot collect tracker patterns: %w", err)
|
||||
}
|
||||
|
||||
*tps = patterns
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (tps *TrackerPatterns) RefreshLastMatchedAtByCookieBannerID(
|
||||
ctx context.Context,
|
||||
tx pg.Tx,
|
||||
|
||||
Reference in New Issue
Block a user